forked from edeng23/binance-trade-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.py
32 lines (25 loc) · 1.04 KB
/
scheduler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import datetime
import logging
from traceback import format_exc
from schedule import Scheduler, Job
class SafeScheduler(Scheduler):
"""
An implementation of Scheduler that catches jobs that fail, logs their
exception tracebacks as errors, 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, logger: logging.Logger, rerun_immediately=True):
self.logger = logger
self.rerun_immediately = rerun_immediately
super().__init__()
def _run_job(self, job: Job):
try:
super()._run_job(job)
except Exception:
self.logger.error(f"Error while {next(iter(job.tags))}...\n{format_exc()}")
job.last_run = datetime.datetime.now()
if not self.rerun_immediately:
# Reschedule the job for the next time it was meant to run, instead of letting it run
# next tick
job._schedule_next_run()