mirror of https://github.com/peter4431/rq.git
Cross platform simple worker (#1629)
* Added CrossPlatformDeathPenalty that doesn't rely on signals * Updated `SimpleWorker`'s `death_penalty_class` to utilize `CrossPlatformDeathPenalty` to allow use on Windows machines * Changed `CrossPlatformDeathPenalty` to `TimerDeathPenalty` * Removed overridden `death_penalty_class` in `SimpleWorker` until feature matures * Added section in testing.md explaining how to utilize `SimpleWorker` on Windows OS * Replaced usage of chatting with .format for python 3.5 compatibility * Add tests for new timeout feature * Explicitly set defaults.CALLBACK_TIMEOUT * Implemented cross-thread method of raising errors by using ctypes * Finished writing tests for new TimerDeathPenaltymain
parent
609c068ef8
commit
f4602d30d5
@ -0,0 +1,44 @@
|
||||
import time
|
||||
|
||||
from rq import Queue, SimpleWorker
|
||||
from rq.timeouts import TimerDeathPenalty
|
||||
from rq.registry import FailedJobRegistry, FinishedJobRegistry
|
||||
from tests import RQTestCase
|
||||
|
||||
|
||||
class TimerBasedWorker(SimpleWorker):
|
||||
death_penalty_class = TimerDeathPenalty
|
||||
|
||||
|
||||
def thread_friendly_sleep_func(seconds):
|
||||
end_at = time.time() + seconds
|
||||
while True:
|
||||
if time.time() > end_at:
|
||||
break
|
||||
|
||||
|
||||
class TestTimeouts(RQTestCase):
|
||||
def test_timer_death_penalty(self):
|
||||
"""Ensure TimerDeathPenalty works correctly."""
|
||||
q = Queue(connection=self.testconn)
|
||||
q.empty()
|
||||
finished_job_registry = FinishedJobRegistry(connection=self.testconn)
|
||||
failed_job_registry = FailedJobRegistry(connection=self.testconn)
|
||||
|
||||
# make sure death_penalty_class persists
|
||||
w = TimerBasedWorker([q], connection=self.testconn)
|
||||
self.assertIsNotNone(w)
|
||||
self.assertEqual(w.death_penalty_class, TimerDeathPenalty)
|
||||
|
||||
# Test short-running job doesn't raise JobTimeoutException
|
||||
job = q.enqueue(thread_friendly_sleep_func, args=(1,), job_timeout=3)
|
||||
w.work(burst=True)
|
||||
job.refresh()
|
||||
self.assertIn(job, finished_job_registry)
|
||||
|
||||
# Test long-running job raises JobTimeoutException
|
||||
job = q.enqueue(thread_friendly_sleep_func, args=(5,), job_timeout=3)
|
||||
w.work(burst=True)
|
||||
self.assertIn(job, failed_job_registry)
|
||||
job.refresh()
|
||||
self.assertIn("rq.timeouts.JobTimeoutException", job.exc_info)
|
Loading…
Reference in New Issue