X Tutup
Skip to content

Commit 2ff6fe9

Browse files
committed
initial commit for JobQueue by @franciscod
1 parent 34b91f5 commit 2ff6fe9

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

docs/source/telegram.jobqueue.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
telegram.sticker module
2+
=======================
3+
4+
.. automodule:: telegram.jobqueue
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

docs/source/telegram.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Submodules
1111
telegram.bot
1212
telegram.updater
1313
telegram.dispatcher
14+
telegram.jobqueue
1415
telegram.chataction
1516
telegram.contact
1617
telegram.document

telegram/jobqueue.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#!/usr/bin/env python
2+
#
3+
# A library that provides a Python interface to the Telegram Bot API
4+
# Copyright (C) 2015 Leandro Toledo de Souza <devs@python-telegram-bot.org>
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser Public License
17+
# along with this program. If not, see [http://www.gnu.org/licenses/].
18+
19+
"""
20+
This module contains the class JobQueue
21+
"""
22+
23+
import logging
24+
import time
25+
from threading import Lock
26+
27+
try:
28+
from queue import Queue, PriorityQueue
29+
except ImportError:
30+
from Queue import Queue, PriorityQueue
31+
32+
33+
class JobQueue(object):
34+
"""This class allows you to periodically perform tasks with the bot.
35+
36+
Attributes:
37+
tick_interval (float):
38+
queue (PriorityQueue):
39+
bot (Bot):
40+
running (bool):
41+
42+
Args:
43+
bot (Bot): The bot instance that should be passed to the jobs
44+
45+
Keyword Args:
46+
tick_interval (Optional[float]): The interval this queue should check
47+
the newest task in seconds. Defaults to 1.0
48+
"""
49+
50+
def __init__(self, bot, tick_interval=1.0):
51+
self.tick_interval = tick_interval
52+
self.queue = PriorityQueue()
53+
self.bot = bot
54+
self.logger = logging.getLogger(__name__)
55+
self.__lock = Lock()
56+
self.running = False
57+
58+
def put(self, run, interval, next_t=None):
59+
"""
60+
Queue a new job.
61+
62+
Args:
63+
run (function): A function that takes the parameter `bot`
64+
interval (float): The interval in seconds in which `run` should be
65+
executed
66+
next_t (Optional[float]): Time in seconds in which run should be
67+
executed first. Defaults to `interval`
68+
"""
69+
name = run.__name__
70+
71+
job = JobQueue.Job()
72+
job.run = run
73+
job.interval = interval
74+
job.name = name
75+
76+
if next_t is None:
77+
next_t = interval
78+
79+
self.logger.debug("Putting a {} with t={}".format(
80+
job.name, next_t))
81+
self.queue.put((next_t, job))
82+
83+
def tick(self):
84+
"""
85+
Run all jobs that are due and re-enqueue them with their interval
86+
"""
87+
now = time.time()
88+
89+
self.logger.debug("Ticking jobs with t={}".format(now))
90+
while not self.queue.empty():
91+
t, j = self.queue.queue[0]
92+
self.logger.debug("Peeked at {} with t={}".format(
93+
j.name, t))
94+
95+
if t < now:
96+
self.queue.get()
97+
self.logger.debug("About time! running")
98+
j.run()
99+
self.put(j, now + j.interval)
100+
continue
101+
102+
self.logger.debug("Next task isn't due yet. Finished!")
103+
break
104+
105+
def start(self):
106+
"""
107+
Thread target of thread 'job_queue'. Runs in background and performs
108+
ticks on the job queue.
109+
"""
110+
self.__lock.acquire()
111+
if not self.running:
112+
self.running = True
113+
self.__lock.release()
114+
while self.running:
115+
self.tick()
116+
time.sleep(self.tick_interval)
117+
else:
118+
self.__lock.release()
119+
120+
def stop(self):
121+
"""
122+
Stops the thread
123+
"""
124+
with self.__lock:
125+
self.running = False
126+
127+
class Job(object):
128+
""" Inner class that represents a job """
129+
interval = None
130+
name = None
131+
132+
def run(self):
133+
pass
134+
135+
def __lt__(self, other):
136+
return False

0 commit comments

Comments
 (0)
X Tutup