-
Notifications
You must be signed in to change notification settings - Fork 0
Add APScheduler for verse cleanup and implement expiration logic #860
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
Changes from all commits
Commits
Show all changes
3 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import logging | ||
|
|
||
| from apscheduler.schedulers.asyncio import AsyncIOScheduler | ||
| from apscheduler.triggers.cron import CronTrigger | ||
|
|
||
| from pecha_api.config import get_int | ||
| from pecha_api.verse_of_day.verse_of_day_service import cleanup_expired_verses_of_day | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| scheduler = AsyncIOScheduler() | ||
|
|
||
|
|
||
| def setup_scheduler() -> None: | ||
| expiry_days = get_int("VERSE_OF_DAY_EXPIRY_DAYS") | ||
| if expiry_days < 1: | ||
| raise ValueError( | ||
| f"VERSE_OF_DAY_EXPIRY_DAYS must be a positive integer, got {expiry_days}" | ||
| ) | ||
| scheduler.add_job( | ||
| cleanup_expired_verses_of_day, | ||
| CronTrigger(hour=0, minute=0), | ||
| args=[expiry_days], | ||
| id="cleanup_expired_verses_of_day", | ||
| name="Cleanup expired verses of the day", | ||
| replace_existing=True, | ||
| ) | ||
| if not scheduler.running: | ||
| scheduler.start() | ||
| logger.info( | ||
| "Scheduler started: cleaning verses of the day older than %s day(s) daily at midnight", | ||
| expiry_days, | ||
| ) | ||
|
|
||
|
|
||
| def shutdown_scheduler() -> None: | ||
| if scheduler.running: | ||
| scheduler.shutdown(wait=False) | ||
| logger.info("Scheduler shut down") | ||
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from pecha_api.scheduler import setup_scheduler, shutdown_scheduler | ||
|
|
||
|
|
||
| def test_setup_scheduler_rejects_non_positive_retention(): | ||
| with patch("pecha_api.scheduler.get_int", return_value=0), patch( | ||
| "pecha_api.scheduler.scheduler" | ||
| ) as mock_scheduler: | ||
| mock_scheduler.running = False | ||
|
|
||
| with pytest.raises(ValueError, match="positive integer"): | ||
| setup_scheduler() | ||
|
|
||
| mock_scheduler.add_job.assert_not_called() | ||
| mock_scheduler.start.assert_not_called() | ||
|
|
||
|
|
||
| def test_setup_scheduler_rejects_negative_retention(): | ||
| with patch("pecha_api.scheduler.get_int", return_value=-7), patch( | ||
| "pecha_api.scheduler.scheduler" | ||
| ) as mock_scheduler: | ||
| mock_scheduler.running = False | ||
|
|
||
| with pytest.raises(ValueError, match="positive integer"): | ||
| setup_scheduler() | ||
|
|
||
| mock_scheduler.add_job.assert_not_called() | ||
|
|
||
|
|
||
| def test_setup_scheduler_registers_cleanup_job(): | ||
| with patch("pecha_api.scheduler.get_int", return_value=7), patch( | ||
| "pecha_api.scheduler.scheduler" | ||
| ) as mock_scheduler, patch( | ||
| "pecha_api.scheduler.CronTrigger" | ||
| ) as mock_cron_trigger: | ||
| mock_scheduler.running = False | ||
| mock_trigger = MagicMock() | ||
| mock_cron_trigger.return_value = mock_trigger | ||
|
|
||
| setup_scheduler() | ||
|
|
||
| mock_scheduler.add_job.assert_called_once() | ||
| call_kwargs = mock_scheduler.add_job.call_args | ||
| assert call_kwargs.kwargs["args"] == [7] | ||
| assert call_kwargs.kwargs["id"] == "cleanup_expired_verses_of_day" | ||
| mock_scheduler.start.assert_called_once() | ||
|
|
||
|
|
||
| def test_shutdown_scheduler_when_running(): | ||
| with patch("pecha_api.scheduler.scheduler") as mock_scheduler: | ||
| mock_scheduler.running = True | ||
|
|
||
| shutdown_scheduler() | ||
|
|
||
| mock_scheduler.shutdown.assert_called_once_with(wait=False) | ||
|
|
||
|
|
||
| def test_shutdown_scheduler_when_not_running(): | ||
| with patch("pecha_api.scheduler.scheduler") as mock_scheduler: | ||
| mock_scheduler.running = False | ||
|
|
||
| shutdown_scheduler() | ||
|
|
||
| mock_scheduler.shutdown.assert_not_called() |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.