Crunz PHP Cron Job Manager

repository·3.10·Indexed 42 days ago

https://github.com/crunzphp/crunz

A framework-agnostic PHP package that allows developers to manage cron jobs through a fluent PHP API instead of manual crontab editing. It enables scheduling periodic tasks, commands, or closures directly within application code using the Crunz\Schedule API and an event runner pattern.

Tokens
10.4K
Snippets
49
Records
55
Agent score
53%

What's inside Crunz

  1. How Crunz works: The Event Runner pattern

    3.10

    Crunz allows you to manage cron jobs through PHP code instead of manual crontab entries.

    1. Define Tasks: Create PHP files (task files) using the Crunz\Schedule API to define what should run and when.
    2. Single Cron Entry: Instead of many cron entries, you install one single cron job that runs every minute. This entry delegates task execution to the Crunz event runner.
    3. Execution: The schedule:run command scans your task files and executes any tasks that are due to run at that specific minute.
    # Add this to your crontab to run Crunz every minute
    * * * * * cd /project && vendor/bin/crunz schedule:run
  2. Use weekday constraints

    3.10

    Crunz provides methods for specific days: mondays(), tuesdays(), wednesdays(), thursdays(), fridays(), saturdays(), and sundays().

    Warning: These methods only modify the 'Day of Week' field of the cron expression. If used alone, the task will run every minute on that day. You should almost always combine them with a time constraint like at().

    // CORRECT: Runs every Monday at 13:30
    $task->mondays()->at('13:30');
    
    // INCORRECT: Runs every minute on Mondays
    $task->mondays();
  3. Schedule task frequency and timing

    3.10

    Crunz provides several ways to define when a task should run.

    Time Units

    Methods like hourly(), daily(), weekly(), monthly(), quarterly(), and yearly() schedule tasks at the beginning of that time unit (e.g., daily() runs at midnight).

    Specific Times

    • at('HH:mm'): Specifies a specific time of day.
    • on('date time'): Schedules a one-off task using any format compatible with PHP's strtotime().
    • hourlyAt('mm'): A shortcut for running a task every hour at a specific minute.
    • weeklyOn(day, 'HH:mm'): Runs a task on a specific day of the week (0 = Sunday) at a specific time.

    Task Lifetime (Active Windows)

    Use from(), to(), or between() to restrict when a task is active:

    • between('HH:mm', 'HH:mm'): Runs the task every day within that time window.
    • between('YYYY-MM-DD HH:mm', 'YYYY-MM-DD HH:mm'): Runs the task only during a specific date range.
    // Run every hour at the 15th minute
    $task->hourlyAt('15');
    
    // Run every Monday at 13:30
    $task->weeklyOn(1, '13:30');
    
    // Run every day between 12:30 PM and 4:55 PM
    $task->everyFiveMinutes()->between('12:30', '16:55');
  4. Update mailer transport for v2.0

    3.10
    When upgrading to v2.0, stop using the mail transport for the mailer. This is because v6.0 of SwiftMailer dropped support for mail transport. Instead, use smtp or sendmail transport.
  5. Ensure task files return ` extbackslash Crunz extbackslash Schedule` (v1.11 to v1.12)

    3.10

    When upgrading from v1.11 to v1.12, task files must always return an instance of extbackslash Crunz extbackslash Schedule. Returning an empty array or other types is no longer supported.

    Correct task file pattern:

    <?php
    
    use Crunz	extbackslash Schedule;
    
    $scheduler = new Schedule();
    
    $scheduler
        ->run('php -v')
        ->description('PHP version')
        ->everyMinute();
    
    return $scheduler;
  6. Configure `timezone` in `crunz.yml` (v1.7 to v1.8)

    3.10

    When upgrading from v1.7 to v1.8, you must add a timezone key to your crunz.yml configuration file.

    Example crunz.yml:

    source: tasks
    suffix: Tasks.php
    timezone: Europe/Warsaw
  7. Run Crunz from the directory containing `crunz.yml` (v1.10 to v1.11)

    3.10

    As of v1.11, Crunz searches for its configuration (crunz.yml) relative to the current working directory (cwd), rather than relative to the vendor/bin/crunz binary location.

    To ensure Crunz finds your configuration, you must cd into the project directory before executing commands.

    CLI usage:

    cd /var/www/project && vendor/bin/crunz schedule:list

    Cron job configuration:

    * * * * * cd /var/www/project && vendor/bin/crunz schedule:run
  8. Publish and configure Crunz

    3.10

    Crunz uses a YAML configuration file. Instead of modifying the package defaults, you should publish a local copy to your project root. Once published, Crunz will prioritize your local file over the internal one.

    # Publish the configuration file to your project root
    vendor/bin/crunz publish:config
  9. Create and structure Task Files

    3.10

    Task files are PHP files that define one or more scheduled tasks.

    Conventions:

    • Directory: By default, Crunz looks for task files in the tasks/ directory at your project root. You can override this by passing a path to the schedule:run command.
    • Naming: Filenames should ideally end with Tasks.php (e.g., GeneralTasks.php).
    • Return Value: You must return the Crunz\Schedule instance at the end of the file, or the tasks in that file will be ignored.
    • Organization: Crunz scans the directory recursively, so you can organize tasks into subdirectories.
    <?php
    // tasks/FirstTasks.php
    
    use Crunz\Schedule;
    
    $schedule = new Schedule();
    
    $task = $schedule->run('cp project project-bk'); 
    $task
        ->daily()
        ->description('Create a backup of the project directory.');
    
    // IMPORTANT: You must return the schedule object
    return $schedule; 
  10. Migrate ` extbackslash Crunz extbackslash Schedule::run` parameters (v3.2 to v3.3)

    3.10

    When upgrading from v3.2 to v3.3, the extbackslash Crunz extbackslash Schedule::run method requires all parameters in the arguments array to be strings. Do not pass booleans or integers directly.

    Incorrect (v3.2):

    $schedule->run('php', ['-v' => true, 2]);

    Correct (v3.3+):

    $schedule->run('php', ['-v' => '1', '2']);
    $schedule = new Schedule();
    $schedule->run('php', ['-v' => '1', '2']);
  11. Install Crunz via Composer

    3.10

    Install Crunz as a dependency in your PHP project using Composer. Once installed, a command-line utility named crunz will be available in your vendor/bin directory.

    composer require crunzphp/crunz
  12. Stop using ` extbackslash Crunz extbackslash Event::setProcess` (v1.11 to v1.12)

    3.10
    The method extbackslash Crunz extbackslash Event::setProcess was intended to be private and is deprecated. In v2.0, calling this method will result in an exception. Avoid using it to manually set a Symfony Process instance on a task.