0

I try to use this solution but with no success. How can I specify a day of month at which task should be executed? My solution was

class DayOfMonth(schedule):

    def __init__(self, day=1):
        self.day = day

    def is_due(self, last_run_at):
        now = datetime.datetime.now()
        if now.month != last_run_at.month and now.day == self.day:
            return True, 3000
        return False, 3000

    def __eq__(self, other):
            if isinstance(other, DayOfMonth):
                return self.day == other.day and self.month == other.month
            return False

I try tu run it with django-celery, but I still receive error that run_every is not specified.

EDIT 1:

I run my task adding:

"my_task": {
        "task": "util.tasks.CeleryManagementCommand",
        "schedule": DayOfMonth(day=4),
        "args": ('my_task',),
    },

to CELERYBEAT_SHEDULE dict

EDIT 2:

When I specify run_every in init -> self.run_every = None I receive an error that None type object has no attribute total_seconds

Community
  • 1
  • 1
szaman
  • 6,666
  • 13
  • 53
  • 81

2 Answers2

1

If you subclass, and change the init you'd better make sure to call the parent's init. I'm pretty sure this will fix your problem:

class DayOfMonth(schedule):

    def __init__(self, day=1, *args, **kwargs):
        super(DayOfMonth, self).__init__(*args, **kwargs)
        self.day = day

If you check this, it will shed a light on the errors you've encountered: https://github.com/ask/celery/blob/master/celery/schedules.py#L33

Botond Béres
  • 16,057
  • 2
  • 37
  • 50
1

Best for you to include this in your celery.py file:

"schedule": corntab(day_of_month=4),

Also import the corntab:

from celery.schedules import crontab