Django user team scheduling

I'm having difficulty running my custom team on a schedule. I tried cronjob and django-chronograph , but I can't get it to work as it can (successfully) from the command line.

I am just developing the application locally using django installed on Ubunutu.

I have a user command that I use to clear log entries that are longer than 30 days. To retest it, I changed it so that it only deletes one arbitrary entry:

from datetime import datetime, timedelta from hitcount.models import Hit from django.core.management.base import BaseCommand, CommandError class Command(BaseCommand): args = '<days>' help = 'Clear out all Hits that occured over "days" days ago' def handle(self, *args, **options): list = Hit.objects.filter(created__lt = datetime.now()-timedelta(days=2) ) list[0].delete() self.stdout.write('Hit deleted - %s' % list[0]) 

This command (del_hit.py) is in the following structure:

 /project_dir /app /management __init__.py /commands __init__.py del_hit.py 

As I said, I can successfully run this command from my project_dir

 python manage.py del_hit 

I tried installing django-chronograph . This seems very useful. He recognized my team and allowed me to select it from the drop-down list. I applied cron as indicated:

          • /home/vadmin/development/python/my_proj/manage.py cron

However, when he tries to execute the command, he gives me the following error in the log:

 The job failed to run. The exception was : Unknown command: u'del_hit' Traceback (most recent call last): File "/home/vadmin/development/python/my_proj/chronograph/models.py", line 213, in handle_run call_command(self.command, *args, **options) File "/usr/lib/python2.6/dist-packages/django/core/management/__init__.py", line 155, in call_command raise CommandError("Unknown command: %r" % name) CommandError: Unknown command: u'del_hit' 

I tried to run the command myself from the standard view:

 from django.core.management import call_command def test_del(request): list = Hit.objects.filter(created__lt = datetime.now()-timedelta(days=2) ) args = [] options = {} call_command('del_hit', *args, **options) return render_to_response('test.html', {'del_hit_item':list[0]}) 

This succeeded.

Finally, I tried to configure cronjob to run every hour

 30 * * * * python /home/vadmin/development/python/my_proj/manage.py del_hit 

This did not work.

I could use some help to run my custom team on a schedule using either a django chronograph (preferably) or a simple cronjob

+4
source share
2 answers

It looks like you need to be in your project directory for this to work correctly.

Try updating the command you invoke to run 'cd' first

(cd /home/vadmin/development/python/my_proj && ./manage.py del_hit)

+4
source

To write a django script suitable for crontab, put these lines at the top:

 #!/usr/bin/python from django.core.management import setup_environ import settings setup_environ(settings) 

If your file is not in the same settings.py folder, add the application folder to the pythons path.

+2
source

Source: https://habr.com/ru/post/1333202/


All Articles