mirror of https://github.com/peter4431/rq.git
commit
7686785d61
@ -0,0 +1,58 @@
|
||||
from .compat import as_text
|
||||
from .connections import resolve_connection
|
||||
from .queue import FailedQueue
|
||||
from .utils import current_timestamp
|
||||
|
||||
|
||||
class StartedJobRegistry:
|
||||
"""
|
||||
Registry of currently executing jobs. Each queue maintains a StartedJobRegistry.
|
||||
StartedJobRegistry contains job keys that are currently being executed.
|
||||
Each key is scored by job's expiration time (datetime started + timeout).
|
||||
|
||||
Jobs are added to registry right before they are executed and removed
|
||||
right after completion (success or failure).
|
||||
|
||||
Jobs whose score are lower than current time is considered "expired".
|
||||
"""
|
||||
|
||||
def __init__(self, name='default', connection=None):
|
||||
self.name = name
|
||||
self.key = 'rq:wip:%s' % name
|
||||
self.connection = resolve_connection(connection)
|
||||
|
||||
def add(self, job, timeout, pipeline=None):
|
||||
"""Adds a job to StartedJobRegistry with expiry time of now + timeout."""
|
||||
score = current_timestamp() + timeout
|
||||
if pipeline is not None:
|
||||
return pipeline.zadd(self.key, score, job.id)
|
||||
|
||||
return self.connection._zadd(self.key, score, job.id)
|
||||
|
||||
def remove(self, job, pipeline=None):
|
||||
connection = pipeline if pipeline is not None else self.connection
|
||||
return connection.zrem(self.key, job.id)
|
||||
|
||||
def get_expired_job_ids(self):
|
||||
"""Returns job ids whose score are less than current timestamp."""
|
||||
return [as_text(job_id) for job_id in
|
||||
self.connection.zrangebyscore(self.key, 0, current_timestamp())]
|
||||
|
||||
def get_job_ids(self, start=0, end=-1):
|
||||
"""Returns list of all job ids."""
|
||||
self.move_expired_jobs_to_failed_queue()
|
||||
return [as_text(job_id) for job_id in
|
||||
self.connection.zrange(self.key, start, end)]
|
||||
|
||||
def move_expired_jobs_to_failed_queue(self):
|
||||
"""Remove expired jobs from registry and add them to FailedQueue."""
|
||||
job_ids = self.get_expired_job_ids()
|
||||
|
||||
if job_ids:
|
||||
failed_queue = FailedQueue(connection=self.connection)
|
||||
with self.connection.pipeline() as pipeline:
|
||||
for job_id in job_ids:
|
||||
failed_queue.push_job_id(job_id, pipeline=pipeline)
|
||||
pipeline.execute()
|
||||
|
||||
return job_ids
|
@ -0,0 +1,78 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import
|
||||
|
||||
from rq.job import Job
|
||||
from rq.queue import FailedQueue, Queue
|
||||
from rq.utils import current_timestamp
|
||||
from rq.worker import Worker
|
||||
from rq.registry import StartedJobRegistry
|
||||
|
||||
from tests import RQTestCase
|
||||
from tests.fixtures import div_by_zero, say_hello
|
||||
|
||||
|
||||
class TestQueue(RQTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestQueue, self).setUp()
|
||||
self.registry = StartedJobRegistry(connection=self.testconn)
|
||||
|
||||
def test_add_and_remove(self):
|
||||
"""Adding and removing job to StartedJobRegistry."""
|
||||
timestamp = current_timestamp()
|
||||
job = Job()
|
||||
|
||||
# Test that job is added with the right score
|
||||
self.registry.add(job, 1000)
|
||||
self.assertLess(self.testconn.zscore(self.registry.key, job.id),
|
||||
timestamp + 1002)
|
||||
|
||||
# Ensure that job is properly removed from sorted set
|
||||
self.registry.remove(job)
|
||||
self.assertIsNone(self.testconn.zscore(self.registry.key, job.id))
|
||||
|
||||
def test_get_job_ids(self):
|
||||
"""Getting job ids from StartedJobRegistry."""
|
||||
self.testconn.zadd(self.registry.key, 1, 'foo')
|
||||
self.testconn.zadd(self.registry.key, 10, 'bar')
|
||||
self.assertEqual(self.registry.get_job_ids(), ['foo', 'bar'])
|
||||
|
||||
def test_get_expired_job_ids(self):
|
||||
"""Getting expired job ids form StartedJobRegistry."""
|
||||
timestamp = current_timestamp()
|
||||
|
||||
self.testconn.zadd(self.registry.key, 1, 'foo')
|
||||
self.testconn.zadd(self.registry.key, timestamp + 10, 'bar')
|
||||
|
||||
self.assertEqual(self.registry.get_expired_job_ids(), ['foo'])
|
||||
|
||||
def test_cleanup(self):
|
||||
"""Moving expired jobs to FailedQueue."""
|
||||
failed_queue = FailedQueue(connection=self.testconn)
|
||||
self.assertTrue(failed_queue.is_empty())
|
||||
self.testconn.zadd(self.registry.key, 1, 'foo')
|
||||
self.registry.move_expired_jobs_to_failed_queue()
|
||||
self.assertIn('foo', failed_queue.job_ids)
|
||||
|
||||
def test_job_execution(self):
|
||||
"""Job is removed from StartedJobRegistry after execution."""
|
||||
registry = StartedJobRegistry(connection=self.testconn)
|
||||
queue = Queue(connection=self.testconn)
|
||||
worker = Worker([queue])
|
||||
|
||||
job = queue.enqueue(say_hello)
|
||||
|
||||
worker.prepare_job_execution(job)
|
||||
self.assertIn(job.id, registry.get_job_ids())
|
||||
|
||||
worker.perform_job(job)
|
||||
self.assertNotIn(job.id, registry.get_job_ids())
|
||||
|
||||
# Job that fails
|
||||
job = queue.enqueue(div_by_zero)
|
||||
|
||||
worker.prepare_job_execution(job)
|
||||
self.assertIn(job.id, registry.get_job_ids())
|
||||
|
||||
worker.perform_job(job)
|
||||
self.assertNotIn(job.id, registry.get_job_ids())
|
Loading…
Reference in New Issue