Home > Backend Development > PHP Tutorial > Framework-Agnostic PHP Cronjobs Made Easy with Crunz!

Framework-Agnostic PHP Cronjobs Made Easy with Crunz!

William Shakespeare
Release: 2025-02-10 13:54:12
Original
161 people have browsed it

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.

Framework-Agnostic PHP Cronjobs Made Easy with Crunz!

A solid understanding of cron jobs is assumed. Refer to the provided walkthrough if needed.

Key Features:

  • Framework-agnostic: Works seamlessly with any PHP framework.
  • Simple Installation: Uses Composer for easy setup.
  • Flexible Scheduling: Supports various frequencies (e.g., daily(), everyFiveMinutes()).
  • Precise Control: Offers methods like from(), to(), between(), when(), and skip() for managing task execution.
  • Parallel Processing: Executes tasks concurrently for efficiency.
  • Robust Locking: Prevents overlapping task runs to maintain data integrity.
  • Customizable Configuration: Uses a YAML file for flexible settings.
  • Comprehensive Logging: Logs output and errors, with email notification options.

Getting Started:

Install via Composer:

composer require lavary/crunz
Copy after login

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
Copy after login

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;
Copy after login

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();
Copy after login

Directory Changes: Use in() to change the working directory before task execution.

Scheduling Options:

  • Units of Time: hourly(), daily(), weekly(), monthly(), quarterly(), yearly().
  • Dynamic Methods: everyFiveMinutes(), everyTwelveHours(), etc.
  • Specific Times: on('14:00 2025-03-15'), at('14:00'), dailyAt('14:00').
  • Weekdays: mondays(), tuesdays(), etc. (use with other frequency methods).
  • Individual Fields: Precise control over minute, hour, day, month, and day of week using arrays or comma-separated lists.
  • Cron Expression: 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template