-
Notifications
You must be signed in to change notification settings - Fork 55
Feature/task scheduling decorators #84
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
base: main
Are you sure you want to change the base?
Feature/task scheduling decorators #84
Conversation
…ts-tests.txt Add APScheduler, pytz, six, and tzlocal dependencies to the requirements files.
This commit adds a new file `apscheduler.py` to the `nest/core/apscheduler` directory. The file contains the implementation of an instance of the `BackgroundScheduler` class from the APScheduler library. Additionally, a new file `scheduler.py` is added to the `nest/core/decorators` directory. This file contains decorators `Cron` and `Interval` for scheduling functions to run at specific times or intervals.
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.
I found a few minor issues in this change, PTAL:
Tip
- Found some suggestions on how to write the code in a shorter and more concise manner
- Minor issues around naming, typos etc
Reviewer workflows in this change
- Breaking changes
- Typos and ambiguous identifiers
- Code hygiene
- Assertion coverage
- Type correctness
- Concise code
- Code Dedup and Conventions
Bazzy your AI Code Review Assistant, can explain and help resolve these issues.
EVERY_SECOND = CronTrigger(second="*") | ||
EVERY_5_SECONDS = CronTrigger(second="*/5") | ||
EVERY_10_SECONDS = CronTrigger(second="*/10") | ||
EVERY_30_SECONDS = CronTrigger(second="*/30") | ||
EVERY_MINUTE = CronTrigger(minute="*/1") | ||
EVERY_5_MINUTES = CronTrigger(minute="*/5") | ||
EVERY_10_MINUTES = CronTrigger(minute="*/10") | ||
EVERY_30_MINUTES = CronTrigger(minute="*/30") | ||
EVERY_HOUR = CronTrigger(minute=0, hour="0-23/1") | ||
EVERY_2_HOURS = CronTrigger(minute=0, hour="0-23/2") | ||
EVERY_3_HOURS = CronTrigger(minute=0, hour="0-23/3") | ||
EVERY_4_HOURS = CronTrigger(minute=0, hour="0-23/4") | ||
EVERY_5_HOURS = CronTrigger(minute=0, hour="0-23/5") | ||
EVERY_6_HOURS = CronTrigger(minute=0, hour="0-23/6") | ||
EVERY_7_HOURS = CronTrigger(minute=0, hour="0-23/7") | ||
EVERY_8_HOURS = CronTrigger(minute=0, hour="0-23/8") | ||
EVERY_9_HOURS = CronTrigger(minute=0, hour="0-23/9") | ||
EVERY_10_HOURS = CronTrigger(minute=0, hour="0-23/10") | ||
EVERY_11_HOURS = CronTrigger(minute=0, hour="0-23/11") | ||
EVERY_12_HOURS = CronTrigger(minute=0, hour="0-23/12") | ||
EVERY_DAY_AT_1AM = CronTrigger(minute=0, hour=1) | ||
EVERY_DAY_AT_2AM = CronTrigger(minute=0, hour=2) | ||
EVERY_DAY_AT_3AM = CronTrigger(minute=0, hour=3) | ||
EVERY_DAY_AT_4AM = CronTrigger(minute=0, hour=4) | ||
EVERY_DAY_AT_5AM = CronTrigger(minute=0, hour=5) | ||
EVERY_DAY_AT_6AM = CronTrigger(minute=0, hour=6) | ||
EVERY_DAY_AT_7AM = CronTrigger(minute=0, hour=7) | ||
EVERY_DAY_AT_8AM = CronTrigger(minute=0, hour=8) | ||
EVERY_DAY_AT_9AM = CronTrigger(minute=0, hour=9) | ||
EVERY_DAY_AT_10AM = CronTrigger(minute=0, hour=10) | ||
EVERY_DAY_AT_11AM = CronTrigger(minute=0, hour=11) | ||
EVERY_DAY_AT_NOON = CronTrigger(minute=0, hour=12) | ||
EVERY_DAY_AT_1PM = CronTrigger(minute=0, hour=13) | ||
EVERY_DAY_AT_2PM = CronTrigger(minute=0, hour=14) | ||
EVERY_DAY_AT_3PM = CronTrigger(minute=0, hour=15) | ||
EVERY_DAY_AT_4PM = CronTrigger(minute=0, hour=16) | ||
EVERY_DAY_AT_5PM = CronTrigger(minute=0, hour=17) | ||
EVERY_DAY_AT_6PM = CronTrigger(minute=0, hour=18) | ||
EVERY_DAY_AT_7PM = CronTrigger(minute=0, hour=19) | ||
EVERY_DAY_AT_8PM = CronTrigger(minute=0, hour=20) | ||
EVERY_DAY_AT_9PM = CronTrigger(minute=0, hour=21) | ||
EVERY_DAY_AT_10PM = CronTrigger(minute=0, hour=22) | ||
EVERY_DAY_AT_11PM = CronTrigger(minute=0, hour=23) | ||
EVERY_DAY_AT_MIDNIGHT = CronTrigger(minute=0, hour=0) | ||
EVERY_WEEK = CronTrigger(minute=0, hour=0, day_of_week=0) | ||
EVERY_WEEKDAY = CronTrigger(minute=0, hour=0, day_of_week="1-5") | ||
EVERY_WEEKEND = CronTrigger(minute=0, hour=0, day_of_week="6,0") | ||
EVERY_1ST_DAY_OF_MONTH_AT_MIDNIGHT = CronTrigger(minute=0, hour=0, day=1) | ||
EVERY_1ST_DAY_OF_MONTH_AT_NOON = CronTrigger(minute=0, hour=12, day=1) | ||
EVERY_2ND_HOUR = CronTrigger(minute=0, hour="*/2") | ||
EVERY_2ND_HOUR_FROM_1AM_THROUGH_11PM = CronTrigger(minute=0, hour="1-23/2") | ||
EVERY_2ND_MONTH = CronTrigger(minute=0, hour=0, day=1, month="*/2") | ||
EVERY_QUARTER = CronTrigger(minute=0, hour=0, day=1, month="*/3") | ||
EVERY_6_MONTHS = CronTrigger(minute=0, hour=0, day=1, month="*/6") | ||
EVERY_YEAR = CronTrigger(minute=0, hour=0, day=1, month=1) |
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.
The CronExpression enum contains many repetitive entries that could be generated programmatically. This would make the code more concise and easier to maintain.
Finding type: Conciseness
CRON = 'cron', | ||
DATE = 'date' | ||
INTERVAL = 'interval', |
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.
There are trailing commas after 'cron' and 'interval' in the SchedulerTypes enum, which is inconsistent with the 'date' entry.
Finding type: Naming and Typos
def Cron(expression: CronExpression = CronExpression.EVERY_MINUTE) -> Callable: | ||
""" | ||
Decorator that schedules a function to run at a specific time. | ||
|
||
Args: | ||
expression (CronExpression): A cron expression. | ||
|
||
Returns: | ||
function: The decorated function. | ||
""" | ||
def decorated(func: Callable) -> Callable: | ||
def wrapper(*args, **kwargs): | ||
""" | ||
Wrapper function that schedules the function to run at a specific time. | ||
""" | ||
try: | ||
if not isinstance(expression, CronExpression): | ||
raise ValueError("Invalid cron expression.") | ||
scheduler.add_job( | ||
func, | ||
trigger = expression.value, | ||
id = func.__name__, | ||
) | ||
except Exception as e: | ||
raise ValueError(f"Invalid cron expression: {e}") | ||
|
||
|
||
return wrapper | ||
|
||
return decorated |
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.
The Cron decorator function contains nested try-except blocks and conditionals that can be simplified. The type checking can be moved to a separate function for better readability.
Finding type: Conciseness
"APScheduler >= "3.10.4", | ||
"pytz >= "2024.2", | ||
"six >= "1.16.0", |
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.
There are typos in the version specifications for APScheduler, pytz, and tzlocal. The quotation marks are incorrectly placed.
Finding type: Naming and Typos
User description
Pull Request Description
This pull request introduces task scheduling functionality using APScheduler and includes the following updates:
Update requirements files:
requirements-dev.txt
,requirements-release.txt
, andrequirements-tests.txt
are updated to include the following dependencies:Add BackgroundScheduler instance and task scheduling decorators:
apscheduler.py
in thenest/core/apscheduler
directory, which implements an instance of theBackgroundScheduler
from the APScheduler library.scheduler.py
to thenest/core/decorators
directory containing two decorators:@Cron
: For scheduling tasks using cron expressions.@Interval
: For scheduling tasks to run at fixed intervals.Add enums for cron expressions and scheduler types:
Update pyproject.toml:
pyproject.toml
, ensuring the project is compatible with the new scheduling functionality.Add Example Usage of Task Scheduling Decorators:
The newly introduced @Cron and @interval decorators allow developers to easily schedule functions for execution at specific times or intervals. Below is an example of how these decorators can be used in the project:
Generated description
Below is a concise technical summary of the changes proposed in this PR:
Implements a comprehensive task scheduling system using APScheduler, introducing decorators for easy task definition and management. Establishes a robust foundation for periodic and cron-based task execution within the application. Enhances the project's dependency management to support the new scheduling capabilities across development, testing, and production environments.
Modified files (2)
Latest Contributors(0)
Modified files (2)
Latest Contributors(0)
Modified files (4)
Latest Contributors(2)
Modified files (1)
Latest Contributors(0)