timeloop/looper/app.py

48 lines
1.5 KiB
Python
Raw Normal View History

2018-09-18 11:40:59 +02:00
import logging, sys, signal, time
from exceptions import ServiceExit
from job import Job
from helpers import service_shutdown
class Looper():
def __init__(self):
self.jobs = []
2018-09-18 11:57:32 +02:00
logger = logging.getLogger('looper')
2018-09-18 11:40:59 +02:00
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.INFO)
formatter = logging.Formatter('[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.setLevel(logging.INFO)
self.logger = logger
def _add_task(self, func, interval, *args, **kwargs):
j = Job(interval, func, *args, **kwargs)
self.jobs.append(j)
def job(self, interval):
def decorator(f):
self._add_task(f, interval)
return f
return decorator
def start(self):
try:
signal.signal(signal.SIGTERM, service_shutdown)
signal.signal(signal.SIGINT, service_shutdown)
self.logger.info("Starting looper..")
for j in self.jobs:
self.logger.info("Registered task {}".format(j.execute))
j.start()
self.logger.info("Looper now started. Tasks will run based on the interval set")
# block main thead
while True:
time.sleep(1)
except ServiceExit:
for j in self.jobs:
self.logger.info("Stopping task {}".format(j.execute))
j.stop()