django-future is a Django application for scheduling jobs on specified times.
django-future allows you to schedule invocation of callables at a given time. The job queue is stored in the database and can be managed through the admin interface. Queued jobs are run by invoking an external django management command.
You need to have django-future installed. A recent version should be available from PyPI.
To schedule jobs from your code, use the schedule_job
function:
>>> from django_future import schedule_job >>> import datetime >>> schedule_job(datetime.datetime(2010, 10, 10), ... 'myproject.myapp.handlers.dosomething')
Scheduled jobs will not start automagically. The job queue must regularly
be processed by invoking the Django management command
runscheduledjobs
. You will probably want to run this command regularly,
perhaps in a cron job, to ensure that scheduled jobs are run in a timely
manner.
When the job processor is started, it checks for concurrently active job processors. If any active jobs are found, the new instance of the job processor will not continue and will raise an error, so you do not need to worry about overlapping parallel job runs.
Each job is run in a separate database transaction. If the handler raises an error, the transaction is rolled back.
By default, job entries for completed jobs are marked as finished, but not
deleted from the database. If you do not want to keep them, use the -d
parameter to runscheduledjobs
and they will be deleted upon successful
completion.
If a job handler raises an error, the queue processor will abort and
show the traceback. If you do not want to abort the processing in such a case
use the -i
parameter. Either way, if an exception occurs, the traceback
will be stored on the job entry in the database.
If a job returns a value, the unicode representation of that value will also be stored on the job entry in the database.
There are several ways to indicate the time the job should be executed. You can use a straight datetime (as above), but you can also specify an offset from the present. The offset can be a specified as a timedelta:
>>> schedule_job(datetime.timedelta(days=5), 'myproject.myapp.x')
or it can be a string:
>>> schedule_job('5d', 'myproject.myapp.x')
An expiry time (one week by default) may also be specified so that old jobs will not be run by accident.
>>> schedule_job('5d', 'myproject.myapp.x', expires='7d')
The expiry date is calculated relative to the scheduled time.
You can pass parameters to jobs:
>>> schedule_job('5d', 'myproject.myapp.x', ... args=[1, 2], kwargs={'foo': 'bar'})
The parameters will be passed on to the callable. Note that the parameters have to be picklable.
You can also associate a job with a database object:
>>> schedule_job('5d', 'myproject.myapp.x', ... content_object=some_model_instance)
If specified, the content object will be passed in to the callable as the first parameter.
If you decorate your handler using job_as_parameter
, the active job will be
passed as a parameter. Example:
>>> from django_future import job_as_parameter >>> @job_as_parameter ... def handler(job): ... do_stuff()
There is a home page with instructions on how to access the code repository.
Send feedback and suggestions to team@shrubberysoft.com.
- Fixed a NameError on ignore_errors (thanks doreilly@github).
(thanks to Jannis Leidel!)
- Marked strings for translation.
- Added German translation.
- Raise a nicer error in case a job is running.
- Use admin fieldset.
- Fixed a bug in start_scheduled_jobs parameters (thanks to Maciek Szczesniak).
- Store the string value returned by the job.
- When rescheduling, the new date is calculated from the scheduled date of the current job rather than the start of the actual run.
- Implemented check for concurrent job processors properly.
- Status of expired jobs is now set to 'expired'.
- Updated admin interface: colored status, filtering by date.
- Reused django-picklefield implementation for storing job arguments instead of the homebrewn pickle field.
- Doctests are now part of the source distribution.
- Minor packaging and formatting changes.
- Basic protection against concurrent job processors.
- Added
--ignore-errors
option.
- Transaction support.
- Added
-d
option torunscheduledjobs
command. - Better test coverage.
- Fix pickled field implementation.
- Job rescheduling made easy.
- Renamed to
django-future
.
- First public release.