async def test_run_job_pass_context(app): result = [] @app.task(queue="yay", name="job", pass_context=True) def task_func(test_context, a): result.extend([test_context, a]) job = jobs.Job( id=16, task_kwargs={"a": 1}, lock="sherlock", queueing_lock="houba", task_name="job", queue="yay", ) test_worker = worker.Worker(app, queues=["yay"], name="my_worker") context = job_context.JobContext( worker_name="my_worker", worker_id=3, worker_queues=["yay"], job=job, task=task_func, ) test_worker.current_contexts[3] = context await test_worker.run_job(job=job, worker_id=3) assert result == [ context, 1, ]
def test_context_for_worker_kwargs(app): test_worker = worker.Worker(app=app, name="foo") expected = job_context.JobContext(app=app, worker_id=3, worker_name="bar") context = test_worker.context_for_worker(worker_id=3, worker_name="bar") assert context == expected
def __init__( self, app: app.App, queues: Optional[Iterable[str]] = None, name: Optional[str] = None, concurrency: int = 1, wait: bool = True, timeout: float = WORKER_TIMEOUT, listen_notify: bool = True, ): self.app = app self.queues = queues self.worker_name: str = name or WORKER_NAME self.concurrency = concurrency self.timeout = timeout self.wait = wait self.listen_notify = listen_notify # Handling the info about the currently running task. self.known_missing_tasks: Set[str] = set() self.job_store = self.app.job_store if name: self.logger = logger.getChild(name) else: self.logger = logger self.base_context: job_context.JobContext = job_context.JobContext( app=app, worker_name=self.worker_name, worker_queues=self.queues) self.current_contexts: Dict[int, job_context.JobContext] = {} self.stop_requested = False self.notify_event: Optional[asyncio.Event] = None
def test_job_description_job_time(job_factory): job = job_factory(task_name="some_task", id=12, task_kwargs={"a": "b"}) descr = job_context.JobContext( worker_name="a", worker_id=2, job=job, job_result=job_context.JobResult(start_timestamp=20.0), ).job_description(current_timestamp=30.0) assert descr == "worker 2: some_task[12](a='b') (started 10.000 s ago)"
def test_log_extra_job(job_factory): job = job_factory() context = job_context.JobContext(worker_name="a", worker_id=2, job=job) assert context.log_extra(action="foo") == { "action": "foo", "job": job.log_context(), "worker": {"name": "a", "id": 2, "queues": None}, }
def test_log_extra(): context = job_context.JobContext( worker_name="a", worker_id=2, additional_context={"ha": "ho"} ) assert context.log_extra(action="foo", bar="baz") == { "action": "foo", "bar": "baz", "worker": {"name": "a", "id": 2, "queues": None}, }
async def test_remove_old_jobs(app): await builtin_tasks.remove_old_jobs( job_context.JobContext(app=app), max_hours=2, queue="queue_a", remove_error=True ) assert app.connector.queries == [ ( "delete_old_jobs", {"nb_hours": 2, "queue": "queue_a", "statuses": ("succeeded", "failed")}, ) ]
def __init__( self, app: "app.App", queues: Optional[Iterable[str]] = None, name: Optional[str] = None, concurrency: int = WORKER_CONCURRENCY, wait: bool = True, timeout: float = WORKER_TIMEOUT, listen_notify: bool = True, delete_jobs: str = DeleteJobCondition.NEVER.value, additional_context: Optional[Dict[str, Any]] = None, ): self.app = app self.queues = queues self.worker_name: str = name or WORKER_NAME self.concurrency = concurrency self.timeout = timeout self.wait = wait self.listen_notify = listen_notify self.delete_jobs = DeleteJobCondition(delete_jobs) self.job_manager = self.app.job_manager if name: self.logger = logger.getChild(name) else: self.logger = logger # Handling the info about the currently running task. self.base_context: job_context.JobContext = job_context.JobContext( app=app, worker_name=self.worker_name, worker_queues=self.queues, additional_context=additional_context.copy() if additional_context else {}, ) self.current_contexts: Dict[int, job_context.JobContext] = {} self.stop_requested = False self.notify_event: Optional[asyncio.Event] = None
def context(): return job_context.JobContext()
def _(job): return job_context.JobContext(app=app, worker_name="worker", job=job)
def test_queues_display(queues, result): context = job_context.JobContext(worker_queues=queues) assert context.queues_display == result
def test_job_description_job_no_time(job_factory): job = job_factory(task_name="some_task", id=12, task_kwargs={"a": "b"}) descr = job_context.JobContext( worker_name="a", worker_id=2, job=job).job_description(current_timestamp=0) assert descr == "worker 2: some_task[12](a='b')"
def test_job_description_no_job(job_factory): descr = job_context.JobContext( worker_name="a", worker_id=2).job_description(current_timestamp=0) assert descr == "worker 2: no current job"
def test_evolve(): context = job_context.JobContext(worker_name="a") assert context.evolve(worker_name="b").worker_name == "b"