-
Notifications
You must be signed in to change notification settings - Fork 43
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
add timezone to cron scheduler #204
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,12 @@ | |
import time | ||
import typing as t | ||
import unittest | ||
from datetime import datetime, timedelta, timezone | ||
from unittest import mock | ||
|
||
from aiohttp import web | ||
from aiohttp.test_utils import AioHTTPTestCase | ||
from time_machine import travel | ||
|
||
from saq.job import CronJob, Job, Status, ACTIVE_STATUSES | ||
from saq.queue import Queue | ||
|
@@ -368,35 +370,40 @@ async def test_schedule(self, mock_time: MagicMock) -> None: | |
self.assertEqual(job.result, 1) | ||
|
||
@mock.patch("saq.worker.logger") | ||
@mock.patch("saq.utils.time") | ||
async def test_cron(self, mock_time: MagicMock, mock_logger: MagicMock) -> None: | ||
async def test_cron(self, mock_logger: MagicMock) -> None: | ||
with self.assertRaises(ValueError): | ||
Worker( | ||
self.queue, | ||
functions=functions, | ||
cron_jobs=[CronJob(cron, cron="x")], | ||
cron_jobs=[CronJob(sleeper, cron="x")], | ||
) | ||
|
||
mock_time.time.return_value = 1 | ||
worker = Worker( | ||
self.queue, | ||
functions=functions, | ||
cron_jobs=[CronJob(cron, cron="* * * * *", kwargs={"param": 1})], | ||
) | ||
self.assertEqual(await self.queue.count("queued"), 0) | ||
self.assertEqual(await self.queue.count("incomplete"), 0) | ||
await worker.schedule() | ||
self.assertEqual(await self.queue.count("queued"), 0) | ||
self.assertEqual(await self.queue.count("incomplete"), 1) | ||
|
||
mock_time.time.return_value = 60 | ||
await asyncio.sleep(1) | ||
await worker.schedule() | ||
self.assertEqual(await self.queue.count("queued"), 1) | ||
self.assertEqual(await self.queue.count("incomplete"), 1) | ||
# Remove if statement when schedule is implemented for Postgres queue | ||
if isinstance(self.queue, RedisQueue): | ||
mock_logger.info.assert_any_call("Scheduled %s", ["saq:job:default:cron:cron"]) | ||
with travel(datetime(2025, 1, 1, 0, 0, 1, tzinfo=timezone.utc), tick=True) as traveller: | ||
worker = Worker( | ||
self.queue, | ||
functions=functions, | ||
cron_jobs=[CronJob(sleeper, cron="* 12 * * *", kwargs={"sleep": 3})], | ||
cron_tz=timezone(offset=timedelta(hours=-3)), # 3 hours behind (UTC-3) | ||
) | ||
await worker.queue.connect() | ||
self.assertEqual(await self.queue.count("incomplete"), 0) | ||
tobymao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(await self.queue.count("queued"), 0) | ||
asyncio.create_task(worker.start()) | ||
await asyncio.sleep(1) | ||
self.assertEqual(await self.queue.count("incomplete"), 1) | ||
self.assertEqual(await self.queue.count("queued"), 0) | ||
|
||
traveller.move_to(datetime(2025, 1, 1, 12, 0, 0, tzinfo=timezone.utc)) # noon UTC | ||
await asyncio.sleep(2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any way to make these tests faster? can you sleep for less time? perhaps reduce the cron timer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll play around a bit add see |
||
self.assertEqual(await self.queue.count("active"), 0) | ||
|
||
traveller.move_to(datetime(2025, 1, 1, 15, 0, 0, tzinfo=timezone.utc)) # noon in tz | ||
await asyncio.sleep(2) | ||
self.assertEqual(await self.queue.count("active"), 1) | ||
|
||
# Remove if statement when schedule is implemented for Postgres queue | ||
if isinstance(self.queue, RedisQueue): | ||
mock_logger.info.assert_any_call("Scheduled %s", ["saq:job:default:cron:sleeper"]) | ||
|
||
@mock.patch("saq.worker.logger") | ||
async def test_abort(self, mock_logger: MagicMock) -> None: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this function changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See line 404, one of the checks is if the job is running which requires it to take some time.