def test_running_job(broker): running_jobs_key = broker._to_namespaced( RUNNING_JOBS_KEY.format(broker._id)) # Non-idempotent job job = Job('foo_task', 'foo_queue', datetime.now(timezone.utc), 0) broker.enqueue_jobs([job]) assert broker._r.hget(running_jobs_key, str(job.id)) is None broker.get_jobs_from_queue('foo_queue', 1) assert broker._r.hget(running_jobs_key, str(job.id)) is None # Try to remove it, even if it doesn't exist in running broker.remove_job_from_running(job) # Idempotent job - get from queue job = Job('foo_task', 'foo_queue', datetime.now(timezone.utc), 10) broker.enqueue_jobs([job]) assert broker._r.hget(running_jobs_key, str(job.id)) is None broker.get_jobs_from_queue('foo_queue', 1) job.status = JobStatus.RUNNING assert (Job.deserialize( broker._r.hget(running_jobs_key, str(job.id)).decode()) == job) # Idempotent job - re-enqueue after job ran with error job.retries += 1 broker.enqueue_jobs([job]) assert broker._r.hget(running_jobs_key, str(job.id)) is None broker.get_jobs_from_queue('foo_queue', 1) job.status = JobStatus.RUNNING assert (Job.deserialize( broker._r.hget(running_jobs_key, str(job.id)).decode()) == job) # Idempotent job - job succeeded broker.remove_job_from_running(job) assert broker._r.hget(running_jobs_key, str(job.id)) is None assert broker.get_jobs_from_queue('foo_queue', 1) == []
def test_get_jobs_from_queue_re_adds_jobs_if_over_limit(broker): task = Task(print, 'foo', 'q1', 10, None, max_concurrency=1) broker.set_concurrency_keys([task]) job1 = Job('foo', 'q1', datetime.now(timezone.utc), 10) job2 = Job('foo', 'q1', datetime.now(timezone.utc), 10) broker.enqueue_jobs([job1, job2]) # Try to get one more than it allows. [running_job] = broker.get_jobs_from_queue('q1', 2) # Pop what's left in the broker's Queue and inspect it. job_json_string = broker._get_queue('q1').get(block=False) queued_job = Job.deserialize(job_json_string) assert queued_job != running_job
def test_serialization(job): job.status = JobStatus.QUEUED job.retries = 2 job_json = job.serialize() assert Job.deserialize(job_json) == job