Merge pull request #1 from peerchemist/master
Fixes module import on python3
This commit is contained in:
commit
f9c0410319
9 changed files with 107 additions and 6 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
*.pyc
|
||||
*.mypy*
|
1
build/lib/timeloop/__init__.py
Normal file
1
build/lib/timeloop/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from timeloop.app import Timeloop
|
65
build/lib/timeloop/app.py
Normal file
65
build/lib/timeloop/app.py
Normal file
|
@ -0,0 +1,65 @@
|
|||
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()
|
6
build/lib/timeloop/exceptions.py
Normal file
6
build/lib/timeloop/exceptions.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
class ServiceExit(Exception):
|
||||
"""
|
||||
Custom exception which is used to trigger the clean exit
|
||||
of all running threads and the main program.
|
||||
"""
|
||||
pass
|
4
build/lib/timeloop/helpers.py
Normal file
4
build/lib/timeloop/helpers.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
from timeloop.exceptions import ServiceExit
|
||||
|
||||
def service_shutdown(signum, frame):
|
||||
raise ServiceExit
|
19
build/lib/timeloop/job.py
Normal file
19
build/lib/timeloop/job.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
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)
|
|
@ -1 +1 @@
|
|||
from app import Timeloop
|
||||
from timeloop.app import Timeloop
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
import logging, sys, signal, time
|
||||
import logging
|
||||
import sys
|
||||
import signal
|
||||
import time
|
||||
|
||||
from timeloop.exceptions import ServiceExit
|
||||
from timeloop.job import Job
|
||||
from timeloop.helpers import service_shutdown
|
||||
|
||||
from exceptions import ServiceExit
|
||||
from job import Job
|
||||
from helpers import service_shutdown
|
||||
|
||||
class Timeloop():
|
||||
def __init__(self):
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from exceptions import ServiceExit
|
||||
from timeloop.exceptions import ServiceExit
|
||||
|
||||
|
||||
def service_shutdown(signum, frame):
|
||||
raise ServiceExit
|
||||
|
|
Loading…
Reference in a new issue