Skip to content

Handling Errors in Long-Running Scripts

  • fail fast on programmer errors
  • retry transient network failures
  • alert on repeated failure
retry_backoff.py
import time
 
 
def retry(fn, tries=3, base_delay=1.0):
    last = None
    for i in range(tries):
        try:
            return fn()
        except Exception as e:
            last = e
            time.sleep(base_delay * (2 ** i))
    raise last
main_guard.py
import logging
 
log = logging.getLogger("job")
 
 
def main():
    ...
 
 
if __name__ == "__main__":
    try:
        main()
    except Exception:
        log.exception("job failed")
        raise

Resilient scripts retry transient failures with backoff and only alert once retries are exhausted.

diagram Resilient error handling flow mermaid
How a long-running script retries with backoff and eventually alerts

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading