@@ -108,7 +108,7 @@ def check_timeout(self, task):
108
108
'level' : logging .ERROR ,
109
109
'msg' : 'Task timed out after %s.' % running_time ,
110
110
}
111
- self .complete_task (task , extra = extra )
111
+ self .complete_task (task , False , extra = extra )
112
112
113
113
def check_timeouts (self ):
114
114
"""
@@ -169,33 +169,39 @@ def run_task(self, task, schedule, scheduled_time=None):
169
169
'msg' : str (err ),
170
170
'exc_info' : sys .exc_info (),
171
171
}
172
+ success = False
173
+ except (KeyboardInterrupt , SystemExit ), err :
174
+ extra = {
175
+ 'level' : logging .DEBUG ,
176
+ 'msg' : 'The task was cancelled by the user.' ,
177
+ }
178
+ success = False
179
+ raise
172
180
else :
173
181
extra = None
174
-
175
- if extra is not None or getattr (task , 'is_blocking' , True ):
176
- self .complete_task (task , extra = extra )
182
+ success = True
183
+ finally :
184
+ if not success or getattr (task , 'is_blocking' , True ):
185
+ self .complete_task (task , success , extra = extra )
177
186
178
187
def _create_receiver (self , sender ):
179
188
def receiver (task , extra = None ):
180
189
task_complete .disconnect (receiver , sender , dispatch_uid = task .task_id )
181
- self .complete_task (task , extra = extra )
190
+ self .complete_task (task , True , extra = extra )
182
191
return receiver
183
192
184
- def complete_task (self , task , extra = None ):
193
+ def complete_task (self , task , success = True , extra = None ):
185
194
"""
186
195
Marks a task as complete and performs other post-completion tasks. The
187
196
<code>extra</code> argument is a dictionary of values to be passed to
188
197
<code>Logger.log()</code> as keyword args.
189
198
"""
190
199
if extra is not None :
191
200
self .logger .log (** extra )
192
- completed_successfully = extra .get ('level' , logging .ERROR ) != logging .ERROR
193
- else :
194
- completed_successfully = True
195
201
196
202
record = ExecutionRecord .objects .filter (task_id = task .task_id , end_time = None ).order_by ('-start_time' )[0 ]
197
203
record .end_time = datetime .now ()
198
- record .completed_successfully = completed_successfully
204
+ record .completed_successfully = success
199
205
record .save ()
200
206
201
207
# TODO: Retries.
0 commit comments