Skip to content

Commit 25a0ce6

Browse files
KeyboardInterrupts and SystemExits are now handled intelligently.
1 parent 910c194 commit 25a0ce6

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

periodically/backends.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def check_timeout(self, task):
108108
'level': logging.ERROR,
109109
'msg': 'Task timed out after %s.' % running_time,
110110
}
111-
self.complete_task(task, extra=extra)
111+
self.complete_task(task, False, extra=extra)
112112

113113
def check_timeouts(self):
114114
"""
@@ -169,33 +169,39 @@ def run_task(self, task, schedule, scheduled_time=None):
169169
'msg': str(err),
170170
'exc_info': sys.exc_info(),
171171
}
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
172180
else:
173181
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)
177186

178187
def _create_receiver(self, sender):
179188
def receiver(task, extra=None):
180189
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)
182191
return receiver
183192

184-
def complete_task(self, task, extra=None):
193+
def complete_task(self, task, success=True, extra=None):
185194
"""
186195
Marks a task as complete and performs other post-completion tasks. The
187196
<code>extra</code> argument is a dictionary of values to be passed to
188197
<code>Logger.log()</code> as keyword args.
189198
"""
190199
if extra is not None:
191200
self.logger.log(**extra)
192-
completed_successfully = extra.get('level', logging.ERROR) != logging.ERROR
193-
else:
194-
completed_successfully = True
195201

196202
record = ExecutionRecord.objects.filter(task_id=task.task_id, end_time=None).order_by('-start_time')[0]
197203
record.end_time = datetime.now()
198-
record.completed_successfully = completed_successfully
204+
record.completed_successfully = success
199205
record.save()
200206

201207
# TODO: Retries.

0 commit comments

Comments
 (0)