Skip to content
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
16 changes: 14 additions & 2 deletions telegram/ext/_jobqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from apscheduler.schedulers.asyncio import AsyncIOScheduler

from telegram._utils.types import JSONDict
from telegram._utils.warnings import warn
from telegram.ext._extbot import ExtBot
from telegram.ext._utils.types import JobCallback

Expand All @@ -50,6 +51,7 @@ class JobQueue:
"""

__slots__ = ("_application", "scheduler", "_executor")
_CRON_MAPPING = ("sun", "mon", "tue", "wed", "thu", "fri", "sat")

def __init__(self) -> None:
self._application: "Optional[weakref.ReferenceType[Application]]" = None
Expand Down Expand Up @@ -419,7 +421,11 @@ async def callback(context: CallbackContext)
(:obj:`datetime.time.tzinfo`) is :obj:`None`, the default timezone of the bot will
be used.
days (Tuple[:obj:`int`], optional): Defines on which days of the week the job should
run (where ``0-6`` correspond to monday - sunday). Defaults to ``EVERY_DAY``
run (where ``0-6`` correspond to sunday - saturday). By default, the job will run
every day.

.. versionchanged:: 20.0
Changed day of the week mapping of 0-6 from monday-sunday to sunday-saturday.
data (:obj:`object`, optional): Additional data needed for the callback function.
Can be accessed through :attr:`Job.data` in the callback. Defaults to
:obj:`None`.
Expand Down Expand Up @@ -447,6 +453,12 @@ async def callback(context: CallbackContext)
queue.

"""
# TODO: After v20.0, we should remove the this warning.
warn(
"Prior to v20.0 the `days` parameter was not aligned to that of cron's weekday scheme."
"We recommend double checking if the passed value is correct.",
stacklevel=2,
)
Comment on lines +457 to +461

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a comment here, reminding us that we remove this in the next major version?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this enough?

        # TODO: After v20.0, we should remove the this warning.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be enough IMO, yes :)

if not job_kwargs:
job_kwargs = {}

Expand All @@ -458,7 +470,7 @@ async def callback(context: CallbackContext)
name=name,
args=(self.application,),
trigger="cron",
day_of_week=",".join([str(d) for d in days]),
day_of_week=",".join([self._CRON_MAPPING[d] for d in days]),
hour=time.hour,
minute=time.minute,
second=time.second,
Expand Down
30 changes: 29 additions & 1 deletion tests/test_jobqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,11 @@ async def test_time_unit_dt_time_tomorrow(self, job_queue):
scheduled_time = job_queue.jobs()[0].next_t.timestamp()
assert scheduled_time == pytest.approx(expected_time)

async def test_run_daily(self, job_queue):
async def test_run_daily(self, job_queue, recwarn):
expected_warning = (
"Prior to v20.0 the `days` parameter was not aligned to that of cron's weekday scheme."
"We recommend double checking if the passed value is correct."
)
delta, now = 1, dtm.datetime.now(pytz.utc)
time_of_day = (now + dtm.timedelta(seconds=delta)).time()
expected_reschedule_time = (now + dtm.timedelta(seconds=delta, days=1)).timestamp()
Expand All @@ -313,6 +317,30 @@ async def test_run_daily(self, job_queue):
assert self.result == 1
scheduled_time = job_queue.jobs()[0].next_t.timestamp()
assert scheduled_time == pytest.approx(expected_reschedule_time)
assert len(recwarn) == 1
assert str(recwarn[0].message) == expected_warning
Comment thread
Bibo-Joshi marked this conversation as resolved.
assert recwarn[0].filename == __file__, "wrong stacklevel"

@pytest.mark.parametrize("weekday", (0, 1, 2, 3, 4, 5, 6))
async def test_run_daily_days_of_week(self, job_queue, recwarn, weekday):
expected_warning = (
"Prior to v20.0 the `days` parameter was not aligned to that of cron's weekday scheme."
"We recommend double checking if the passed value is correct."
)
delta, now = 1, dtm.datetime.now(pytz.utc)
time_of_day = (now + dtm.timedelta(seconds=delta)).time()
# offset in days until next weekday
offset = (weekday + 6 - now.weekday()) % 7
offset = offset if offset > 0 else 7
expected_reschedule_time = (now + dtm.timedelta(seconds=delta, days=offset)).timestamp()

job_queue.run_daily(self.job_run_once, time_of_day, days=[weekday])
await asyncio.sleep(delta + 0.1)
scheduled_time = job_queue.jobs()[0].next_t.timestamp()
assert scheduled_time == pytest.approx(expected_reschedule_time)
assert len(recwarn) == 1
assert str(recwarn[0].message) == expected_warning
assert recwarn[0].filename == __file__, "wrong stacklevel"

async def test_run_monthly(self, job_queue, timezone):
delta, now = 1, dtm.datetime.now(timezone)
Expand Down