async def process(self, req: Request[ControlPage.Arguments], user: User ) -> None: # pylint: disable=attribute-defined-outside-init # Parse posted XML request. rawReq = req.rawInput() request = cast(TaskRunnerData, parse(RequestFactory(), rawReq)) # Sync Task Runner database. assert isinstance(user, TokenUser), user try: taskRunner = self.resourceDB.runnerFromToken(user) except KeyError as ex: raise InvalidRequest(*ex.args) from ex self.taskRunner = taskRunner self.abort = taskRunner.sync(self.jobDB, request) # Try to assign a new run if the Task Runner is available. # Or exit if the Task Runner exit flag is set. self.exit = False self.newRun = None if not taskRunner.isReserved(): if taskRunner.shouldExit(): self.exit = True taskRunner.setExitFlag(False) elif not taskRunner.isSuspended(): self.newRun = assignExecutionRun(taskRunner, self.unfinishedJobs)
def testTaskRunnerToXML(databases, data): """Test toXML() functionality. Check if toXML() generates valid XML code by parsing the toXML() output while creating a new object and compare the old and new object, as well as their toXML() outputs. """ # TaskRunnerData class: data2 = parse(DataFactory(), StringIO(data.toXML().flattenXML())) assert data == data2 # TaskRunner class: resourceFactory = databases.resourceDB.factory record1 = resourceFactory.newTaskRunner('runner1', '', set()) record1.sync(databases.jobDB, data) taskRunnerFactory = TaskRunnerFactory(databases) record2 = parse(taskRunnerFactory, StringIO(record1.toXML().flattenXML())) assert record1._properties == record2._properties
def _iterLoad(self, logger: logging.Logger) -> Iterator[None]: """Generator that loads the records in this database into memory. Every iteration does a small amount of work. Errors are logged on the given logger and not propagated. """ # Sorting the keys makes it more likely that records that will be # used around the same time are close together in memory as well. keys = sorted( self._keyForFileName(fileName) for fileName in os.listdir(self.baseDir) if fileName.endswith('.xml')) yield # sorting might take a while for big DBs # Log a small number of exceptions per DB. # If there are more exceptions, it is likely the same problem # repeated again and again; no point in flooding the log file. failedRecordCount = 0 for key in keys: try: value = cast(DBRecord, parse(self.factory, self._fileNameForKey(key))) self._register(key, value) except ObsoleteRecordError: if migrationInProgress: logging.warning('Removing obsolete record: %s', key) os.remove(self._fileNameForKey(key)) else: if failedRecordCount < 3: logger.warning( 'Ignoring obsolete record "%s" from %s database', key, self.description) failedRecordCount += 1 except Exception: if failedRecordCount < 3: logger.exception( 'Failed to load record "%s" from %s database', key, self.description) failedRecordCount += 1 yield if failedRecordCount != 0: logger.error('Failed to load %d of %d records from %s database', failedRecordCount, len(keys), self.description) # Sync tag cache. tagCache = self.tagCache if tagCache is not None: yield tagCache._refreshCache() # pylint: disable=protected-access
from io import StringIO from pytest import mark import attr from softfab.resourcelib import RequestFactory, TaskRunner, TaskRunnerData from softfab.resourceview import getResourceStatus from softfab.timelib import setTime from softfab.xmlbind import parse from conftest import Databases dataRun = parse( RequestFactory(), StringIO('<request runnerVersion="2.0.0" host="factorypc">' '<run jobId="2004-05-06_12-34_567890" taskId="TC-6.1" runId="0"/>' '</request>')) dataNoRun = parse( RequestFactory(), StringIO('<request runnerVersion="2.0.0" host="factorypc">' '</request>')) @mark.parametrize('data1', (dataRun, dataNoRun)) @mark.parametrize('data2', (dataRun, dataNoRun)) def testTaskRunnerSync(databases, data1, data2): """Test syncing of the TR database.""" def check(): """Check if cache and database are in sync."""
def createTaskRunnerData(self, name, target, versionStr): return parse( RequestFactory(), StringIO('<request runnerId="' + name + '" ' 'runnerVersion="' + versionStr + '"/>'))