Compare commits
No commits in common. "master" and "v1.0.2" have entirely different histories.
8 changed files with 109 additions and 27 deletions
16
README.md
16
README.md
|
@ -1,15 +1,16 @@
|
||||||
# Timeloop
|
# Timeloop
|
||||||
Timeloop is a service that can be used to run periodic tasks after a certain interval.
|
Timeloop is a service that can be used to run periodic tasks after a certain interval.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
Each job runs on a separate thread and when the service is shut down, it waits till all tasks currently being executed are completed.
|
Each job runs on a separate thread and when the service is shut down, it waits till all tasks currently being executed are completed.
|
||||||
|
|
||||||
Inspired by this blog [`here`](https://www.g-loaded.eu/2016/11/24/how-to-terminate-running-python-threads-using-signals/)
|
Inspired by this blog [`here`](https://www.g-loaded.eu/2016/11/24/how-to-terminate-running-python-threads-using-signals/)
|
||||||
|
|
||||||
## Fork
|
|
||||||
This fork aims to provide some improvements to the original library. Mainly to be able to set a start time or start offset for tasks.
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
Has to be installed manually atm. Since it is just a fork of the original and I still have to set that up and want to respect the work of Sankalp Jonna. Feel free to help tho!
|
```sh
|
||||||
|
pip install timeloop
|
||||||
|
```
|
||||||
|
|
||||||
## Writing jobs
|
## Writing jobs
|
||||||
```python
|
```python
|
||||||
|
@ -29,10 +30,9 @@ def sample_job_every_5s():
|
||||||
print "5s job current time : {}".format(time.ctime())
|
print "5s job current time : {}".format(time.ctime())
|
||||||
|
|
||||||
|
|
||||||
# Added support for initial offset!
|
@tl.job(interval=timedelta(seconds=10))
|
||||||
@tl.job(interval=timedelta(seconds=10), offset=timedelta(hours=1))
|
def sample_job_every_10s():
|
||||||
def sample_job_after_an_hour_every_10s():
|
print "10s job current time : {}".format(time.ctime())
|
||||||
print "after an hour 10s job current time : {}".format(time.ctime())
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Start time loop in separate thread
|
## Start time loop in separate thread
|
||||||
|
|
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,4 +1,3 @@
|
||||||
from datetime import datetime, timedelta
|
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
import signal
|
import signal
|
||||||
|
@ -21,8 +20,8 @@ class Timeloop():
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(logging.INFO)
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
|
|
||||||
def _add_job(self, func, interval: timedelta, offset: timedelta=None, *args, **kwargs):
|
def _add_job(self, func, interval, *args, **kwargs):
|
||||||
j = Job(interval, func, offset=offset, *args, **kwargs)
|
j = Job(interval, func, *args, **kwargs)
|
||||||
self.jobs.append(j)
|
self.jobs.append(j)
|
||||||
|
|
||||||
def _block_main_thread(self):
|
def _block_main_thread(self):
|
||||||
|
@ -47,15 +46,9 @@ class Timeloop():
|
||||||
self.logger.info("Stopping job {}".format(j.execute))
|
self.logger.info("Stopping job {}".format(j.execute))
|
||||||
j.stop()
|
j.stop()
|
||||||
|
|
||||||
def job(self, interval: timedelta, offset: timedelta=None):
|
def job(self, interval):
|
||||||
"""Decorator to define a timeloop for the decorated function.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
interval (timedelta): How long to wait after every execution until the next one.
|
|
||||||
offset (timedelta, optional): Positive offset until the first execution of the function. If None, will wait with first execution until the first interval passed. If timedelta with length 0 (or smaller) will execute immediately. Defaults to None.
|
|
||||||
"""
|
|
||||||
def decorator(f):
|
def decorator(f):
|
||||||
self._add_job(f, interval, offset=offset)
|
self._add_job(f, interval)
|
||||||
return f
|
return f
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
from threading import Thread, Event
|
from threading import Thread, Event
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from time import sleep
|
|
||||||
|
|
||||||
class Job(Thread):
|
class Job(Thread):
|
||||||
def __init__(self, interval: timedelta, execute, offset: timedelta=None, *args, **kwargs):
|
def __init__(self, interval, execute, *args, **kwargs):
|
||||||
Thread.__init__(self)
|
Thread.__init__(self)
|
||||||
self.stopped = Event()
|
self.stopped = Event()
|
||||||
self.interval: timedelta = interval
|
self.interval = interval
|
||||||
self.execute = execute
|
self.execute = execute
|
||||||
self.offset: timedelta = offset
|
|
||||||
self.args = args
|
self.args = args
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
|
|
||||||
|
@ -17,9 +15,5 @@ class Job(Thread):
|
||||||
self.join()
|
self.join()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if self.offset:
|
|
||||||
sleep(self.offset.total_seconds())
|
|
||||||
self.execute(*self.args, **self.kwargs)
|
|
||||||
|
|
||||||
while not self.stopped.wait(self.interval.total_seconds()):
|
while not self.stopped.wait(self.interval.total_seconds()):
|
||||||
self.execute(*self.args, **self.kwargs)
|
self.execute(*self.args, **self.kwargs)
|
||||||
|
|
Loading…
Reference in a new issue