removed build folder
This commit is contained in:
parent
36ec5cbb13
commit
100a3c7798
5 changed files with 0 additions and 95 deletions
|
@ -1 +0,0 @@
|
||||||
from timeloop.app import Timeloop
|
|
|
@ -1,65 +0,0 @@
|
||||||
import logging
|
|
||||||
import sys
|
|
||||||
import signal
|
|
||||||
import time
|
|
||||||
|
|
||||||
from timeloop.exceptions import ServiceExit
|
|
||||||
from timeloop.job import Job
|
|
||||||
from timeloop.helpers import service_shutdown
|
|
||||||
|
|
||||||
|
|
||||||
class Timeloop():
|
|
||||||
def __init__(self):
|
|
||||||
self.jobs = []
|
|
||||||
logger = logging.getLogger('timeloop')
|
|
||||||
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_job(self, func, interval, *args, **kwargs):
|
|
||||||
j = Job(interval, func, *args, **kwargs)
|
|
||||||
self.jobs.append(j)
|
|
||||||
|
|
||||||
def _block_main_thread(self):
|
|
||||||
signal.signal(signal.SIGTERM, service_shutdown)
|
|
||||||
signal.signal(signal.SIGINT, service_shutdown)
|
|
||||||
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
time.sleep(1)
|
|
||||||
except ServiceExit:
|
|
||||||
self.stop()
|
|
||||||
break
|
|
||||||
|
|
||||||
def _start_jobs(self, block):
|
|
||||||
for j in self.jobs:
|
|
||||||
j.daemon = not block
|
|
||||||
j.start()
|
|
||||||
self.logger.info("Registered job {}".format(j.execute))
|
|
||||||
|
|
||||||
def _stop_jobs(self):
|
|
||||||
for j in self.jobs:
|
|
||||||
self.logger.info("Stopping job {}".format(j.execute))
|
|
||||||
j.stop()
|
|
||||||
|
|
||||||
def job(self, interval):
|
|
||||||
def decorator(f):
|
|
||||||
self._add_job(f, interval)
|
|
||||||
return f
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
def stop(self):
|
|
||||||
self._stop_jobs()
|
|
||||||
self.logger.info("Timeloop exited.")
|
|
||||||
|
|
||||||
def start(self, block=False):
|
|
||||||
self.logger.info("Starting Timeloop..")
|
|
||||||
self._start_jobs(block=block)
|
|
||||||
|
|
||||||
self.logger.info("Timeloop now started. Jobs will run based on the interval set")
|
|
||||||
if block:
|
|
||||||
self._block_main_thread()
|
|
|
@ -1,6 +0,0 @@
|
||||||
class ServiceExit(Exception):
|
|
||||||
"""
|
|
||||||
Custom exception which is used to trigger the clean exit
|
|
||||||
of all running threads and the main program.
|
|
||||||
"""
|
|
||||||
pass
|
|
|
@ -1,4 +0,0 @@
|
||||||
from timeloop.exceptions import ServiceExit
|
|
||||||
|
|
||||||
def service_shutdown(signum, frame):
|
|
||||||
raise ServiceExit
|
|
|
@ -1,19 +0,0 @@
|
||||||
from threading import Thread, Event
|
|
||||||
from datetime import timedelta
|
|
||||||
|
|
||||||
class Job(Thread):
|
|
||||||
def __init__(self, interval, execute, *args, **kwargs):
|
|
||||||
Thread.__init__(self)
|
|
||||||
self.stopped = Event()
|
|
||||||
self.interval = interval
|
|
||||||
self.execute = execute
|
|
||||||
self.args = args
|
|
||||||
self.kwargs = kwargs
|
|
||||||
|
|
||||||
def stop(self):
|
|
||||||
self.stopped.set()
|
|
||||||
self.join()
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
while not self.stopped.wait(self.interval.total_seconds()):
|
|
||||||
self.execute(*self.args, **self.kwargs)
|
|
Loading…
Reference in a new issue