|
|
@ -14,7 +14,7 @@ from rq.results import Result, get_key
|
|
|
|
from rq.utils import get_version, utcnow
|
|
|
|
from rq.utils import get_version, utcnow
|
|
|
|
from rq.worker import Worker
|
|
|
|
from rq.worker import Worker
|
|
|
|
|
|
|
|
|
|
|
|
from .fixtures import say_hello
|
|
|
|
from .fixtures import say_hello, div_by_zero
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipIf(get_version(Redis()) < (5, 0, 0), 'Skip if Redis server < 5.0')
|
|
|
|
@unittest.skipIf(get_version(Redis()) < (5, 0, 0), 'Skip if Redis server < 5.0')
|
|
|
@ -123,23 +123,25 @@ class TestScheduledJobRegistry(RQTestCase):
|
|
|
|
self.assertEqual(job.result, 'Success')
|
|
|
|
self.assertEqual(job.result, 'Success')
|
|
|
|
|
|
|
|
|
|
|
|
with patch('rq.worker.Worker.supports_redis_streams', new_callable=PropertyMock) as mock:
|
|
|
|
with patch('rq.worker.Worker.supports_redis_streams', new_callable=PropertyMock) as mock:
|
|
|
|
mock.return_value = False
|
|
|
|
with patch('rq.job.Job.supports_redis_streams', new_callable=PropertyMock) as job_mock:
|
|
|
|
worker = Worker([queue])
|
|
|
|
job_mock.return_value = False
|
|
|
|
worker.register_birth()
|
|
|
|
mock.return_value = False
|
|
|
|
job = queue.enqueue(say_hello)
|
|
|
|
worker = Worker([queue])
|
|
|
|
job._result = 'Success'
|
|
|
|
worker.register_birth()
|
|
|
|
job.started_at = utcnow()
|
|
|
|
job = queue.enqueue(say_hello)
|
|
|
|
job.ended_at = job.started_at + timedelta(seconds=0.75)
|
|
|
|
job._result = 'Success'
|
|
|
|
|
|
|
|
job.started_at = utcnow()
|
|
|
|
# If `save_result_to_job` = True, result will be saved to job
|
|
|
|
job.ended_at = job.started_at + timedelta(seconds=0.75)
|
|
|
|
# hash, simulating older versions of RQ
|
|
|
|
|
|
|
|
|
|
|
|
# If `save_result_to_job` = True, result will be saved to job
|
|
|
|
worker.handle_job_success(job, queue, registry)
|
|
|
|
# hash, simulating older versions of RQ
|
|
|
|
payload = self.connection.hgetall(job.key)
|
|
|
|
|
|
|
|
self.assertTrue(b'result' in payload.keys())
|
|
|
|
worker.handle_job_success(job, queue, registry)
|
|
|
|
# Delete all new result objects so we only have result stored in job hash,
|
|
|
|
payload = self.connection.hgetall(job.key)
|
|
|
|
# this should simulate a job that was executed in an earlier RQ version
|
|
|
|
self.assertTrue(b'result' in payload.keys())
|
|
|
|
self.assertEqual(job.result, 'Success')
|
|
|
|
# Delete all new result objects so we only have result stored in job hash,
|
|
|
|
|
|
|
|
# this should simulate a job that was executed in an earlier RQ version
|
|
|
|
|
|
|
|
self.assertEqual(job.result, 'Success')
|
|
|
|
|
|
|
|
|
|
|
|
def test_job_failed_result_fallback(self):
|
|
|
|
def test_job_failed_result_fallback(self):
|
|
|
|
"""Changes to job.result failure handling should be backwards compatible."""
|
|
|
|
"""Changes to job.result failure handling should be backwards compatible."""
|
|
|
@ -164,26 +166,28 @@ class TestScheduledJobRegistry(RQTestCase):
|
|
|
|
self.assertEqual(job.exc_info, 'Error')
|
|
|
|
self.assertEqual(job.exc_info, 'Error')
|
|
|
|
|
|
|
|
|
|
|
|
with patch('rq.worker.Worker.supports_redis_streams', new_callable=PropertyMock) as mock:
|
|
|
|
with patch('rq.worker.Worker.supports_redis_streams', new_callable=PropertyMock) as mock:
|
|
|
|
mock.return_value = False
|
|
|
|
with patch('rq.job.Job.supports_redis_streams', new_callable=PropertyMock) as job_mock:
|
|
|
|
worker = Worker([queue])
|
|
|
|
job_mock.return_value = False
|
|
|
|
worker.register_birth()
|
|
|
|
mock.return_value = False
|
|
|
|
|
|
|
|
worker = Worker([queue])
|
|
|
|
job = queue.enqueue(say_hello)
|
|
|
|
worker.register_birth()
|
|
|
|
job.started_at = utcnow()
|
|
|
|
|
|
|
|
job.ended_at = job.started_at + timedelta(seconds=0.75)
|
|
|
|
job = queue.enqueue(say_hello)
|
|
|
|
|
|
|
|
job.started_at = utcnow()
|
|
|
|
# If `save_result_to_job` = True, result will be saved to job
|
|
|
|
job.ended_at = job.started_at + timedelta(seconds=0.75)
|
|
|
|
# hash, simulating older versions of RQ
|
|
|
|
|
|
|
|
|
|
|
|
# If `save_result_to_job` = True, result will be saved to job
|
|
|
|
worker.handle_job_failure(job, exc_string='Error', queue=queue,
|
|
|
|
# hash, simulating older versions of RQ
|
|
|
|
started_job_registry=registry)
|
|
|
|
|
|
|
|
payload = self.connection.hgetall(job.key)
|
|
|
|
worker.handle_job_failure(job, exc_string='Error', queue=queue,
|
|
|
|
self.assertTrue(b'exc_info' in payload.keys())
|
|
|
|
started_job_registry=registry)
|
|
|
|
# Delete all new result objects so we only have result stored in job hash,
|
|
|
|
payload = self.connection.hgetall(job.key)
|
|
|
|
# this should simulate a job that was executed in an earlier RQ version
|
|
|
|
self.assertTrue(b'exc_info' in payload.keys())
|
|
|
|
Result.delete_all(job)
|
|
|
|
# Delete all new result objects so we only have result stored in job hash,
|
|
|
|
job = Job.fetch(job.id, connection=self.connection)
|
|
|
|
# this should simulate a job that was executed in an earlier RQ version
|
|
|
|
self.assertEqual(job.exc_info, 'Error')
|
|
|
|
Result.delete_all(job)
|
|
|
|
|
|
|
|
job = Job.fetch(job.id, connection=self.connection)
|
|
|
|
|
|
|
|
self.assertEqual(job.exc_info, 'Error')
|
|
|
|
|
|
|
|
|
|
|
|
def test_job_return_value(self):
|
|
|
|
def test_job_return_value(self):
|
|
|
|
"""Test job.return_value"""
|
|
|
|
"""Test job.return_value"""
|
|
|
@ -199,3 +203,14 @@ class TestScheduledJobRegistry(RQTestCase):
|
|
|
|
# Returns None if latest result is a failure
|
|
|
|
# Returns None if latest result is a failure
|
|
|
|
Result.create_failure(job, ttl=10, exc_string='exception')
|
|
|
|
Result.create_failure(job, ttl=10, exc_string='exception')
|
|
|
|
self.assertIsNone(job.return_value(refresh=True))
|
|
|
|
self.assertIsNone(job.return_value(refresh=True))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_job_return_value_sync(self):
|
|
|
|
|
|
|
|
"""Test job.return_value when queue.is_async=False"""
|
|
|
|
|
|
|
|
queue = Queue(connection=self.connection, is_async=False)
|
|
|
|
|
|
|
|
job = queue.enqueue(say_hello)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Returns None when there's no result
|
|
|
|
|
|
|
|
self.assertIsNotNone(job.return_value())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
job = queue.enqueue(div_by_zero)
|
|
|
|
|
|
|
|
self.assertEqual(job.latest_result().type, Result.Type.FAILED)
|
|
|
|