def __init__(self, db, autocommit=False, autoflush=True, **options): SignallingSession.__init__(self, db, autocommit=autocommit, autoflush=autoflush, **options) self.default_key = db.default_key self.master_keys = db.master_keys if len( db.master_keys) else self.default_key self.slave_keys = db.slave_keys if len( db.slave_keys) else self.default_key self.bind_key = None
def __init__(self, db, bind_name=None, autocommit=False, autoflush=True, **options): self._name = bind_name SignallingSession.__init__(self, db, autocommit=autocommit, autoflush=autoflush, **options)
def get_bind(self, mapper, clause=None): binding_key = self.__find_binding_key_from_context(mapper) if binding_key is None: return BaseSignallingSession.get_bind(self, mapper, clause) else: state = get_state(self.app) return state.db.get_engine(self.app, bind=binding_key)
def executeTask(task, imageName, parentCgroup): """ Given a benchmarking task, run it in given container. Updates "updatedAt" field on the model and stores benchmarking results to the model. """ dbSession = SignallingSession.object_session(task) with TemporaryDirectory() as d, parentCgroup.newGroup(f"task{task.id}") as cgroup: logging.info(f"Starting container for task {task.id}") # Create a separate cgroup in case OOM killer starts working with cgroup.newGroup("benchmark", controllers=[]) as containerCgroup: env = task.suite.env container = None try: container = podman.createContainer( image=imageName, command=shlex.split(task.command), mounts=[{ "target": "/artefact", "source": d }], cpuLimit=env.cpuLimit, memLimit=env.memoryLimit, cgroup=containerCgroup, name=createContainerName(task)) logging.debug(f"Container created for task {task.id}") def notify(): task.poke(podman.containerLogs(container)) dbSession.commit() stats = podman.runAndWatch( container, containerCgroup, cgroup, notify, env.wallClockTimeLimit, env.cpuTimeLimit) except podman.PodmanError as e: logging.error(f"Cannot execute task {task.id}: {e.log} \n\nCommand: {e}") raise TaskRunError(f"Cannot execute task: {e.log} \n\nCommand: {e}") finally: if container is not None: podman.removeContainer(container) exitcode = stats["exitCode"] dbStats = { "cpuTime": stats["cpuStat"]["usage_usec"], "wallTime": stats["wallTime"], "userTime": stats["cpuStat"]["user_usec"], "systemTime": stats["cpuStat"]["system_usec"], "outOfMemory": stats["outOfMemory"], "timeout": stats["timeout"], "memStat": stats["memStat"], "memUsage": stats["maxMemory"], "artefactError": None } try: artefact = extractArtefact(d) except ArtefactError as e: dbStats["artefactError"] = str(e) artefact = None task.finish(exitcode, stats["output"], dbStats, artefact) dbSession.commit()
def obtainEnvironment(task, envManager): """ Return an image name for running given task. If needed, build one. """ dbSession = SignallingSession.object_session(task) buildOutput = None def updateOutput(x): nonlocal buildOutput if buildOutput is not None: buildOutput += x else: buildOutput = x envImageF = envManager.getImage(task.suite.env, updateOutput) while not envImageF.done(): try: return envImageF.result(timeout=5) except TimeoutError: task.buildPoke(buildOutput) dbSession.commit() task.buildPoke(buildOutput) dbSession.commit() return envImageF.result()
def create_session(self, options): options['autoflush'] = False return SignallingSession(self, **options)
def __init__(self, *args, **kwargs): BaseSignallingSession.__init__(self, *args, **kwargs) self._binding_keys = [] self._binding_key = None