-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathREADME
More file actions
232 lines (152 loc) · 6.1 KB
/
README
File metadata and controls
232 lines (152 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
-----
About
-----
**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.
-----
Usage
-----
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')
------------
Running jobs
------------
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.
----------------
Scheduling times
----------------
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.
----------
Parameters
----------
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()
------------
Rescheduling
------------
Some jobs may need to be repeated. You can achieve this by scheduling a new
job in the handler of a job, but it is more convenient to use the ``reschedule``
method on jobs. ``reschedule`` has the same signature as ``schedule_job``, but
copies attributes of the current job.
::
>>> @job_as_parameter
... def handler(job, n=5):
... do_something()
... job.reschedule('3d', kwargs={'n': 6})
When you pass a relative time value to ``reschedule``, the new scheduled time
is calculated by adding the offset to the *scheduled time* of the original job,
not to the time the job was actually executed.
--------
Feedback
--------
There is a `home page <http://github.com/shrubberysoft/django-future>`_ with
instructions on how to access the code repository.
Send feedback and suggestions to team@shrubberysoft.com.
-------
Changes
-------
Changes in version 0.2.3
========================
* Fixed a NameError on `ignore_errors` (thanks doreilly@github).
Changes in version 0.2.2
========================
(thanks to Jannis Leidel!)
* Marked strings for translation.
* Added German translation.
* Raise a nicer error in case a job is running.
* Use admin fieldset.
Changes in version 0.2.1
========================
* Fixed a bug in start_scheduled_jobs parameters (thanks to Maciek Szczesniak).
Changes in version 0.2.0
========================
* Store the string value returned by the job.
Changes in version 0.1.9
========================
* 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'.
Changes in version 0.1.8
========================
* Updated admin interface: colored status, filtering by date.
* Reused django-picklefield implementation for storing job arguments instead of
the homebrewn pickle field.
Changes in version 0.1.7
========================
* Doctests are now part of the source distribution.
Changes in version 0.1.6
========================
* Minor packaging and formatting changes.
Changes in version 0.1.5
========================
* Basic protection against concurrent job processors.
* Added ``--ignore-errors`` option.
Changes in version 0.1.4
========================
* Transaction support.
* Added ``-d`` option to ``runscheduledjobs`` command.
* Better test coverage.
Changes in version 0.1.3
========================
* Fix pickled field implementation.
* Job rescheduling made easy.
Changes in version 0.1.1
========================
* Renamed to ``django-future``.
Changes in version 0.1
======================
* First public release.