简体   繁体   English

设置芹菜周期性任务

[英]Setup celery periodic task

How do I set up a periodic task with Celerybeat and Flask that queries a database every hour? 如何设置Celerybeat和Flask的定期任务,每小时查询一次数据库?

The environment looks like this: 环境看起来像这样:

/
|-app
  |-__init__.py
  |-jobs
    |-task.py
|-celery-beat.sh
|-celery-worker.sh
|-manage.py

I currently have a query function called run_query() located in task.py 我目前在task.py有一个名为run_query()的查询函数

I want the scheduler to kick in once the application initiates so I have the following lines in my /app/__init__.py folder: 我希望调度程序在应用程序启动后启动,所以我在/app/__init__.py文件夹中有以下行:

celery = Celery()

@celery.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
  sender.add_periodic_task(1, app.jobs.task.run_query())

(For simplicity's sake, I've set it up so that if it runs, it will run every minute. No such luck yet.) (为简单起见,我已将其设置为如果它运行,它将每分钟运行。还没有运气。)

When I launch the celery-worker.sh it recognizes my function under the [tasks] heading. 当我启动celery-worker.sh它会在[tasks]标题下识别我的功能。 But the scheduled function never runs. 预定的功能永远不会运行。 I can manually force the function to run by issuing the following at the command prompt: 我可以通过在命令提示符处发出以下命令来手动强制运行该函数:

>> from app.jobs import task
>> task.run_query.delay()

EDIT : Added celerybeat.sh 编辑 :添加了celerybeat.sh

As a follow up: If the database is accessed through a flask context, during my asynch function call is it wise to create a new flask context to access the database? 作为后续:如果通过烧瓶上下文访问数据库,在我的异步函数调用期间创建一个新的烧瓶上下文来访问数据库是明智的吗? Use the existing flask context? 使用现有的烧瓶背景? Or forget contexts altogether and just initiate a connection to the database? 或者完全忘记上下文并只是启动与数据库的连接? My worry is that if I just initiate a new connection it may interfere with the existing context's connection? 我担心的是,如果我只是发起新连接,它可能会干扰现有的上下文连接?

To run periodic tasks you need some kind of schduler (Eg. celery beat). 要运行定期任务,你需要某种调度器(例如芹菜节拍)。

celery beat is a scheduler; 芹菜拍是一种调度者; It kicks off tasks at regular intervals, that are then executed by available worker nodes in the cluster. 它定期启动任务,然后由群集中的可用工作节点执行。

You have to ensure only a single scheduler is running for a schedule at a time, otherwise you'd end up with duplicate tasks. 您必须确保一次只有一个调度程序正在运行,否则您最终会遇到重复的任务。 Using a centralized approach means the schedule doesn't have to be synchronized, and the service can operate without using locks. 使用集中式方法意味着不必同步调度,并且服务可以在不使用锁的情况下运行。

Reference: periodic-tasks 参考: 周期性任务

You can invoke scheduler with command, 您可以使用命令调用scheduler,

$ celery -A proj beat #different process from your worker

You can also embed beat inside the worker by enabling the workers -B option, this is convenient if you'll never run more than one worker node, but it's not commonly used and for that reason isn't recommended for production use Starting scheduler: 您还可以通过启用workers -B选项在工作者中嵌入节拍,如果您永远不会运行多个工作节点,这很方便,但它并不常用,因此不建议用于生产用途启动调度程序:

$ celery -A proj worker -B

Reference: celery starting scheduler 参考: 芹菜启动调度程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM