Cron Scheduler Plugin #1
butschster
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Implemented: https://github.com/roadrunner-plugins/cron
Current Pain Point
PHP applications using RoadRunner often need to execute scheduled tasks (like Laravel's task scheduler, periodic maintenance jobs, or data synchronization). Currently, this requires external cron daemons or separate orchestration tools, which adds operational complexity and deployment overhead.
Proposed Solution
A native RoadRunner plugin that manages scheduled command execution within the RoadRunner process itself. The plugin will parse cron expressions, execute shell commands at scheduled intervals, and provide comprehensive observability through Prometheus metrics and debug logging.
Benefits
2. Technical Requirements
2.1 Configuration Structure
The plugin reads configuration from
.rr.yamlunder acronsection. Configuration is a collection of scheduled jobs, where each job defines:Job Definition Fields:
name(string, required): Unique identifier for the job (used in metrics and logs)command(string, required): Shell command to execute (e.g.,php artisan cache:clear)schedule(string, required): Cron expression defining when to runallow_overlap(boolean, optional, default:false): Whether to allow concurrent executions of the same jobtimeout(duration, optional, default: no timeout): Maximum execution time before SIGTERM is sentGlobal Settings:
timezone(string, optional, default:UTC): Timezone for cron schedule interpretationgrace_period(duration, optional, default:30s): Time to wait for running jobs during shutdown2.2 Cron Expression Support
The plugin must support standard cron syntax with extended precision:
Standard 5-field format:
Extended 6-field format for second precision:
Special expressions:
@yearly/@annually- Run once a year (0 0 1 1 *)@monthly- Run once a month (0 0 1 * *)@weekly- Run once a week (0 0 * * 0)@daily/@midnight- Run once a day (0 0 * * *)@hourly- Run once an hour (0 * * * *)@every <duration>- Run at fixed intervals (e.g.,@every 5m,@every 30s)2.3 Command Execution Behavior
Execution Environment:
Overlap Prevention:
allow_overlap: false(default) and job is already running:roadrunner_cron_skipped_totalmetricallow_overlap: true:Timeout Handling:
timeoutis configured and exceeded:roadrunner_cron_timeout_totalmetricExit Code Handling:
2.4 Lifecycle Management
Initialization (Init phase):
.rr.yamlService Start (Serve phase):
Graceful Shutdown (Stop phase):
grace_periodduration for commands to complete2.5 Metrics Integration (Prometheus)
The plugin exposes the following metrics when the metrics plugin is available:
Counter Metrics:
roadrunner_cron_executions_total{job_name, status}- Total executions (status: success, failure)roadrunner_cron_skipped_total{job_name}- Skipped executions due to overlap preventionroadrunner_cron_timeout_total{job_name}- Executions terminated due to timeoutHistogram Metrics:
roadrunner_cron_execution_duration_seconds{job_name}- Command execution durationGauge Metrics:
roadrunner_cron_running_jobs{job_name}- Number of currently running instances per job2.6 Debug Logging
When debug logging is enabled, the plugin outputs:
Initialization:
Execution Events:
Shutdown:
3. Architecture Design
3.1 Concurrency Model
Per-Job Goroutines:
time.NewTimer()for efficient sleep scheduling (not busy-waiting)Execution Isolation:
os/execpackageMutex-Based Overlap Prevention:
sync.Mutexfor execution serializationTryLock()is used (non-blocking)Context Propagation:
timeoutis configured3.2 Error Handling Strategy
Configuration Validation:
Runtime Errors:
No Panic Policy:
3.3 Resource Management
Memory Management:
Process Management:
cmd.Wait()File Descriptor Management:
4. Performance Considerations
4.1 Scalability Characteristics
Number of Jobs:
Execution Frequency:
Concurrent Executions:
allow_overlap: true, theoretically unlimited (limited by system resources)allow_overlap: falsefor resource-intensive commands4.2 Bottlenecks and Mitigations
Potential Bottleneck: Many jobs scheduled at exact same time (e.g., all at midnight)
Potential Bottleneck: Long-running commands blocking shutdown
grace_periodwith forceful termination fallbackPotential Bottleneck: Large command output causing memory pressure
5. Integration Points
5.1 RoadRunner Plugin System
Dependencies:
config.Provider(required): Configuration accesslog.Logger(required): Logging interfacemetrics.Provider(optional): Prometheus metrics registrationInterfaces Implemented:
endure.Named: Returns plugin name "cron"endure.Service: ImplementsServe()andStop()for lifecycle managementendure.Weight: Returns weight for dependency resolution (low priority, no dependencies on worker pools)5.2 Configuration Example
5.3 Prometheus Query Examples
Job Success Rate:
Jobs Timing Out:
Average Execution Duration:
Jobs Currently Running:
6. Acceptance Criteria
6.1 Functional Requirements
Must Have:
allow_overlap: falseShould Have:
Won't Have (Out of Scope):
6.2 Performance Targets
grace_periodwith ±500ms accuracy7. Implementation Considerations
7.1 Cron Library Selection
Recommend using
github.com/robfig/cron/v3for cron expression parsing:7.2 Command Execution
Use
os/exec.CommandContextfor command execution:7.3 Metrics Registration
Follow existing RoadRunner plugin patterns:
roadrunner_cron_prefix7.4 Configuration Validation
Validate during
Init()phase beforeServe():8. Future Enhancements (Not in Initial Scope)
Potential V2 Features:
These features are explicitly out of scope for initial implementation to maintain simplicity and focus on core scheduling functionality.
Beta Was this translation helpful? Give feedback.
All reactions