Scheduling Tasks in Django

1

Galera is as follows, in Django things only happen when some user accesses your application.

AN EXAMPLE )

Let's say you've created an application where the user logs in and writes little reminder texts, more or less like a post-it, for him to check every day and check the activities he can not forget.

The user needs to sign in to the application to check their reminders for the current date and to add new reminders for future dates.

THE NEW NEED

Now imagine that you want to implement a new feature so that every day your application emails a list of user reminders for that day you are starting.

THE REFLECTION ABOUT THE CASE

Note that originally all the actions that occur in the application depend on a user's access, but for this new feature we will need the system to automatically start an action without waiting for a user action.

It would be a kind of service running that every day at a particular time check the reminders of each user, prepare each message and send them.

WHAT I HAD TO DO

It occurred to me to create a thread that runs all the time in parallel and that when it arrives at the scheduled time it triggers the necessary routines.

It seems to make some sense, but I'm not sure if there is a better option to do this, and what the pros and cons of that framework might be, and some potential impact on system resource consumption.

  

Import: I have no difficulty in preparing the routines that perform   actions (check reminders, prepare messages, and send   emails), my problem is about creating this schedule that goes   trigger this process automatically.

Have you ever had to create something like this with Django, how did you do it?

    
asked by anonymous 12.05.2017 / 15:21

1 answer

2

The Django Basic Tutorial guides you in creating a custom command for running external routines. In your case, from the description of the problem, I believe you can follow this line. Here's the link to Writing custom django-admin commands that has one detailed explanation on the subject.

In general terms you will have to:

  • Write your Command:

    class Command(BaseCommand):
        help = 'Command Customizado Teste'
    
        def add_arguments(self, parser):
            parser.add_argument('parametro', nargs='+', type=str)
    
        def handle(self, *args, **options):
            print("Chamou a execução deste command")
            print(options['parametro'])                 
    
  • Place the Command in your app's management / commands directory (in the Django example (project_dir) / polls / management / commands). The name of the command will be the name of the file (module) you created.

  • To verify that your Command has been recognized by Django, type python manage.py. You'll see something similar to this:

    Type 'manage.py help <subcommand>' for help on a specific subcommand.
    
    Available subcommands:
    
    [auth]
        changepassword
        createsuperuser
    
    [seu app]
        seu_command
    
    [django]
        check
        compilemessages
        createcachetable
        dbshell
        diffsettings
        dumpdata
        flush
        inspectdb
        loaddata
        makemessages
        makemigrations
        migrate
        sendtestemail
        shell
        showmigrations
        sqlflush
        sqlmigrate
        sqlsequencereset
        squashmigrations
        startapp
        startproject
        test
        testserver
    
    [sessions]
        clearsessions
    
    [staticfiles]
        collectstatic
        findstatic
        runserver
    
  • To run it use ( check the documentation for parameters ):

    python manage.py seu_command "seu parametro"
    
  • Finally, after you have finalized and tested your Command, schedule it to run on the OS crontab or task scheduler you are using.

  • 05.06.2017 / 18:09