def execute(job, **kwargs): """ Enqueue and immediately execute a job in foreground. This method is used in development:: execute(DummyJob, sleep=15) :param job: qual_name or job class :param kwargs: job arguments :return: final MongoDB job document from ``sys.queue`` """ if isinstance(job, str): name = job else: name = job.qual_name() setup = core4.service.setup.CoreSetup() setup.make_all() core4.logger.logon() queue = core4.queue.main.CoreQueue() job = queue.enqueue(name=name, **kwargs) job.manual = True worker = DirectWorker(name="manual") worker.at = core4.util.node.mongo_now() worker.start_job(job.serialise(), run_async=False) doc = worker.queue.job_detail(job._id) worker.config.sys.queue.delete_one(filter={"_id": job._id}) worker.config.sys.journal.delete_one(filter={"_id": job._id}) return doc
def test_worker_has_resources(queue): job = queue.enqueue(core4.queue.helper.job.example.DummyJob) worker = WorkerNoCPU(name="testRes") worker.at = core4.util.node.now() worker.start_job(job.serialise()) while queue.config.sys.queue.count_documents({}) > 0: time.sleep(1) data = list(queue.config.sys.log.find()) assert sum([1 for d in data if "start execution" in d["message"]]) == 1
def test_start_job(): queue = core4.queue.main.CoreQueue() worker = core4.queue.worker.CoreWorker() worker.cleanup() job = queue.enqueue(core4.queue.helper.job.example.DummyJob) assert job.identifier == job._id assert job._id is not None assert job.wall_time is None worker.at = core4.util.node.now() worker.start_job(job.serialise()) while queue.config.sys.queue.count_documents({}) > 0: time.sleep(0.5) assert queue.config.sys.queue.count_documents({}) == 0 assert queue.config.sys.journal.count_documents({}) == 1 job = queue.find_job(job._id) assert job.state == "complete" print(job.started_at) print(job.finished_at) print(job.finished_at - job.started_at) print(job.runtime)