Repository navigation
Count attempts when a job is picked up so killed runs can't loop forever #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
|
|
||
| class TimeoutException(Exception): pass | ||
| class TerminatedException(Exception): pass | ||
| class AttemptsExhaustedException(Exception): pass | ||
|
|
||
| class Worker(object): | ||
| def __init__(self, dbstring, logger=None, | ||
|
|
@@ -126,12 +127,15 @@ def get_job_row(): | |
| now, expired = str(now), str(expired) | ||
| queues = self.queue_names.split(',') | ||
| queues = ', '.join(["'%s'" % q for q in queues]) | ||
| fields = ['id', 'attempts', 'run_at', 'queue', 'handler'] | ||
| # attempts is counted when the job is locked (see the UPDATE below), so a run | ||
| # that never reaches set_error_unlock (e.g. the process is OOM-killed) still | ||
| # uses an attempt; return the attempts made before this run | ||
| fields = ['id', 'attempts - 1', 'run_at', 'queue', 'handler'] | ||
| if self.extra_delayed_job_fields: | ||
| fields += self.extra_delayed_job_fields | ||
| fields = ', '.join(fields) | ||
| query = ''' | ||
| UPDATE delayed_jobs SET locked_at = '%s', locked_by = '%s' | ||
| UPDATE delayed_jobs SET locked_at = '%s', locked_by = '%s', attempts = attempts + 1 | ||
| WHERE id IN (SELECT delayed_jobs.id FROM delayed_jobs | ||
| WHERE ((run_at <= '%s' | ||
| AND (locked_at IS NULL OR locked_at < '%s') | ||
|
|
@@ -142,7 +146,11 @@ def get_job_row(): | |
| ''' % (now, self.name, now, expired, self.name, queues, fields) | ||
| self.logger.debug('query: %s' % query) | ||
| self._cursor.execute(query) | ||
| return self._cursor.fetchone() | ||
| row = self._cursor.fetchone() | ||
| # commit the lock and the attempt now: if the process is killed while the | ||
| # job runs, an uncommitted UPDATE would be rolled back with it | ||
| self.database.commit() | ||
| return row | ||
|
|
||
| job_row = get_job_row() | ||
| if job_row: | ||
|
|
@@ -162,6 +170,12 @@ def handle_job(self, job): | |
| error = failed = False | ||
| caught_exc_info = None | ||
| try: | ||
| if job.attempts >= self.max_attempts: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this replace the comparison done on error?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it complements it. The comparison in |
||
| # every previous run was counted but never finished, e.g. the | ||
| # worker was killed, so fail it instead of running it again | ||
| raise AttemptsExhaustedException(('Job %d was not run: it already used ' \ | ||
| + 'all %d attempts without finishing, the worker was likely killed ' \ | ||
| + '(e.g. out of memory)') % (job.job_id, self.max_attempts)) | ||
| if job.abstract: | ||
| raise ValueError(('Unsupported Job: %s, please import it ' \ | ||
| + 'before you can handle it') % job.class_name) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.