This article introduces Crunz, a versatile PHP job scheduling library. Inspired by Laravel's task scheduler, Crunz offers enhanced flexibility and ease of use for managing cron jobs. The author welcomes contributions and feedback.
A solid understanding of cron jobs is assumed. Refer to the provided walkthrough if needed.
Key Features:
daily()
, everyFiveMinutes()
).from()
, to()
, between()
, when()
, and skip()
for managing task execution.Getting Started:
Install via Composer:
composer require lavary/crunz
A crunz
command-line utility will be available.
How it Works:
Define cron jobs in PHP files (e.g., within a tasks/
directory) using the Crunz interface, instead of directly in a crontab
file. A single cron job (running every minute) delegates execution to Crunz's event runner:
* * * * * /project/vendor/bin/crunz schedule:run
schedule:run
manages task execution based on defined schedules.
Task File Creation:
Task files (conventionally ending in Tasks.php
) contain task definitions. Crunz scans the tasks/
directory recursively. You can specify the task directory via configuration or command-line arguments.
Example Task:
<?php // tasks/MyTasks.php use Crunz\Schedule; $schedule = new Schedule(); $schedule->run('my_command') ->daily() ->description('My daily task.'); return $schedule;
Remember to return the $schedule
object.
Commands and Closures:
run()
accepts commands or PHP closures:
$schedule->run('/usr/bin/my_script.sh', ['--option' => 'value']) ->hourly(); $schedule->run(function () { /* code */ }) ->everyTenMinutes();
Directory Changes: Use in()
to change the working directory before task execution.
Scheduling Options:
hourly()
, daily()
, weekly()
, monthly()
, quarterly()
, yearly()
.everyFiveMinutes()
, everyTwelveHours()
, etc.on('14:00 2025-03-15')
, at('14:00')
, dailyAt('14:00')
.mondays()
, tuesdays()
, etc. (use with other frequency methods).cron('30 12 * * 1')
.Task Lifetime: from()
, to()
, between()
control task execution periods.
Conditional Execution: when()
and skip()
methods allow conditional task execution based on callbacks.
Configuration (YAML): A crunz.yaml
file (published using crunz publish:config
) allows customization of source directory, file suffix, logging, and email notifications.
Parallelism and Locking: Crunz runs tasks in parallel using symfony/Process
. preventOverlapping()
prevents overlapping runs.
Output and Error Handling: Log output and errors to files (configure in crunz.yaml
) or send via email. onError()
callbacks handle errors.
Pre/Post-Process Hooks: before()
and after()
callbacks allow execution of code before and after tasks.
Command-Line Utilities: crunz schedule:list
lists tasks; crunz make:task
creates task file skeletons.
Web Interface: Consider using lavary/crunz-ui
(a Laravel-based interface) for a web-based management solution.
Conclusion:
Managing cron jobs within your codebase offers version control and easier collaboration. Crunz simplifies this process significantly.
Frequently Asked Questions (FAQs): (The original FAQs are already well-written and don't require significant modification for this rewrite.)
The above is the detailed content of Framework-Agnostic PHP Cronjobs Made Easy with Crunz!. For more information, please follow other related articles on the PHP Chinese website!