Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An implementation of Scheduler that catches jobs that fail. For use with https://github.com/dbader/schedule
import logging
from traceback import format_exc
import datetime
from schedule import Scheduler
logger = logging.getLogger('schedule')
class SafeScheduler(Scheduler):
"""
An implementation of Scheduler that catches jobs that fail, logs their
exception tracebacks as errors, optionally reschedules the jobs for their
next run time, and keeps going.
Use this to run jobs that may or may not crash without worrying about
whether other jobs will run or if they'll crash the entire script.
"""
def __init__(self, reschedule_on_failure=True):
"""
If reschedule_on_failure is True, jobs will be rescheduled for their
next run as if they had completed successfully. If False, they'll run
on the next run_pending() tick.
"""
self.reschedule_on_failure = reschedule_on_failure
super().__init__()
def _run_job(self, job):
try:
super()._run_job(job)
except Exception:
logger.error(format_exc())
job.last_run = datetime.datetime.now()
job._schedule_next_run()
#!/usr/bin/env python3
import time
from safe_schedule import SafeScheduler
def good_task_1():
print('Good Task 1')
def good_task_2():
print('Good Task 2')
def good_task_3():
print('Good Task 3')
def bad_task_1():
print('Bad Task 1')
print(1/0)
def bad_task_2():
print('Bad Task 2')
raise Exception('Something went wrong!')
scheduler = SafeScheduler()
scheduler.every(3).seconds.do(good_task_1)
scheduler.every(5).seconds.do(bad_task_1)
scheduler.every(7).seconds.do(good_task_2)
scheduler.every(8).seconds.do(bad_task_2)
scheduler.every(12).seconds.do(good_task_3)
while True:
scheduler.run_pending()
time.sleep(1)
@abelsonlive

This comment has been minimized.

Copy link

abelsonlive commented Sep 28, 2014

I get the following error when i run this:

Traceback (most recent call last):
  File "scheduler.py", line 31, in _run_job
    super()._run_job(job)
TypeError: super() takes at least 1 argument (0 given)
@abelsonlive

This comment has been minimized.

Copy link

abelsonlive commented Sep 28, 2014

Oh whups, it's a python 3 thing. FYI: Here's a python 2 version:

class TaskScheduler(Scheduler):
  """
  An implementation of Scheduler that catches jobs that fail, logs their
  exception tracebacks as errors, optionally reschedules the jobs for their
  next run time, and keeps going.

  Use this to run jobs that may or may not crash without worrying about
  whether other jobs will run or if they'll crash the entire script.
  """

  def __init__(self, reschedule_on_failure=True):
    """
    If reschedule_on_failure is True, jobs will be rescheduled for their
    next run as if they had completed successfully. If False, they'll run
    on the next run_pending() tick.
    """
    self.reschedule_on_failure = reschedule_on_failure
    Scheduler.__init__(self)

  def _run_job(self, job):
    try:
      Scheduler._run_job(self, job)
    except Exception:
      tb = format_exc()
      logger.error(tb)
      job.last_run = datetime.datetime.now()
      job._schedule_next_run()
@El3k0n

This comment has been minimized.

Copy link

El3k0n commented Dec 6, 2014

You don't need to use the traceback module, logging has the exception method to deal with exceptions, which automatically appends the traceback to the log.

Example:
logger.exception("Here's the error: ")

@simkimsia

This comment has been minimized.

Copy link

simkimsia commented Mar 4, 2017

what if I just want to restart the whole script instead of scheduling the next run?

I used your code to run something that needs to talk to a reader. I tested by purposely disconnecting to the internet to cause some issues.

How do I resolve this? Perhaps restart the script after say 60 seconds and then keep doing the restart every 60 seconds?

@h-2-0

This comment has been minimized.

Copy link

h-2-0 commented Mar 12, 2017

Hello, @simkimsia, @abelsonlive, @El3k0n, @mplewis

I have been using SafeScheduler for a while with success...But now I need to use run_continuously(). So excuse me if my question sounds too naive but how do I implement run_continously() into SafeScheduler?

Thanks!

@hldh214

This comment has been minimized.

Copy link

hldh214 commented Jul 25, 2017

Hey @mplewis
im new to python and im wonder about how reschedule_on_failure=True work?
seems has no effect to anything
pls help me understand of this

Thanks <3

@lobstrio

This comment has been minimized.

Copy link

lobstrio commented Aug 4, 2018

Hi @h-2-0,

If you well fixed the schedule/init.py as here, you can just implement it as following, using super() and the inherited method from Scheduler <Scheduler>:

class SafeScheduler(Scheduler):
    """
    An implementation of Scheduler that catches jobs that fail, logs their
    exception tracebacks as errors, optionally reschedules the jobs for their
    next run time, and keeps going.
    Use this to run jobs that may or may not crash without worrying about
    whether other jobs will run or if they'll crash the entire script.
    """

    def __init__(self, reschedule_on_failure=True):
        """
        If reschedule_on_failure is True, jobs will be rescheduled for their
        next run as if they had completed successfully. If False, they'll run
        on the next run_pending() tick.
        """
        self.reschedule_on_failure = reschedule_on_failure
        super().__init__()

    def _run_job(self, job):
        try:
            super()._run_job(job)
        except Exception:
            logger.error(format_exc())
            job.last_run = datetime.datetime.now()
            job._schedule_next_run()
            
    def run_continuously(self, interval=1):
        try:
            super().run_continuously(interval)
        except Exception:
            logger.error(format_exc())
            job.last_run = datetime.datetime.now()
            job._schedule_next_run()
@eddified

This comment has been minimized.

Copy link

eddified commented Mar 18, 2019

Hey @mplewis
im new to python and im wonder about how reschedule_on_failure=True work?
seems has no effect to anything
pls help me understand of this

Thanks <3

You're right, I don't think reschedule_on_failure does anything. I checked the parent class (version 0.6.0) and it doesn't reference that variable at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.