Skip to content

Commit

Permalink
Move error reporting to the finally block
Browse files Browse the repository at this point in the history
  • Loading branch information
hammady committed Dec 17, 2023
1 parent 2aaf60a commit f04f12f
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pyworker/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def handle_job(self, job):
return
with self._instrument(job):
start_time = time.time()
error = failed = False
caught_exception = None
try:
if job.abstract:
raise ValueError(('Unsupported Job: %s, please import it ' \
Expand All @@ -157,23 +159,22 @@ def handle_job(self, job):
job.after()
job.success()
job.remove()
# report success
if self.newrelic_app:
newrelic.agent.add_custom_attribute('error', False)
newrelic.agent.add_custom_attribute('job_failure', False)
except Exception as exception:
error = True
caught_exception = exception
# handle error
error_str = traceback.format_exc()
failed = job.set_error_unlock(error_str)
# report error
if self.newrelic_app:
newrelic.agent.add_custom_attribute('error', True)
newrelic.agent.add_custom_attribute('job_failure', failed)
newrelic.agent.record_exception(exception)
# if that was a termination error, bubble up to caller
if type(exception) == TerminatedException:
raise exception
finally:
# report error status
if self.newrelic_app:
newrelic.agent.add_custom_attribute('error', error)
newrelic.agent.add_custom_attribute('job_failure', failed)
if caught_exception:
newrelic.agent.record_exception(caught_exception)
time_diff = time.time() - start_time
self.logger.info('Job %d finished in %d seconds' % \
(job.job_id, time_diff))

0 comments on commit f04f12f

Please sign in to comment.