25
25
26
26
27
27
class JobQueue (object ):
28
- """
29
- This class allows you to periodically perform tasks with the bot.
28
+ """This class allows you to periodically perform tasks with the bot.
30
29
31
30
Attributes:
32
31
queue (PriorityQueue):
@@ -47,17 +46,16 @@ def __init__(self, bot):
47
46
self ._running = False
48
47
49
48
def put (self , job , next_t = None , prevent_autostart = False ):
50
- """
51
- Queue a new job. If the JobQueue is not running, it will be started.
49
+ """Queue a new job. If the JobQueue is not running, it will be started.
52
50
53
51
Args:
54
52
job (Job): The ``Job`` instance representing the new job
55
53
next_t (Optional[float]): Time in seconds in which the job should be executed first.
56
54
Defaults to ``job.interval``
57
55
prevent_autostart (Optional[bool]): If ``True``, the job queue will not be started
58
56
automatically if it is not running. Defaults to ``False``
59
- """
60
57
58
+ """
61
59
job .job_queue = self
62
60
63
61
if next_t is None :
@@ -66,7 +64,7 @@ def put(self, job, next_t=None, prevent_autostart=False):
66
64
now = time .time ()
67
65
next_t += now
68
66
69
- self .logger .debug ('Putting a %s with t=%f' % ( job .name , next_t ) )
67
+ self .logger .debug ('Putting job %s with t=%f' , job .name , next_t )
70
68
self .queue .put ((next_t , job ))
71
69
72
70
# Wake up the loop if this job should be executed next
@@ -80,35 +78,36 @@ def put(self, job, next_t=None, prevent_autostart=False):
80
78
81
79
def tick (self ):
82
80
"""
83
- Run all jobs that are due and re-enqueue them with their interval
81
+ Run all jobs that are due and re-enqueue them with their interval.
82
+
84
83
"""
85
84
now = time .time ()
86
85
87
- self .logger .debug ('Ticking jobs with t=%f' % now )
86
+ self .logger .debug ('Ticking jobs with t=%f' , now )
88
87
89
88
while not self .queue .empty ():
90
89
t , job = self .queue .queue [0 ]
91
- self .logger .debug ('Peeked at %s with t=%f' % ( job .name , t ) )
90
+ self .logger .debug ('Peeked at %s with t=%f' , job .name , t )
92
91
93
92
if t <= now :
94
93
self .queue .get ()
95
94
96
95
if job ._remove .is_set ():
97
- self .logger .debug ('Removing job %s' % job .name )
96
+ self .logger .debug ('Removing job %s' , job .name )
98
97
continue
99
98
100
99
elif job .enabled :
101
- self .logger .debug ('Running job %s' % job .name )
100
+ self .logger .debug ('Running job %s' , job .name )
102
101
103
102
try :
104
103
job .run (self .bot )
105
104
106
105
except :
107
106
self .logger .exception (
108
- 'An uncaught error was raised while executing job %s' % job .name )
107
+ 'An uncaught error was raised while executing job %s' , job .name )
109
108
110
109
else :
111
- self .logger .debug ('Skipping disabled job %s' % job .name )
110
+ self .logger .debug ('Skipping disabled job %s' , job .name )
112
111
113
112
if job .repeat :
114
113
self .put (job )
@@ -127,6 +126,7 @@ def tick(self):
127
126
def start (self ):
128
127
"""
129
128
Starts the job_queue thread.
129
+
130
130
"""
131
131
self .__lock .acquire ()
132
132
@@ -135,7 +135,7 @@ def start(self):
135
135
self .__lock .release ()
136
136
job_queue_thread = Thread (target = self ._start , name = "job_queue" )
137
137
job_queue_thread .start ()
138
- self .logger .debug ('Job Queue thread started' )
138
+ self .logger .debug ('%s thread started' , self . __class__ . __name__ )
139
139
140
140
else :
141
141
self .__lock .release ()
@@ -144,8 +144,8 @@ def _start(self):
144
144
"""
145
145
Thread target of thread ``job_queue``. Runs in background and performs ticks on the job
146
146
queue.
147
- """
148
147
148
+ """
149
149
while self ._running :
150
150
self .__tick .wait (self ._next_peek and self ._next_peek - time .time ())
151
151
@@ -156,7 +156,7 @@ def _start(self):
156
156
157
157
self .tick ()
158
158
159
- self .logger .debug ('Job Queue thread stopped' )
159
+ self .logger .debug ('%s thread stopped' , self . __class__ . __name__ )
160
160
161
161
def stop (self ):
162
162
"""
@@ -215,6 +215,7 @@ def schedule_removal(self):
215
215
"""
216
216
Schedules this job for removal from the ``JobQueue``. It will be removed without executing
217
217
its callback function again.
218
+
218
219
"""
219
220
self ._remove .set ()
220
221
0 commit comments