12

I'd like to create a periodic task for celery using django-celery's admin interface. I have a task set up which runs great when called manually or by script. It just doesn't work through celerybeat. According to the debug logs the task is set to enabled = False on first retrieval and I wonder why.

When adding the periodic task and passing [1, False] as positional arguments, the task is automatically disabled and I don't see any further output. When added without arguments the task is executed but raises an exception instantly because I didn't supply the needed arguments (makes sense).

Does anyone see what's the problem here?

Thanks in advance.

This is the output after supplying arguments:

[DEBUG/Beat] SELECT "djcelery_periodictask"."id", [...] 
             FROM "djcelery_periodictask" 
             WHERE "djcelery_periodictask"."enabled" = true ; args=(True,)

[DEBUG/Beat] SELECT "djcelery_intervalschedule"."id", [...] 
             FROM "djcelery_intervalschedule" 
             WHERE "djcelery_intervalschedule"."id" = 3 ; args=(3,)

[DEBUG/Beat] SELECT (1) AS "a" 
             FROM "djcelery_periodictask" 
             WHERE "djcelery_periodictask"."id" = 3  LIMIT 1; args=(3,)

[DEBUG/Beat] UPDATE "djcelery_periodictask" 
             SET "name" = E'<taskname>', "task" = E'<task.module.path>', 
                 "interval_id" = 3, "crontab_id" = NULL, 
                 "args" = E'[1, False,]', "kwargs" = E'{}', "queue" = NULL, 
                 "exchange" = NULL, "routing_key" = NULL, 
                 "expires" = NULL, "enabled" = false, 
                 "last_run_at" = E'2011-05-25 00:45:23.242387', "total_run_count" = 9, 
                 "date_changed" = E'2011-05-25 09:28:06.201148' 
             WHERE "djcelery_periodictask"."id" = 3; 
             args=(
                   u'<periodic-task-name>', u'<task.module.path>', 
                   3, u'[1, False,]', u'{}', 
                   False, u'2011-05-25 00:45:23.242387', 9, 
                   u'2011-05-25 09:28:06.201148', 3
             )

[DEBUG/Beat] Current schedule:
<ModelEntry: celery.backend_cleanup celery.backend_cleanup(*[], **{}) {<crontab: 0 4 * (m/h/d)>}
[DEBUG/Beat] Celerybeat: Waking up in 5.00 seconds.

EDIT: It works with the following setting. I still have no idea why it doesn't work with django-celery.

CELERYBEAT_SCHEDULE = {
    "example": {
        "task": "<task.module.path>",
        "schedule": crontab(),
        "args": (1, False)
    },
}
Aldarund
  • 17,312
  • 5
  • 73
  • 104
jnns
  • 5,148
  • 4
  • 47
  • 74
  • Can you post your task and periodictask configuration? – Ken Cochrane May 25 '11 at 11:35
  • Thanks for seeing about my issue. I didn't set `CELERYBEAT_SCHEDULE` because I used django-celery's administration instead. I also checked the [FAQ](http://docs.celeryproject.org/en/latest/faq.html#why-won-t-my-task-run) but as I said in the original posting, I'm able to launch the task from the shell manually. Is there something else I could have overlooked? – jnns May 25 '11 at 15:38
  • Have exactly same problem ... Any solutions? – Aldarund Jun 14 '11 at 21:36
  • Have you tried setting the tasks in `settings.py` with `CELERYBEAT_SCHEDULE`? – jnns Jun 14 '11 at 21:49
  • Yep, it works fine with CELERYBEAT_SCHEDULE. But i need it to work with django admin. – Aldarund Jun 14 '11 at 22:58
  • Stupid question, You have set the var CELERYBEAT_SCHEDULER in this way ? CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler" – Mauro Rocco Jun 15 '11 at 15:29

3 Answers3

15

I had the same issue. Make sure the arguments are JSON formatted. For example, try setting the positional args to [1, false] -- lowercase 'false' -- I just tested it on a django-celery instance (version 2.2.4) and it worked.

For the keyword args, use something like {"name": "aldarund"}

Evan
  • 236
  • 1
  • 2
  • 1
    Yes, I'm confirm, the problem is the encoding. In fact for string arguments you have to use double quotes " instead of the single one '. – Mauro Rocco Jul 11 '11 at 09:48
9

I got the same problem too.

With the description of PeriodicTask models in djcelery ("JSON encoded positional arguments"), same as Evan answer. I try using python json lib to encode before save.

And this work with me

import json 
o = PeriodicTask()
o.kwargs = json.dumps({'myargs': 'hello'})
o.save()

celery version 3.0.11

Hardy
  • 1,499
  • 2
  • 26
  • 39
0
CELERYBEAT_SCHEDULE = {
    "example": {
        "task": "<task.module.path>",
        "schedule": crontab(),
        "enable": False
    },
}

I tried and it worked.I run on celery beat v5.1.2

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 08 '21 at 05:47