rq / rq-scheduler

A lightweight library that adds job scheduling capabilities to RQ (Redis Queue)
MIT License
1.45k stars 229 forks source link

[question] How do I get a job id ? #237

Closed Geoffrey42 closed 4 years ago

Geoffrey42 commented 4 years ago

Hi,

My newbie problem might be very simple but I can't wrap my mind around it.

How can I extract some job id ?

I am trying doing it using rq queue.job_ids without much success. Is there a way to do it directly using rq-scheduler API. I know I am probably doing it wrong the whole time.

import rq
from redis import Redis
from rq_scheduler import Scheduler 

def say_hello():
    print("hello !")

q = rq.Queue("some_queue", connection=Redis())
scheduler = Scheduler(queue_name=q, connection=Redis())

scheduler.cron(
    cron_string="*/5 * * * *",
    func=say_hello,
    queue_name="some_queue"
)

job_instances = scheduler.get_jobs(with_times=True)

for instance in job_instances:
   print(instance[0])

print("len(q): " + str(len(q)))
print("queued job ids: " + str(q.job_ids))

Outputs:

<Job fbf86931-c04e-4bf3-8def-9147a3131e1c: say_hello>
len(q): 0
queued job ids: []
selwin commented 4 years ago

Scheduled jobs are only contained in the scheduler and are not yet put in the queue. When the scheduled time comes, jobs will be moved from the scheduler to the queue.

So one job should be in either scheduler or queue, not both at the same time.