author | Sverre Rabbelier <sverre@rabbelier.nl> |
Mon, 25 May 2009 23:40:07 +0200 | |
changeset 2346 | d830123c1be1 |
parent 2345 | f78caf12f32d |
child 2366 | 1a954e80d444 |
permissions | -rw-r--r-- |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
2 |
# |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2009 the Melange authors. |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
4 |
# |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
8 |
# |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
10 |
# |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
16 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
17 |
"""Cron jobs. |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
18 |
""" |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
19 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
20 |
__authors__ = [ |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
21 |
'"Sverre Rabbelier" <sverre@rabbelier.nl>', |
2219
6f835882160e
Fixed typo and added myself to authors in the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2217
diff
changeset
|
22 |
'"Lennard de Rijk" <ljvderijk@gmail.com>', |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
23 |
] |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
24 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
25 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
26 |
import logging |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
27 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
28 |
from google.appengine.ext import db |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
29 |
from google.appengine.runtime import DeadlineExceededError |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
30 |
|
2224
fc719e902a70
Hook up the Student Proposal Mailer in the cron/jobs module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2219
diff
changeset
|
31 |
from soc.cron import student_proposal_mailer |
2345
f78caf12f32d
Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2247
diff
changeset
|
32 |
from soc.cron import unique_user_id_adder |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
33 |
from soc.models.job import Job |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
34 |
|
2345
f78caf12f32d
Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2247
diff
changeset
|
35 |
|
2217
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
36 |
class Error(Exception): |
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
37 |
"""Base class for all exceptions raised by this module. |
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
38 |
""" |
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
39 |
pass |
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
40 |
|
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
41 |
|
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
42 |
class FatalJobError(Error): |
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
43 |
"""Class for all errors that lead to immediate job abortion. |
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
44 |
""" |
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
45 |
pass |
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
46 |
|
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
47 |
|
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
48 |
class Handler(object): |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
49 |
"""A handler that dispatches a cron job. |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
50 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
51 |
The tasks that are mapped into tasks will be called when a worker |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
52 |
has claimed the job. However, there is no guarantee as to how long |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
53 |
the task will be allowed to run. If an Exception is raised the task |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
54 |
is automatically rescheduled for execution. |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
55 |
""" |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
56 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
57 |
def __init__(self): |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
58 |
"""Constructs a new Handler with all known jobs set. |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
59 |
""" |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
60 |
|
2243
c61f9dd5e325
Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2229
diff
changeset
|
61 |
self.OUT_OF_TIME = 0 |
c61f9dd5e325
Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2229
diff
changeset
|
62 |
self.ALREADY_CLAIMED = 1 |
c61f9dd5e325
Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2229
diff
changeset
|
63 |
self.SUCCESS = 2 |
c61f9dd5e325
Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2229
diff
changeset
|
64 |
self.ABORTED = 3 |
c61f9dd5e325
Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2229
diff
changeset
|
65 |
self.ERRORED = 4 |
c61f9dd5e325
Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2229
diff
changeset
|
66 |
|
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
67 |
self.tasks = {} |
2224
fc719e902a70
Hook up the Student Proposal Mailer in the cron/jobs module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2219
diff
changeset
|
68 |
self.tasks['setupStudentProposalMailing'] = \ |
fc719e902a70
Hook up the Student Proposal Mailer in the cron/jobs module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2219
diff
changeset
|
69 |
student_proposal_mailer.setupStudentProposalMailing |
fc719e902a70
Hook up the Student Proposal Mailer in the cron/jobs module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2219
diff
changeset
|
70 |
self.tasks['sendStudentProposalMail'] = \ |
fc719e902a70
Hook up the Student Proposal Mailer in the cron/jobs module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2219
diff
changeset
|
71 |
student_proposal_mailer.sendStudentProposalMail |
2345
f78caf12f32d
Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2247
diff
changeset
|
72 |
self.tasks['setupUniqueUserIdAdder'] = \ |
f78caf12f32d
Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2247
diff
changeset
|
73 |
unique_user_id_adder.setupUniqueUserIdAdder |
f78caf12f32d
Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2247
diff
changeset
|
74 |
self.tasks['addUniqueUserIds'] = \ |
f78caf12f32d
Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2247
diff
changeset
|
75 |
unique_user_id_adder.addUniqueUserIds |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
76 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
77 |
def claimJob(self, job_key): |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
78 |
"""A transaction to claim a job. |
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
79 |
|
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
80 |
The transaction is rolled back if the status is not 'waiting'. |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
81 |
""" |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
82 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
83 |
job = Job.get_by_id(job_key) |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
84 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
85 |
if job.status != 'waiting': |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
86 |
raise db.Rollback() |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
87 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
88 |
job.status = 'started' |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
89 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
90 |
if job.put(): |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
91 |
return job |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
92 |
else: |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
93 |
return None |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
94 |
|
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
95 |
def timeoutJob(self, job): |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
96 |
"""Timeout a job. |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
97 |
|
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
98 |
If a job has timed out more than 50 times, the job is aborted. |
2229
b36ecf371aef
Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2224
diff
changeset
|
99 |
""" |
b36ecf371aef
Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2224
diff
changeset
|
100 |
|
b36ecf371aef
Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2224
diff
changeset
|
101 |
job.timeouts += 1 |
b36ecf371aef
Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2224
diff
changeset
|
102 |
|
b36ecf371aef
Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2224
diff
changeset
|
103 |
if job.timeouts > 50: |
b36ecf371aef
Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2224
diff
changeset
|
104 |
job.status = 'aborted' |
b36ecf371aef
Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2224
diff
changeset
|
105 |
else: |
b36ecf371aef
Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2224
diff
changeset
|
106 |
job.status = 'waiting' |
b36ecf371aef
Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2224
diff
changeset
|
107 |
|
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
108 |
job.put() |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
109 |
|
2229
b36ecf371aef
Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2224
diff
changeset
|
110 |
job_id = job.key().id() |
b36ecf371aef
Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2224
diff
changeset
|
111 |
logging.debug("job %d now timeout %d time(s)" % (job_id, job.timeouts)) |
b36ecf371aef
Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2224
diff
changeset
|
112 |
|
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
113 |
def failJob(self, job): |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
114 |
"""Fail a job. |
2229
b36ecf371aef
Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2224
diff
changeset
|
115 |
|
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
116 |
If the job has failed more than 5 times, the job is aborted. |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
117 |
""" |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
118 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
119 |
job.errors += 1 |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
120 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
121 |
if job.errors > 5: |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
122 |
job.status = 'aborted' |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
123 |
else: |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
124 |
job.status = 'waiting' |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
125 |
|
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
126 |
job.put() |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
127 |
|
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
128 |
job_id = job.key().id() |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
129 |
logging.warning("job %d now failed %d time(s)" % (job_id, job.errors)) |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
130 |
|
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
131 |
def finishJob(self, job): |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
132 |
"""Finish a job. |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
133 |
""" |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
134 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
135 |
job.status = 'finished' |
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
136 |
job.put() |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
137 |
|
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
138 |
def abortJob(self, job): |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
139 |
"""Abort a job. |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
140 |
""" |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
141 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
142 |
job.status = 'aborted' |
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
143 |
job.put() |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
144 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
145 |
def handle(self, job_key): |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
146 |
"""Handle one job. |
2213
c0f52da7a808
Remove testing method and update docstring for cron system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2211
diff
changeset
|
147 |
|
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
148 |
Returns: one of the following status codes: |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
149 |
self.OUT_OF_TIME: returned when a DeadlineExceededError is raised |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
150 |
self.ALREADY_CLAIMED: if job.status is not 'waiting' |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
151 |
self.SUCCESS: if the job.status has been set to 'succes' |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
152 |
self.ABORTED: if the job.status has been set to 'aborted' |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
153 |
self.ERRORED: if the job encountered an error |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
154 |
""" |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
155 |
|
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
156 |
job = None |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
157 |
|
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
158 |
try: |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
159 |
job = db.run_in_transaction(self.claimJob, job_key) |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
160 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
161 |
if not job: |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
162 |
# someone already claimed the job |
2243
c61f9dd5e325
Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2229
diff
changeset
|
163 |
return self.ALREADY_CLAIMED |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
164 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
165 |
if job.task_name not in self.tasks: |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
166 |
logging.error("Unknown job %s" % job.task_name) |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
167 |
db.run_in_transaction(self.abortJob, job_key) |
2243
c61f9dd5e325
Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2229
diff
changeset
|
168 |
return self.ABORTED |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
169 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
170 |
task = self.tasks[job.task_name] |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
171 |
|
2219
6f835882160e
Fixed typo and added myself to authors in the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2217
diff
changeset
|
172 |
# execute the actual job |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
173 |
task(job) |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
174 |
|
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
175 |
self.finishJob(job) |
2243
c61f9dd5e325
Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2229
diff
changeset
|
176 |
return self.SUCCESS |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
177 |
except DeadlineExceededError, exception: |
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
178 |
if job: |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
179 |
self.timeoutJob(job) |
2243
c61f9dd5e325
Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2229
diff
changeset
|
180 |
return self.OUT_OF_TIME |
2217
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
181 |
except FatalJobError, exception: |
2c6ec0357149
Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2213
diff
changeset
|
182 |
logging.exception(exception) |
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
183 |
if job: |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
184 |
self.abortJob(job) |
2243
c61f9dd5e325
Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2229
diff
changeset
|
185 |
return self.ABORTED |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
186 |
except Exception, exception: |
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
187 |
logging.exception(exception) |
2247
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
188 |
if job: |
64968b86b07c
Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2246
diff
changeset
|
189 |
self.failJob(job) |
2243
c61f9dd5e325
Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2229
diff
changeset
|
190 |
return self.ERRORED |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
191 |
|
2246
c29272f640b0
Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2244
diff
changeset
|
192 |
def iterate(self, jobs, retry_jobs): |
c29272f640b0
Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2244
diff
changeset
|
193 |
"""Trivial iterator that iterates over jobs then retry_jobs |
c29272f640b0
Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2244
diff
changeset
|
194 |
""" |
c29272f640b0
Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2244
diff
changeset
|
195 |
|
c29272f640b0
Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2244
diff
changeset
|
196 |
for job in jobs: |
c29272f640b0
Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2244
diff
changeset
|
197 |
yield job |
c29272f640b0
Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2244
diff
changeset
|
198 |
while retry_jobs: |
c29272f640b0
Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2244
diff
changeset
|
199 |
yield retry_jobs[0] |
2211
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
200 |
|
f7497180d037
Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
201 |
handler = Handler() |