Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing: how do I force the scheduler to empty out it's queue? #87

Open
jmmills opened this issue Jul 21, 2015 · 9 comments
Open

Testing: how do I force the scheduler to empty out it's queue? #87

jmmills opened this issue Jul 21, 2015 · 9 comments

Comments

@jmmills
Copy link

jmmills commented Jul 21, 2015

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?

@jmmills
Copy link
Author

jmmills commented Jul 21, 2015

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)

@jmmills jmmills closed this as completed Jul 21, 2015
@selwin
Copy link
Contributor

selwin commented Jul 21, 2015

I'm reopening this issue because I think we should have a scheduler.empty() method to do this easily.

@selwin selwin reopened this Jul 21, 2015
@mbodock
Copy link
Contributor

mbodock commented Aug 10, 2015

Hi @selwin,

what do you expect scheduler.empty() to do ?
Just clear the queue, or execute the jobs then clear ?

@selwin
Copy link
Contributor

selwin commented Aug 12, 2015

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 :)

@jmmills
Copy link
Author

jmmills commented Aug 13, 2015

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)

@selwin
Copy link
Contributor

selwin commented Aug 14, 2015

@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

@jegesh
Copy link

jegesh commented Dec 1, 2016

+1

@c-simpson
Copy link

@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)

@linkdesu
Copy link

linkdesu commented Jun 29, 2018

@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 a Queue instance later, which support queue.empty already!

So the right way to empty queue is :

queue = Queue(the_same_queue_name_your_scheduler_using)
queue.empty()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants