From 4d493aff16f13bd474925baf2a3f7c62dde40f80 Mon Sep 17 00:00:00 2001 From: Ankit Raibole <46680697+iota-008@users.noreply.github.com> Date: Fri, 27 Aug 2021 00:29:23 +0530 Subject: [PATCH] Remove day_is_strict argument of JobQueue.run_monthly (#2634) Co-authored-by: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> --- telegram/ext/jobqueue.py | 63 ++++++++++++---------------------------- tests/test_jobqueue.py | 2 +- 2 files changed, 20 insertions(+), 45 deletions(-) diff --git a/telegram/ext/jobqueue.py b/telegram/ext/jobqueue.py index b27f2c327..7f897f4e4 100644 --- a/telegram/ext/jobqueue.py +++ b/telegram/ext/jobqueue.py @@ -25,8 +25,6 @@ from typing import TYPE_CHECKING, Callable, List, Optional, Tuple, Union, cast, 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 @@ -307,11 +305,14 @@ class JobQueue: 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: @@ -323,13 +324,13 @@ class JobQueue: 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()``. @@ -344,44 +345,18 @@ class JobQueue: 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 diff --git a/tests/test_jobqueue.py b/tests/test_jobqueue.py index a2d0cbd56..74f32717b 100644 --- a/tests/test_jobqueue.py +++ b/tests/test_jobqueue.py @@ -354,7 +354,7 @@ class TestJobQueue: ) 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)