|
| 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