# Crunz

Document (opens new window)

# Install

composer require lavary/crunz

# Usage

Just need only one task in crontabs, will be executed in every minute.

* * * * * cd /project && vendor/bin/crunz schedule:run /path/to/tasks/directory

By default, Crunz assumes all the task files reside in the tasks/ directory within the project's root directory. And the suffix of the file is Tasks.php

# Directory

tasks\
    locks\
        index.html
    FirstGroupTasks.php
    SecondGroupTasks.php

A template for creating tasks

<?php
use Crunz\Schedule;
use Symfony\Component\Lock\Store\FlockStore;

$schedule = new Schedule();

$schedule->onError(function() {
    // sendmai de 
    echo 'Send mail deeeee';
});

/// run with a closure 
/// this can be replaced by other ways
$task = $schedule->run(function() use ($x) { 
    echo "I'm running <3";
    echo "I'm done";
});


$task->description('A task run every minute'); 

// set time
$task->everyMinute();

// prevent overllapse
$store = new FlockStore(__DIR__ . '/locks');
$task->preventOverlapping($store);

// add hooks 
$task->before(function() { 
    echo "\n[START]: ". date('Y-m-d H:i:s') . "\n";
})->after(function () {
    echo "\n[END]: ". date('Y-m-d H:i:s') . "\n";
});

// append logs
$task->appendOutputTo( __DIR__ . '/logs/closure2/' . date('Y-m-d') . '.log');

return $schedule;

# Ways to run task

# Ways to set timeline

# Skip by specific condition

$task->skip(function() {
    if ((bool) (time() % 2)) {
        return true;
    }
    return false;  
});

# Commands