I have a django-project with an app called app that has a file called cron.py
with a function called my_job().
I want the my_job()
function to be called every minute.
In my django-project/django-project/settings.py
I have this:
INSTALLED_APPS = [
'django_crontab',
...
]
...
CRONJOBS = [
('*/5 * * * *', 'app.cron.my_job')
]
My django-project/app/cron.py
looks like this:
from .models import TestModel
def my_job():
TestModel.objects.create(number=100)
return True
Of course I ran : python3 manage.py crontab add
And the terminal printed:
adding cronjob: (62dd7536ac2c985925ee33743a070a4c) -> ('*/5 * * * *', 'app.cron.my_job')
To be safe I run: python3 manage.py crontab show
And the terminal prints:
Project/ccc_nema -> ('*/5 * * * *', 'app.cron.my_job')
To check if evrything works I run: python3 manage.py crontab run someHash
Traceback (most recent call last):
File "/home/anik/Workplace/Office Project/ccc_nema v2/core/manage.py", line 22, in <module>
main()
File "/home/anik/Workplace/Office Project/ccc_nema v2/core/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/anik/Workplace/Office Project/ccc_nema v2/env_python/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/home/anik/Workplace/Office Project/ccc_nema v2/env_python/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/anik/Workplace/Office Project/ccc_nema v2/env_python/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/anik/Workplace/Office Project/ccc_nema v2/env_python/lib/python3.10/site-packages/django/core/management/base.py", line 448, in execute
output = self.handle(*args, **options)
File "/home/anik/Workplace/Office Project/ccc_nema v2/env_python/lib/python3.10/site-packages/django_crontab/management/commands/crontab.py", line 29, in handle
Crontab().run_job(options['jobhash'])
File "/home/anik/Workplace/Office Project/ccc_nema v2/env_python/lib/python3.10/site-packages/django_crontab/crontab.py", line 126, in run_job
job = self.__get_job_by_hash(job_hash)
File "/home/anik/Workplace/Office Project/ccc_nema v2/env_python/lib/python3.10/site-packages/django_crontab/crontab.py", line 171, in __get_job_by_hash
raise RuntimeError(
RuntimeError: No job with hash someHash found. It seems the crontab is out of sync with your settings.CRONJOBS. Run "python manage.py crontab add" again to resolve this issue!
The main issue: No matter how long I wait the job will not be executed automatically.
- What am I doing wrong?