7

im having trouble passing arguments to my functions via celerybeat schedule. After searching it looks as though I should be able to pass them with the args command but im getting errors as per the below. can anyone point me in the right direction?

CELERYBEAT_SCHEDULE = {
    'maintenance_mail_1_day': {
        'task': 'home.tasks.maintenance_mail',
        'schedule': crontab(hour='15'),
        'args' : (1),
    },
    'maintenance_mail_3_day': {
        'task': 'home.tasks.maintenance_mail',
        'schedule': crontab(hour='15'),
        'args' : (3),
    },    
    'maintenance_mail_5_day': {
        'task': 'home.tasks.maintenance_mail',
        'schedule': crontab(hour='15'),
        'args' : (5),
    },
    'maintenance_mail_7_day': {
        'task': 'home.tasks.maintenance_mail',
        'schedule': crontab(hour='15'),
        'args' : (7),
    }

tasks,py

@app.task
def maintenance_mail(days):
    return send_maintnance_emails(days)
AlexW
  • 2,843
  • 12
  • 74
  • 156

2 Answers2

11

The following holds in Python: (1) == 1

In order to make it a singleton tuple, add an extra comma: (1,) and in your settings accordingly:

# ...
'args' : (1,),
# ...
user2390182
  • 72,016
  • 6
  • 67
  • 89
0

You can specify the arguments and keyword arguments used to execute the task as follows. Note how JSON serialization is required.

import json

CELERYBEAT_SCHEDULE = {
    'maintenance_mail_1_day': {
        'task': 'home.tasks.maintenance_mail',
        'schedule': crontab(hour='15'),
        'args' : json.dumps([1]),
    }
}

OR

import json

CELERYBEAT_SCHEDULE = {
    'maintenance_mail_1_day': {
        'task': 'home.tasks.maintenance_mail',
        'schedule': crontab(hour='15'),
        'kwargs' : json.dumps({
            'days': 1,
        }),
    }
}

More info here: django-celery-beat documentation

Ivan
  • 61
  • 7
  • The docs you link to are for `django-celery-beat` talking about adding rows into the table. The config that OP mentioned requires `'args'` to be a tuple or a list and `'kwargs'` to be a dict: https://docs.celeryq.dev/en/stable/userguide/periodic-tasks.html#available-fields – Tim Tisdall Oct 11 '22 at 18:39