4

file structure

proj/proj/
         celery.py
         (and other files)
    /sitesettings/
         tasks.py
         (and other files)

celery.py

app = Celery('mooncake',broker_url = 'amqp://')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

sitesettings/tasks.py

from __future__ import absolute_import, unicode_literals
from comma.models import Post
from mooncake.celery import app

app.conf.beat_schedule = {
'every-5-seconds': {
    'task': 'sitesettings.tasks.statisticsTag',
    'schedule': 5.0,
    'args': ()
},
}

@app.task
def statisticsTag():
    print(Post.objects.all()[0])

and run it with

celery -A proj beat -l info

it out put with

[2019-02-22 18:21:08,346: INFO/MainProcess] Scheduler: Sending due task every-5-seconds (sitesettings.tasks.statisticsTag)

but no further output. I used to try write it in proj/celery.py, but it cannot run cuz I have to import from another app, it exit with "app not loaded" error. So what should I do?

Shinra tensei
  • 1,283
  • 9
  • 21
Tac
  • 355
  • 3
  • 12
  • I believe that your task is being executed, but it's not printing what you want to print. For checking it you can change the task to do smth else, like change name or add some char to charfield every 5 seconds and see if it changed. What I suggest is to install Flower service and watch your tasks there: https://flower.readthedocs.io/en/latest/ – Sergey Pugach Feb 22 '19 at 11:54
  • It is not... cuz what I acturally do is calculate recent posts and save them as a global value. but it only has initial value. – Tac Feb 22 '19 at 12:12

1 Answers1

16

The command you are calling to start celery celery -A proj beat -l info is starting a beat scheduler instance of celery which sends due tasks to a worker instance.

You will also need to start a worker server that will execute those due tasks. You can start a celery worker with the command celery -A proj worker -l info. This will need to be running at the same time as your scheduler is running.

Alternatively you can run a worker with embedded beat scheduler celery -A proj worker -B -l info but that is not recommended for production use.

grrrrrr
  • 1,395
  • 12
  • 29