1

I am creating a Django application for school project. I want to schedule jobs (every (work)day on 9:00 and 17:00). I am trying to do it with Celery right now, but I stuck very hard on it, and as the deadline is in sight, I want to use alternative options: just a cronjob. I think just the cronjob works fine, but the user should be able to edit the times of the cronjobs using the Django web application (so not logging in to SSH, edit the crontab manually).

Is this possible? Can't find anything about it on the internet.

Marvin
  • 79
  • 8

1 Answers1

1

You need django-celery-beat plugin that adds new models to the django admin named "Periodic tasks" where you can manage cron schedule for your tasks.

As an alternate, if you really do not want to run a background tasks, you can create django management commands and use a library like python-crontab to add/modify/remove cron jobs in the system.

Emin Mastizada
  • 1,375
  • 2
  • 15
  • 30
  • As told, I am stuck on Celery for too long now. Celery works fine. Untill I want to create a service of it (because it needs to run always). So I tought as alternative, I just use crontabs. But the only problem is that the user needs to be able to change the times. – Marvin Dec 18 '20 at 12:16
  • You can control the crontab using [python-crontab](https://pypi.org/project/python-crontab/) library, however, it will be easier to solve the issue with running the celery task. It is mostly the same as running the django application (using supervisor or systemd) – Emin Mastizada Dec 18 '20 at 12:24
  • @Marvin Using crontabs is possible, however the user won't be able to change the schedule. Any solution like using `python-crontab` is almost certainly going to be harder and uglier than using Celery Beat. – Antonis Christofides Dec 18 '20 at 12:26
  • I agree that Celery would work better and is cleaner. But I have a deadline of my school project, and didn't get Celery to work (using services). I've created a stackoverflow post on this issue, without any success (yet). https://stackoverflow.com/questions/65338414/running-celery-as-daemon-does-not-create-pid-file-no-permission-issue I don't have much knowledge about Django or Celery, so for me it's realy hard to explain the issue, troubleshoot it, etc. – Marvin Dec 18 '20 at 12:30
  • Don't waste time with this way of deploying celery, just copy the systemd or supervisor file that you are using for running the django and in the command section add this one: `command=/path/to/project/.venv/bin/celery -A proj worker -l WARNING` - this is using celery from your virtual environment (virtualenv), and also, replace "proj" with your django application name (name of the folder that contains urls.py, settings.py and etc. For systemd, instead of `command=` you will use `ExecStart=` – Emin Mastizada Dec 18 '20 at 12:45