rq-scheduler
rq-scheduler copied to clipboard
Testing: how do I force the scheduler to empty out it's queue?
Use case: Testing a system where objects are expired from a database at scheduled times, how does one force the scheduler to run jobs?
Is it as simple as creating an RQ SimpleWorker
attached to the "scheduled queue", or is there a better way?
I solved this myself, one uses mock
and patches the get_jobs_to_queue
method.
E.g,
def return_all_jobs(scheduler):
return scheduler.get_jobs()
@mock.patch('rq_scheduler.scheduler.Scheduler.get_jobs_to_queue', return_all_jobs)
def test_something_scheduled():
# given that scheduler is an RQ scheduler
# given that worker is a RQ SimpleWorker
# scheduler.enqueue_in(...)
scheduler.enqueue_jobs()
worker.work(burst=True)
I'm reopening this issue because I think we should have a scheduler.empty() method to do this easily.
Hi @selwin,
what do you expect scheduler.empty()
to do ?
Just clear the queue, or execute the jobs then clear ?
Running scheduler.empty()
should just empty the scheduler, while running scheduler.empty(delete_jobs=True)
should empty the scheduler and delete all jobs inside. Thanks :)
so with empty it would look like something like (for testing):
worker = SimpleWorker()
scheduler.enqueue_in(...)
# some time later to test the results
scheduler.empty()
worker.work(burst=True)
However, in my case where I came across this, I have jobs scheduling new jobs... so this would be more like what I was doing:
while scheduler.empty(): # should return jobs that are now queued
worker.work(burst=True)
@jmmills there seems to be a miscommunication. What I think scheduler.empty
should do is clearing the scheduler, not enqueueing all jobs in the scheduler. So it should be something like this:
scheduler.enqueue_in(...)
scheduler.count() # Returns 1
scheduler.empty()
scheduler.count() # Returns 0
+1
@selwin I know it's kind of late, but is this what you are looking for?
scheduled_jobs = scheduler.get_jobs()
for job in scheduled_jobs:
scheduler.cancel(job)
@c-simpson scheduler.cancel
just cancel scheduling job, but do not clear jobs already in queue.
BTW, I found the right way to empty queue:
- When we create scheduler instance, we have set a queue name.
-
rq
will use that queue name to create aQueue
instance later, which supportqueue.empty
already!
So the right way to empty queue is :
queue = Queue(the_same_queue_name_your_scheduler_using)
queue.empty()