Skip to content

Conversation

@maxsonferovante
Copy link

Task Scheduling Feature for PyNest

This pull request introduces a comprehensive task scheduling system to PyNest, enabling developers to easily schedule background tasks, periodic operations, and automated processes using familiar decorator patterns.

Decorators

@Cron - Schedule tasks with cron expressions:

from nest.core.decorators.scheduler import Cron
from nest.core.apscheduler.enums.cron_expression import CronExpression

@Cron(expression=CronExpression.EVERY_DAY_AT_MIDNIGHT)
def daily_cleanup():
    """Runs every day at midnight"""
    print("Performing daily cleanup...")

@Cron(expression=CronExpression.EVERY_WEEKDAY)
def weekday_report():
    """Runs Monday to Friday at midnight"""
    print("Generating weekday report...")

@Interval - Schedule tasks at regular intervals:

from nest.core.decorators.scheduler import Interval

@Interval(minutes=5)
def health_check():
    """Runs every 5 minutes"""
    print("Checking system health...")

@Interval(hours=2, minutes=30)
def cache_refresh():
    """Runs every 2 hours and 30 minutes"""
    print("Refreshing cache...")

Seamless PyNest Integration

Works perfectly with PyNest's dependency injection system:

@Injectable
class ReportService:
    def __init__(self, email_service: EmailService, db_service: DatabaseService):
        self.email_service = email_service
        self.db_service = db_service
    
    @Cron(expression=CronExpression.EVERY_DAY_AT_9AM)
    def generate_daily_report(self):
        """Automatically scheduled when service is instantiated"""
        data = self.db_service.get_daily_stats()
        report = self.create_report(data)
        self.email_service.send_report(report)

Key Features

Zero Configuration Required

  • Tasks are automatically detected and scheduled when your PyNest application starts
  • No manual scheduler setup or job registration needed
  • Graceful startup and shutdown handling

82+ Predefined Cron Expressions

Choose from a comprehensive set of ready-to-use expressions:

  • Time intervals: EVERY_SECOND, EVERY_5_MINUTES, EVERY_HOUR
  • Daily schedules: EVERY_DAY_AT_MIDNIGHT, EVERY_DAY_AT_NOON
  • Weekly patterns: EVERY_WEEKDAY, MONDAY_TO_FRIDAY_AT_9AM
  • Monthly/yearly: EVERY_1ST_DAY_OF_MONTH, EVERY_QUARTER

Production-Ready Features

  • Memory safe: Uses weak references to prevent memory leaks
  • Error resilient: Individual task failures don't crash the application
  • Proper logging: Detailed execution logs for monitoring
  • Lifecycle management: Automatic startup/shutdown with your application

Dependency Injection Support

  • Works with @Injectable services
  • Full access to injected dependencies within scheduled methods
  • Automatic activation when services are instantiated

Implementation Details

Core Components Added

  1. Scheduler Core (nest/core/apscheduler/)

    • APScheduler BackgroundScheduler instance
    • Automatic lifecycle management
    • UTC timezone configuration
  2. Decorators (nest/core/decorators/scheduler.py)

    • @Cron and @Interval decorators
    • Support for both functions and class methods
    • Intelligent instance tracking
  3. Enums (nest/core/apscheduler/enums/)

    • 82+ predefined cron expressions
    • Scheduler type definitions
    • Type-safe scheduling options
  4. PyNest Integration (nest/core/pynest_application.py)

    • Automatic detection of scheduled methods
    • Integration with dependency injection
    • Application lifecycle hooks

Dependencies Added

APScheduler==3.10.4  # Core scheduling library
pytz==2024.2         # Timezone support
six==1.16.0          # Python 2/3 compatibility
tzlocal==5.2         # Local timezone detection

Documentation & Examples

Complete Documentation

  • New docs/task_scheduling.md with comprehensive guide
  • Best practices and troubleshooting tips
  • Integration examples with dependency injection

Working Example Application

  • examples/SchedulerApp/ - Complete PyNest application
  • REST endpoints for monitoring scheduled tasks
  • Real-time task execution logs
  • Statistics and health check endpoints

Comprehensive Testing

  • Full test coverage for both decorators
  • Parameter validation tests
  • Error condition handling
  • Mock-based isolation testing

Use Cases

Perfect for:

  • Background data processing
  • Periodic report generation
  • System health monitoring
  • Database cleanup operations
  • Cache invalidation
  • Automated backups
  • Notification sending
  • Log rotation

Getting Started

  1. Install dependencies (automatically handled by requirements files)

  2. Create a scheduled service:

@Injectable
class TaskService:
    @Cron(expression=CronExpression.EVERY_MINUTE)
    def my_task(self):
        print("Task executed!")
  1. Register in your module:
@Module(providers=[TaskService])
class AppModule:
    pass

…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.
This commit introduces lifecycle management for the scheduler in the `PyNestApp` class, ensuring the scheduler starts and stops with the application. It also adds functionality to activate scheduled methods for instantiated services, enhancing the scheduling capabilities of the application. New methods `_setup_scheduler_lifecycle` and `_activate_scheduled_methods` are implemented to facilitate these features. Additionally, the `apscheduler` module is updated to include start and stop functions for the scheduler, and the decorators for scheduling tasks are enhanced to ensure proper initialization.
This commit introduces a new test file `test_scheduler.py` that contains unit tests for the `@Cron` and `@Interval` decorators. The tests cover various scenarios, including valid and invalid cron expressions, as well as different parameter configurations for the interval decorator. The tests ensure that the decorators correctly register jobs with the scheduler and handle errors appropriately.
…, service, and scheduler service

This commit introduces the `SchedulerApp` module, which includes the main application controller (`AppController`), application service (`AppService`), and scheduler service (`SchedulerService`). The app controller provides endpoints for application information, scheduler statistics, logs, and health checks. The application service offers detailed information about the scheduling system, while the scheduler service implements task scheduling using decorators. A new entry point is also created in `main.py` to run the application with Uvicorn.
This commit updates the `pyproject.toml` file to transition from setuptools to Poetry as the build system. It includes the addition of versioning, authorship, and enhanced dependency management, specifying core and optional dependencies. The project metadata has been updated to reflect the new structure, including homepage and documentation links. The previous optional dependencies have been restructured under Poetry's extras and groups.
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