如何检查python-rq中是否存在类似的预定作业?
How to check if a similar scheduled job exists in python-rq?
下面是在服务器启动时安排作业调用的函数。
但不知何故,计划的作业被一次又一次地调用,这导致对相应函数的调用过多。
这是由于多个函数调用还是其他原因造成的?请提出建议。
def redis_schedule():
with current_app.app_context():
redis_url = current_app.config["REDIS_URL"]
with Connection(redis.from_url(redis_url)):
q = Queue("notification")
from ..tasks.notification import send_notifs
task = q.enqueue_in(timedelta(minutes=5), send_notifs)
参考 - https://python-rq.org/docs/job_registries/
需要阅读 scheduled_job_registry
并检索 jobids
。
目前以下逻辑对我有用,因为我只有一个 scheduled_job
。
但是如果有多个作业,我将需要循环这些 jobids
来找到正确的 job
是否存在。
def redis_schedule():
with current_app.app_context():
redis_url = current_app.config["REDIS_URL"]
with Connection(redis.from_url(redis_url)):
q = Queue("notification")
if len(q.scheduled_job_registry.get_job_ids()) == 0:
from ..tasks.notification import send_notifs
task = q.enqueue_in(timedelta(seconds=30), send_notifs)
下面是在服务器启动时安排作业调用的函数。
但不知何故,计划的作业被一次又一次地调用,这导致对相应函数的调用过多。
这是由于多个函数调用还是其他原因造成的?请提出建议。
def redis_schedule():
with current_app.app_context():
redis_url = current_app.config["REDIS_URL"]
with Connection(redis.from_url(redis_url)):
q = Queue("notification")
from ..tasks.notification import send_notifs
task = q.enqueue_in(timedelta(minutes=5), send_notifs)
参考 - https://python-rq.org/docs/job_registries/
需要阅读 scheduled_job_registry
并检索 jobids
。
目前以下逻辑对我有用,因为我只有一个 scheduled_job
。
但是如果有多个作业,我将需要循环这些 jobids
来找到正确的 job
是否存在。
def redis_schedule():
with current_app.app_context():
redis_url = current_app.config["REDIS_URL"]
with Connection(redis.from_url(redis_url)):
q = Queue("notification")
if len(q.scheduled_job_registry.get_job_ids()) == 0:
from ..tasks.notification import send_notifs
task = q.enqueue_in(timedelta(seconds=30), send_notifs)