Skip to content

jobqueue monthly #2634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 19 additions & 44 deletions telegram/ext/jobqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import pytz
from apscheduler.events import EVENT_JOB_ERROR, EVENT_JOB_EXECUTED, JobEvent
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.combining import OrTrigger
from apscheduler.triggers.cron import CronTrigger
from apscheduler.job import Job as APSJob

from telegram.ext.callbackcontext import CallbackContext
Expand Down Expand Up @@ -311,11 +309,14 @@ def run_monthly(
day: int,
context: object = None,
name: str = None,
day_is_strict: bool = True,
job_kwargs: JSONDict = None,
) -> 'Job':
"""Creates a new ``Job`` that runs on a monthly basis and adds it to the queue.

.. versionchanged:: 14.0
The ``day_is_strict`` argument was removed. Instead one can now pass -1 to the ``day``
parameter to have the job run on the last day of the month.

Args:
callback (:obj:`callable`): The callback function that should be executed by the new
job. Callback signature for context based API:
Expand All @@ -327,13 +328,13 @@ def run_monthly(
when (:obj:`datetime.time`): Time of day at which the job should run. If the timezone
(``when.tzinfo``) is :obj:`None`, the default timezone of the bot will be used.
day (:obj:`int`): Defines the day of the month whereby the job would run. It should
be within the range of 1 and 31, inclusive.
be within the range of 1 and 31, inclusive. If a month has fewer days than this
number, the job will not run in this month. Passing -1 leads to the job running on
the last day of the month.
context (:obj:`object`, optional): Additional data needed for the callback function.
Can be accessed through ``job.context`` in the callback. Defaults to :obj:`None`.
name (:obj:`str`, optional): The name of the new job. Defaults to
``callback.__name__``.
day_is_strict (:obj:`bool`, optional): If :obj:`False` and day > month.days, will pick
the last day in the month. Defaults to :obj:`True`.
job_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to pass to the
``scheduler.add_job()``.

Expand All @@ -348,44 +349,18 @@ def run_monthly(
name = name or callback.__name__
job = Job(callback, context, name, self)

if day_is_strict:
j = self.scheduler.add_job(
callback,
trigger='cron',
args=self._build_args(job),
name=name,
day=day,
hour=when.hour,
minute=when.minute,
second=when.second,
timezone=when.tzinfo or self.scheduler.timezone,
**job_kwargs,
)
else:
trigger = OrTrigger(
[
CronTrigger(
day=day,
hour=when.hour,
minute=when.minute,
second=when.second,
timezone=when.tzinfo,
**job_kwargs,
),
CronTrigger(
day='last',
hour=when.hour,
minute=when.minute,
second=when.second,
timezone=when.tzinfo or self.scheduler.timezone,
**job_kwargs,
),
]
)
j = self.scheduler.add_job(
callback, trigger=trigger, args=self._build_args(job), name=name, **job_kwargs
)

j = self.scheduler.add_job(
callback,
trigger='cron',
args=self._build_args(job),
name=name,
day='last' if day == -1 else day,
hour=when.hour,
minute=when.minute,
second=when.second,
timezone=when.tzinfo or self.scheduler.timezone,
**job_kwargs,
)
job.job = j
return job

Expand Down
2 changes: 1 addition & 1 deletion tests/test_jobqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def test_run_monthly_non_strict_day(self, job_queue, timezone):
)
expected_reschedule_time = expected_reschedule_time.timestamp()

job_queue.run_monthly(self.job_run_once, time_of_day, 31, day_is_strict=False)
job_queue.run_monthly(self.job_run_once, time_of_day, -1)
scheduled_time = job_queue.jobs()[0].next_t.timestamp()
assert scheduled_time == pytest.approx(expected_reschedule_time)

Expand Down