Get jobs by name (#1011)

This commit is contained in:
Noam Meltzer 2018-02-19 10:36:40 +02:00 committed by GitHub
parent 0bed087542
commit 746ae0caf3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View file

@ -1,5 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python
# flake8: noqa E501
# #
# A library that provides a Python interface to the Telegram Bot API # A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2018 # Copyright (C) 2015-2018
@ -73,7 +72,8 @@ class JobQueue(object):
Args: Args:
job (:class:`telegram.ext.Job`): The ``Job`` instance representing the new job. job (:class:`telegram.ext.Job`): The ``Job`` instance representing the new job.
next_t (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta` | :obj:`datetime.datetime` | :obj:`datetime.time`, optional): next_t (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta` | \
:obj:`datetime.datetime` | :obj:`datetime.time`, optional):
Time in or at which the job should run for the first time. This parameter will Time in or at which the job should run for the first time. This parameter will
be interpreted depending on its type. be interpreted depending on its type.
@ -131,7 +131,8 @@ class JobQueue(object):
job. It should take ``bot, job`` as parameters, where ``job`` is the job. It should take ``bot, job`` as parameters, where ``job`` is the
:class:`telegram.ext.Job` instance. It can be used to access it's :class:`telegram.ext.Job` instance. It can be used to access it's
``job.context`` or change it to a repeating job. ``job.context`` or change it to a repeating job.
when (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta` | :obj:`datetime.datetime` | :obj:`datetime.time`): when (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta` | \
:obj:`datetime.datetime` | :obj:`datetime.time`):
Time in or at which the job should run. This parameter will be interpreted Time in or at which the job should run. This parameter will be interpreted
depending on its type. depending on its type.
@ -170,7 +171,8 @@ class JobQueue(object):
interval (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta`): The interval in which interval (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta`): The interval in which
the job will run. If it is an :obj:`int` or a :obj:`float`, it will be interpreted the job will run. If it is an :obj:`int` or a :obj:`float`, it will be interpreted
as seconds. as seconds.
first (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta` | :obj:`datetime.datetime` | :obj:`datetime.time`, optional): first (:obj:`int` | :obj:`float` | :obj:`datetime.timedelta` | \
:obj:`datetime.datetime` | :obj:`datetime.time`, optional):
Time in or at which the job should run. This parameter will be interpreted Time in or at which the job should run. This parameter will be interpreted
depending on its type. depending on its type.
@ -342,6 +344,11 @@ class JobQueue(object):
with self._queue.mutex: with self._queue.mutex:
return tuple(job[1] for job in self._queue.queue if job) return tuple(job[1] for job in self._queue.queue if job)
def get_jobs_by_name(self, name):
"""Returns a tuple of jobs with the given name that are currently in the ``JobQueue``"""
with self._queue.mutex:
return tuple(job[1] for job in self._queue.queue if job and job[1].name == name)
class Job(object): class Job(object):
"""This class encapsulates a Job. """This class encapsulates a Job.

View file

@ -238,3 +238,12 @@ class TestJobQueue(object):
j.days = ('mon', 'wed') j.days = ('mon', 'wed')
with pytest.raises(ValueError, match='from 0 up to and'): with pytest.raises(ValueError, match='from 0 up to and'):
j.days = (0, 6, 12, 14) j.days = (0, 6, 12, 14)
def test_get_jobs(self, job_queue):
job1 = job_queue.run_once(self.job_run_once, 10, name='name1')
job2 = job_queue.run_once(self.job_run_once, 10, name='name1')
job3 = job_queue.run_once(self.job_run_once, 10, name='name2')
assert job_queue.jobs() == (job1, job2, job3)
assert job_queue.get_jobs_by_name('name1') == (job1, job2)
assert job_queue.get_jobs_by_name('name2') == (job3,)