"Fossies" - the Fresh Open Source Software Archive

Member "buildbot-2.3.1/buildbot/db/masters.py" (23 May 2019, 3916 Bytes) of package /linux/misc/buildbot-2.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "masters.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.1.0_vs_2.2.0.

    1 # This file is part of Buildbot.  Buildbot is free software: you can
    2 # redistribute it and/or modify it under the terms of the GNU General Public
    3 # License as published by the Free Software Foundation, version 2.
    4 #
    5 # This program is distributed in the hope that it will be useful, but WITHOUT
    6 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    7 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    8 # details.
    9 #
   10 # You should have received a copy of the GNU General Public License along with
   11 # this program; if not, write to the Free Software Foundation, Inc., 51
   12 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   13 #
   14 # Copyright Buildbot Team Members
   15 
   16 
   17 import sqlalchemy as sa
   18 
   19 from buildbot.db import base
   20 from buildbot.util import epoch2datetime
   21 
   22 
   23 class MasterDict(dict):
   24     pass
   25 
   26 
   27 class MastersConnectorComponent(base.DBConnectorComponent):
   28     data2db = {"masterid": "id", "link": "id"}
   29 
   30     def findMasterId(self, name):
   31         tbl = self.db.model.masters
   32         return self.findSomethingId(
   33             tbl=tbl,
   34             whereclause=(tbl.c.name == name),
   35             insert_values=dict(
   36                 name=name,
   37                 name_hash=self.hashColumns(name),
   38                 active=0,  # initially inactive
   39                 last_active=self.master.reactor.seconds()
   40             ))
   41 
   42     # returns a Deferred that returns a value
   43     def setMasterState(self, masterid, active):
   44         def thd(conn):
   45             tbl = self.db.model.masters
   46             whereclause = (tbl.c.id == masterid)
   47 
   48             # get the old state
   49             r = conn.execute(sa.select([tbl.c.active],
   50                                        whereclause=whereclause))
   51             rows = r.fetchall()
   52             r.close()
   53             if not rows:
   54                 return False  # can't change a row that doesn't exist..
   55             was_active = bool(rows[0].active)
   56 
   57             if not active:
   58                 # if we're marking inactive, then delete any links to this
   59                 # master
   60                 sch_mst_tbl = self.db.model.scheduler_masters
   61                 q = sch_mst_tbl.delete(
   62                     whereclause=(sch_mst_tbl.c.masterid == masterid))
   63                 conn.execute(q)
   64 
   65             # set the state (unconditionally, just to be safe)
   66             q = tbl.update(whereclause=whereclause)
   67             q = q.values(active=1 if active else 0)
   68             if active:
   69                 q = q.values(last_active=self.master.reactor.seconds())
   70             conn.execute(q)
   71 
   72             # return True if there was a change in state
   73             return was_active != bool(active)
   74         return self.db.pool.do(thd)
   75 
   76     # returns a Deferred that returns a value
   77     def getMaster(self, masterid):
   78         def thd(conn):
   79             tbl = self.db.model.masters
   80             res = conn.execute(tbl.select(
   81                 whereclause=(tbl.c.id == masterid)))
   82             row = res.fetchone()
   83 
   84             rv = None
   85             if row:
   86                 rv = self._masterdictFromRow(row)
   87             res.close()
   88             return rv
   89         return self.db.pool.do(thd)
   90 
   91     # returns a Deferred that returns a value
   92     def getMasters(self):
   93         def thd(conn):
   94             tbl = self.db.model.masters
   95             return [
   96                 self._masterdictFromRow(row)
   97                 for row in conn.execute(tbl.select()).fetchall()]
   98         return self.db.pool.do(thd)
   99 
  100     # returns a Deferred that returns None
  101     def setAllMastersActiveLongTimeAgo(self):
  102         def thd(conn):
  103             tbl = self.db.model.masters
  104             q = tbl.update().values(active=1, last_active=0)
  105             conn.execute(q)
  106         return self.db.pool.do(thd)
  107 
  108     def _masterdictFromRow(self, row):
  109         return MasterDict(id=row.id, name=row.name,
  110                           active=bool(row.active),
  111                           last_active=epoch2datetime(row.last_active))