Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

maxsonferovante
Copy link

@maxsonferovante maxsonferovante commented Oct 18, 2024

User description

Pull Request Description

This pull request introduces task scheduling functionality using APScheduler and includes the following updates:

  1. Update requirements files:

    • requirements-dev.txt, requirements-release.txt, and requirements-tests.txt are updated to include the following dependencies:
      • APScheduler
      • pytz
      • six
      • tzlocal
  2. Add BackgroundScheduler instance and task scheduling decorators:

    • Introduces a new file apscheduler.py in the nest/core/apscheduler directory, which implements an instance of the BackgroundScheduler from the APScheduler library.
    • Adds a file scheduler.py to the nest/core/decorators directory containing two decorators:
      • @Cron: For scheduling tasks using cron expressions.
      • @Interval: For scheduling tasks to run at fixed intervals.
  3. Add enums for cron expressions and scheduler types:

    • Defines enums to standardize cron expressions and scheduler types, aiding in creating clearer and more manageable task scheduling.
  4. Update pyproject.toml:

    • The necessary dependencies for the task scheduler are added to pyproject.toml, ensuring the project is compatible with the new scheduling functionality.
  5. 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:

from nest.core.apscheduler import scheduler
from nest.core.apscheduler.enums.cron_expression import CronExpression
from nest.core.apscheduler.enums.scheduler_type import SchedulerType

# Schedule a task to run every minute using a cron expression
@Cron(expression=CronExpression.EVERY_MINUTE)
def task_run_every_minute():
    print("This task runs every minute.")

# Schedule a task to run every 10 seconds
@Interval(seconds=10)
def task_run_every_ten_seconds():
    print("This task runs every 10 seconds.")



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.

TopicDetails
Scheduling Enums Defines enumerations for standardized cron expressions and scheduler types
Modified files (2)
  • nest/core/apscheduler/enums/cron_expression.py
  • nest/core/apscheduler/enums/scheduler_type.py
Latest Contributors(0)
UserCommitDate
Scheduler Core Implements the core scheduling functionality using APScheduler's BackgroundScheduler
Modified files (2)
  • nest/core/apscheduler/__init__.py
  • nest/core/apscheduler/apscheduler.py
Latest Contributors(0)
UserCommitDate
Dependency Management Updates project dependencies to include APScheduler and related libraries across all environments
Modified files (4)
  • requirements-dev.txt
  • requirements-tests.txt
  • requirements-release.txt
  • pyproject.toml
Latest Contributors(2)
UserCommitDate
github@actions.comIncrement-version-toDecember 20, 2024
itay.dar@lemonade.comMigrate-pynest-to-poet...December 20, 2024
Decorator Interface Creates decorators for easy task scheduling using cron expressions and intervals
Modified files (1)
  • nest/core/decorators/scheduler.py
Latest Contributors(0)
UserCommitDate
This pull request is reviewed by Baz. Join @maxsonferovante and the rest of your team on (Baz).

…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.
Copy link

@baz-reviewer baz-reviewer bot left a 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.

Comment on lines +18 to +72
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)
Copy link

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

Comment on lines +7 to +9
CRON = 'cron',
DATE = 'date'
INTERVAL = 'interval',
Copy link

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

Comment on lines +9 to +38
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
Copy link

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

Comment on lines +36 to +38
"APScheduler >= "3.10.4",
"pytz >= "2024.2",
"six >= "1.16.0",
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant