forked from Netflix/dispatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.py
More file actions
48 lines (34 loc) · 1.2 KB
/
Copy pathscheduler.py
File metadata and controls
48 lines (34 loc) · 1.2 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
.. module: dispatch.scheduler
:platform: Unix
:copyright: (c) 2019 by Netflix Inc., see AUTHORS for more
:license: Apache, see LICENSE for more details.
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
.. moduleauthor:: Marc Vilanova <mvilanova@netflix.com>
"""
import logging
import time
import schedule
log = logging.getLogger(__name__)
# See: https://schedule.readthedocs.io/en/stable/ for documentation on job syntax
class Scheduler(object):
"""Simple scheduler class that holds all scheduled functions."""
registered_tasks = []
def add(self, job, *args, **kwargs):
"""Adds a task to the scheduler."""
def decorator(func):
if not kwargs.get("name"):
name = func.__name__
else:
name = kwargs.pop("name")
self.registered_tasks.append({"name": name, "func": func, "job": job.do(func)})
return decorator
def remove(self, task):
"""Removes a task from the scheduler."""
schedule.cancel_job(task["job"])
def start(self):
"""Runs all scheduled tasks."""
while True:
schedule.run_pending()
time.sleep(1)
scheduler = Scheduler()