carlossosa88/cron-expression
Fork of dragonmantank/cron-expression adding nonstandard seconds support for Fcron-style scheduling. Parse cron strings and macros, check if a schedule is due, and compute next/previous run dates with optional second-level precision control.
schedules in app/Console/Kernel.php), or cron-like workflows. It directly maps to PHP’s native cron syntax, including seconds support (critical for high-frequency tasks like rate-limiting or real-time processing).dragonmantank/cron-expression), this package is battle-tested in the ecosystem. Its compatibility with Laravel’s DateTime/Carbon and timezone handling aligns with Laravel’s conventions.* * * * * */5 for every 5 seconds) and macros (@daily, @hourly), reducing boilerplate for common patterns. The getNextRunDate()/getPreviousRunDate() methods enable predictive scheduling, useful for:
composer require carlossosa88/cron-expression) and PSR-4 autoloading ensure zero-config integration. No database migrations or schema changes required.CronExpression (if needed) by aliasing the facade:
// config/app.php
'aliases' => [
'Cron' => CarlosSosa88\CronExpression\CronExpression::class,
]
shouldRun() logic:
$job->handle() if (Cron::factory($job->cronExpression)->isDue());
traits) simplify validation of cron logic.| Risk Area | Mitigation Strategy |
|---|---|
| Expression Parsing | Validate inputs with try-catch for malformed cron strings (e.g., */99 25). |
| Timezone Handling | Explicitly pass DateTimeZone to isDue()/getNextRunDate() to avoid defaults. |
| Performance | Benchmark getNextRunDate() for high-frequency jobs (e.g., every 5 seconds). |
| Deprecation | Monitor upstream (dragonmantank/cron-expression) for breaking changes. |
| Seconds Support | Test edge cases (e.g., * * * * * 0,5,10,20 with leap seconds). |
CronExpression, or supplement it (e.g., for seconds precision)?* * * * * 60) be logged/handled in production?spatie/schedule-laravel (if using Laravel’s scheduler) or briannesbitt/Carbon for simpler cases.app/Console/Kernel.php’s schedule() method for dynamic cron logic.Job classes to gate execution:
public function handle() {
if (!Cron::factory($this->expression)->isDue()) {
return;
}
// Execute logic...
}
ReportGenerated) using cron calculations.@daily for Cron::factory('@daily')).isDue() against existing cron logs to validate accuracy.* * * * * */5 for API rate-limiting).| Component | Compatibility Notes |
|---|---|
| Laravel 8+ | Fully compatible (PHP 7.1+). Test with Laravel’s Carbon and DateTime. |
| Symfony | Use the Setono bundle for DI integration. |
| Legacy PHP (5.x) | Unsupported (package dropped PHP 5.x in v2.0.0). |
| Timezones | Explicitly pass DateTimeZone to avoid UTC defaults. |
| Database | No schema changes, but store cron expressions as strings in DB (e.g., jobs table). |
phpunit/phpunit for tests).composer require carlossosa88/cron-expression
isDue()/getNextRunDate() with edge cases (e.g., DST transitions).cron.is_due in structured logs).dragonmantank/cron-expression) may evolve independently.composer.json (e.g., ^2.3.0).try-catch to log malformed expressions:
try {
$cron = Cron::factory($expression);
} catch (\InvalidArgumentException $e) {
Log::error("Invalid cron expression: {$e->getMessage()}");
}
*/5 vs. 0-59/5).dragonmantank/cron-expression) for advanced use cases.static $parsedExpressions = []).DateTime object creation.spatie/clock for precise timing).| Scenario | Impact | Mitigation |
|---|---|---|
| Malformed Cron Expression | Job silently fails. | Validate inputs; log errors. |
| Timezone Mismatch | Jobs run at wrong local time. | Enforce UTC in cron expressions. |
| Leap Seconds | Seconds-based cron fails. | Test with DateTime::createFromFormat(). |
| PHP Upgrade | Breaking changes (e.g., PHP 8). | Use platform-check in CI. |
| Package Abandonment | No updates. | Fork the repo if critical. |
How can I help you explore Laravel packages today?