1

I know that that there are question about "how to make periodical task in Django" like this post. But I'm wondering which would be best option, to use one from the options mentioned in that post (Celerity or cron mostly) or dealing the periodical task from the Javascript/jQuery.

What I want to accomplish is to simply execute a task every day at a certain time. To call a function from the "view.py". That function will connect to an URL, download a file and search in that file for information and then store it in the database. After that, refresh the homepage with the new information.

Which would be the pros and cons?

2 Answers2

1

Running a periodic task from javascript is certainly not an option because it will need your frontend side to call your views and run task.

My suggestion would be to use Celery for running periodic task as it is simple to setup and in your case it will require just a couple lines of code to run task.

Below is an example of how to run a periodic task:

from celery.task.schedules import crontab
from celery.decorators import periodic_task

@periodic_task(run_every=crontab(minute=0, hour=0)  )
def every_monday_morning():
    print("Execute every day at midnight")

Refs: docs

franga2000
  • 587
  • 1
  • 9
  • 19
Arpit Solanki
  • 9,567
  • 3
  • 41
  • 57
  • Thank for your early response, I'm abaout to test it. From the different python script in a django project (view.py, urls.py, models.py) I guess the one in which I'll need to add this code will be the "views.py" but I'm not sure. If you could confirm please. – Santiago Palomares Gallego Aug 03 '17 at 08:27
  • I've noticed that the "Refs" you refer to is an old version, the updated one will be the [following](http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html), am I wrong? – Santiago Palomares Gallego Aug 03 '17 at 08:30
  • Your link is right. Apologies for the old link. Whatever code you want to run every day just add it to the function mentioned above and then start the celery worker. – Arpit Solanki Aug 03 '17 at 08:31
0

I fail to see how client-side javascript could be of any help here (and if it's running server-side why use javascript at all ?)

If you have other uses for celery in your project then using it to run scheduled tasks makes sense, and the 'pro' is that it doesn't require any manual crontab setup at deployment.

Now if it's only to run this single operation having to install and setup (and run !) the whole celery stack (celery + rabbitmq + redis) might be just overkill compared with a plain cron job (or whatever scheduler exists for your system).

Also, if the task to execute isn't related to the HTTP request/response cycle it has nothing to do in your views. By default django-admin startapp creates a package stub with views.py, models.py and urls.py modules but you can add as many other arbitrary modules as you want. For scripts that are to be runned from the command-line (which is the case for cronjobs) the idiomatic solution is to write them as custom management commands.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118