Reputation: 164
I'm using Django and Celery for scheduling tasks.
tasks.py
@periodic_task(run_every=crontab(hour='00', minute='00', day_of_week="*"))
def hello_world():
print('hello world!)
In addition to running the function daily, I need to be able to invoke it manually. If a user presses a button in the front-end it will run. I could do:
@periodic_task(run_every=crontab(hour='00', minute='00', day_of_week="*"))
def hello_world():
print('hello world!)
@task()
def hello_world():
print('hello world!)
But that goes against DRY. Also, I may have more than one function that will have this same scenario. I will need to run it periodically, but also upon request.
I tried this:
def hello_world():
print('hello world!)
@periodic_task(run_every=crontab(hour='00', minute='00', day_of_week="*"))
hello_world()
@task
hello_world()
But that does not work. I get
invalid syntax
I haven't been able to find this scenario in the documentation or other stackoverflow answers. They talk about invoking the function from the shell. Help would be appreciated.
Thanks!
Upvotes: 0
Views: 93
Reputation: 4140
You don't need to define the task twice, your periodic task can also be invoked manually with hello_world.delay()
, so you would have:
@periodic_task(run_every=crontab(hour='00', minute='00', day_of_week='*'))
def hello_world():
print('hello world!')
def on_button_press():
hello_world.delay()
Upvotes: 2