From d9fd446cf71407249eea167dc532df712b079d18 Mon Sep 17 00:00:00 2001 From: Ankit Raibole Date: Thu, 19 Aug 2021 19:37:10 +0530 Subject: [PATCH 1/5] jobqueue end of month --- telegram/ext/jobqueue.py | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/telegram/ext/jobqueue.py b/telegram/ext/jobqueue.py index da2dea4f210..810c58ad7af 100644 --- a/telegram/ext/jobqueue.py +++ b/telegram/ext/jobqueue.py @@ -348,42 +348,33 @@ def run_monthly( name = name or callback.__name__ job = Job(callback, context, name, self) - if day_is_strict: + # if day == -1 the run on last day of the month + if day == -1: j = self.scheduler.add_job( callback, trigger='cron', args=self._build_args(job), name=name, - day=day, + day='last', hour=when.hour, minute=when.minute, second=when.second, timezone=when.tzinfo or self.scheduler.timezone, **job_kwargs, ) + # else run on the day as provided 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 + 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, ) job.job = j From fc7d1e8b859c440870cdad4fe71f347852cf28b0 Mon Sep 17 00:00:00 2001 From: Ankit Raibole Date: Sun, 22 Aug 2021 12:53:44 +0530 Subject: [PATCH 2/5] jobqueue end of month --- telegram/ext/jobqueue.py | 43 ++++++++++++++-------------------------- tests/test_jobqueue.py | 6 +++--- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/telegram/ext/jobqueue.py b/telegram/ext/jobqueue.py index 810c58ad7af..5a933854933 100644 --- a/telegram/ext/jobqueue.py +++ b/telegram/ext/jobqueue.py @@ -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 @@ -311,7 +309,6 @@ 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. @@ -349,34 +346,24 @@ def run_monthly( job = Job(callback, context, name, self) # if day == -1 the run on last day of the month - if day == -1: - j = self.scheduler.add_job( - callback, - trigger='cron', - args=self._build_args(job), - name=name, - day='last', - hour=when.hour, - minute=when.minute, - second=when.second, - timezone=when.tzinfo or self.scheduler.timezone, - **job_kwargs, - ) # else run on the day as provided + if day == -1: + _day = 'last' else: - 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, - ) + _day = day + 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, + ) job.job = j return job diff --git a/tests/test_jobqueue.py b/tests/test_jobqueue.py index 2851827dc63..31bc0cd12c0 100644 --- a/tests/test_jobqueue.py +++ b/tests/test_jobqueue.py @@ -227,7 +227,7 @@ def test_longer_first(self, job_queue): sleep(0.015) - assert self.result == 1 + assert self.result == 0 def test_error(self, job_queue): job_queue.run_repeating(self.job_with_exception, 0.01) @@ -241,7 +241,7 @@ def test_in_updater(self, bot): try: u.job_queue.run_repeating(self.job_run_once, 0.02) sleep(0.03) - assert self.result == 1 + assert self.result == 0 u.stop() sleep(1) assert self.result == 1 @@ -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) From 9de27a18533fb86c3262f99fe5c9b86321adb07e Mon Sep 17 00:00:00 2001 From: Ankit Raibole Date: Wed, 25 Aug 2021 21:06:04 +0530 Subject: [PATCH 3/5] jobqueue end of month --- telegram/ext/jobqueue.py | 8 +++----- tests/test_jobqueue.py | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/telegram/ext/jobqueue.py b/telegram/ext/jobqueue.py index 5a933854933..7e1b92a8feb 100644 --- a/telegram/ext/jobqueue.py +++ b/telegram/ext/jobqueue.py @@ -347,17 +347,15 @@ def run_monthly( # if day == -1 the run on last day of the month # else run on the day as provided - if day == -1: - _day = 'last' - else: - _day = day + + day = 'last' if day == -1 else day j = self.scheduler.add_job( callback, trigger='cron', args=self._build_args(job), name=name, - day=_day, + day=day, hour=when.hour, minute=when.minute, second=when.second, diff --git a/tests/test_jobqueue.py b/tests/test_jobqueue.py index 31bc0cd12c0..a896a157a27 100644 --- a/tests/test_jobqueue.py +++ b/tests/test_jobqueue.py @@ -227,7 +227,7 @@ def test_longer_first(self, job_queue): sleep(0.015) - assert self.result == 0 + assert self.result == 1 def test_error(self, job_queue): job_queue.run_repeating(self.job_with_exception, 0.01) @@ -241,7 +241,7 @@ def test_in_updater(self, bot): try: u.job_queue.run_repeating(self.job_run_once, 0.02) sleep(0.03) - assert self.result == 0 + assert self.result == 1 u.stop() sleep(1) assert self.result == 1 From 447a22cce0e585191a73c959362e6241a625eca3 Mon Sep 17 00:00:00 2001 From: Ankit Raibole Date: Thu, 26 Aug 2021 15:58:49 +0530 Subject: [PATCH 4/5] changes as mentioned in comment --- telegram/ext/jobqueue.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/telegram/ext/jobqueue.py b/telegram/ext/jobqueue.py index 7e1b92a8feb..f4f6b8121d2 100644 --- a/telegram/ext/jobqueue.py +++ b/telegram/ext/jobqueue.py @@ -324,13 +324,11 @@ 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 or -1 for end of 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()``. @@ -345,17 +343,12 @@ def run_monthly( name = name or callback.__name__ job = Job(callback, context, name, self) - # if day == -1 the run on last day of the month - # else run on the day as provided - - day = 'last' if day == -1 else day - j = self.scheduler.add_job( callback, trigger='cron', args=self._build_args(job), name=name, - day=day, + day='last' if day == -1 else day, hour=when.hour, minute=when.minute, second=when.second, From 3b219721ef403800f3f4991ffa0b4e8362396237 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Thu, 26 Aug 2021 20:45:24 +0200 Subject: [PATCH 5/5] Fine tune docs --- telegram/ext/jobqueue.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/telegram/ext/jobqueue.py b/telegram/ext/jobqueue.py index f4f6b8121d2..a7cdbb6ca43 100644 --- a/telegram/ext/jobqueue.py +++ b/telegram/ext/jobqueue.py @@ -313,6 +313,10 @@ def run_monthly( ) -> '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: @@ -324,7 +328,9 @@ 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 or -1 for end of month. + 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