-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtasks.py
More file actions
78 lines (56 loc) · 1.88 KB
/
tasks.py
File metadata and controls
78 lines (56 loc) · 1.88 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
botogram.tasks
Core implementation of tasks and timers
Copyright (c) 2015-2016 Pietro Albini <pietro@pietroalbini.io>
Released under the MIT license
"""
import time
class BaseTask:
"""A basic task"""
def __init__(self, hook):
self.hook = hook
def process(self, bot):
"""Process the task"""
if hasattr(self.hook, "call"):
return self.hook.call(bot)
return self.hook(bot)
class TimerTask(BaseTask):
"""Representation of a single timer"""
def __init__(self, interval, hook):
self.interval = interval
self.last_run = -interval
super(TimerTask, self).__init__(hook)
def now(self, current=None):
"""Check if the timer should be ran now"""
# Allow to provide a dummy time
if current is None:
current = time.time()
res = self.last_run + self.interval <= current
# Increment the last_run if the result is True
if res:
self.last_run = current
return res
class Scheduler:
"""Schedule all the tasks"""
def __init__(self):
# Each component will add its own list here
self.tasks_lists = []
self.tasks = []
self.tasks_lists.append(self.tasks)
def add(self, task):
"""Add a task to the scheduler"""
self.tasks.append(task)
def register_tasks_list(self, tasks):
"""Register a new list of tasks"""
self.tasks_lists.append(tasks)
def now(self, current=None):
"""Return which tasks should be scheduled now"""
# Allow to provide a dummy time
if current is None:
current = time.time()
# Return all the tasks which should be executed now
for tasks in self.tasks_lists:
for task in tasks:
if not task.now(current):
continue
yield task