job scheduler
composer require lavary/crunz
https://github.com/lavary/crunz
<?php
/*
- all the task files reside in tasks/ directory (By default) within the project's root directory.
- only need to install an ordinary cron job: responsible for collecting all the PHP task files and run the tasks which are due
* * * * * /project/vendor/bin/crunz schedule:run
# can explicitly set the source path by arg
* * * * * /project/vendor/bin/crunz schedule:run /path/to/tasks/directory
- can either put all the tasks in one file or across different files (have a well organized tasks directory)
*/
// tasks/backupTasks.php --------------------------------------------
use Crunz\Schedule;
$schedule = new Schedule();
$schedule->run('cp project project-bk')
//accepts two arguments
$schedule->run('/usr/bin/php backup.php', ['--destination' => 'path/to/destination'])
->daily()
->everyMinute() #or
->description('Create a backup of the project directory.');
// IMPORTANT: You must return the schedule object
return $schedule;
#------------------------------------------------------------------
//change directory before running a command
$schedule->run('./deploy.sh')
->in('/home') # change directory before running a command
->weekly()
->sundays()
->at('12:30')
//need to log the outputs on an event-basis
->appendOutputTo('/var/log/backup.log');
//write a closure to instead of a command
$x = 12;
$schedule->run(function() use ($x) {
// Do some cool stuff in here
})
->everyMinute()
/**
* Frequency of Execution
*/
->daily() # daily at midnight
->monthly() # first day of each month
->weekly() # run the event on Sundays
//Dynamic methods
everyFiveMinutes()
everyMinute()
everyTwelveHours()
everyMonth
everySixMonths()
everyFifteenDays()
everyFiveHundredThirtySevenMinutes()
everyThreeThousandAndFiveHundredFiftyNineMinutes()
//run at at Certain Times: schedule one-off tasks
->on('13:30 2016-03-01') # run on the first of march 2016 at 01:30 pm
//specify the time: at()
->daily()
->at('13:30') # same as 'on()'
# equal
->dailyAt('13:30')
# sample
->mondays()
->on('13:30') # equal: ->at('13:30')
//specify a certain day in the week
// Cron equivalent: * * * * 1 -> run every Monday
$schedule->run('/usr/bin/php email.php')
->mondays();
//runs every 3 hours on Mondays
$schedule->run('/usr/bin/php email.php')
->everyThreeHours()
->mondays();
/**
* Individual Fields
*/
//accept an array of values, or list arguments separated by commas
$schedule->run('/usr/bin/php email.php')
->minute(['1-30', 45, 55])
->hour('1-5', 7, 8)
->dayOfMonth(12, 15)
->month(1);
$schedule->run('/usr/bin/php email.php')
->minute('30')
->hour('13')
->month([1,2])
->dayofWeek('Mon', 'Fri', 'Sat');
/**
* task's lifetime
*/
//period of time when the task is active
->everyFiveMinutes()
->between('12:30 2016-03-04', '04:55 2016-03-10')
#or
->from('12:30 2016-03-04')
->to('04:55 2016-03-10');
# will be active every day but only within the specified duration
->between('12:30', '04:55') # between 12:30 pm and 4:55 pm
/**
* conditions
*/
$schedule->run('/usr/bin/php email.php')
->everyFiveMinutes()
->between('12:30 2016-03-04', '04:55 2016-03-10')
->when(function() {
if ($some_condition_here) { return true; }
})
//If the passed callback returns TRUE, the task will be skipped.
->skip(function() {
if ($some_condition_here) { return true; };
/**
* Error Handling
*/
->onError(function(){
// Send mail
})
->onError(function(){
// Do something else
});