1- # Scheduler [ ![ Latest Version] ( https://img.shields.io/github/release/azurre/scheduler.svg?style=flat-square )] ( https://github.com/azurre/scheduler/releases )
1+ # PHP Cron Scheduler [ ![ Latest Version] ( https://img.shields.io/github/release/azurre/php-cron-scheduler.svg?style=flat-square )] ( https://github.com/azurre/php-cron-scheduler/releases )
2+
23Simple cron jobs manager. Keep your project cron jobs in your project!
34
45# Installation
56
6- Install composer in your project:
7- ```
8- curl -s https://getcomposer.org/installer | php
9- ```
10-
117Require the package with composer:
8+
129```
13- composer require azurre/php-scheduler
10+ composer require azurre/php-cron- scheduler
1411```
1512
1613# Usage
1714
1815Add scheduler starter to cron:
16+
1917``` bash
2018$ crontab -e
2119```
@@ -25,32 +23,46 @@ $ crontab -e
2523```
2624
2725Sample of scheduler.php
26+
2827``` php
2928$loader = require_once __DIR__ . '/vendor/autoload.php';
3029
3130use Azurre\Component\Cron\Scheduler;
31+ use Azurre\Component\Cron\Expression;
3232
33- $php = '/usr/bin/php';
34- $path = dirname(__FILE__) . '/';
33+ $e = new Expression();
3534
36- $Scheduler = new Scheduler();
37- $Scheduler
38- ->setJobPath($path)
39- ->setLogsPath($path);
35+ echo $e->monthly(28); // 0 0 28 * *
36+ echo $e->weekly($e::FRIDAY)->at('05:30'); // 30 5 * * 5
37+ echo $e->daily('06:10'); // 10 6 * * *
4038
41- $Scheduler->addJob('* * * * *', function($logsPath){
42- // just do something
43- file_put_contents($logsPath . 'log.log', 'OK', FILE_APPEND);
44- });
39+ echo Expression::create() // */5 0 16 1 5
40+ ->setMinute('*/5')
41+ ->setHour('*')
42+ ->setDayOfMonth(16)
43+ ->setDayOfWeek('fri')
44+ ->setMonth('Jan');
4545
46- $Scheduler->addJob('*/2 * * * *', function ($logsPath, $jobPath) use ($php) {
47- // run standalone php script
48- $cmd = "{$php} {$jobPath}calculate.php >> {$logsPath}calculate.log 2>&1";
49- $result = `$cmd`;
50- });
46+ // ------------
5147
52- // Do something ...
48+ $testFunc = function () {
49+ echo 'TEST OK';
50+ };
51+ $scheduler = new Scheduler();
52+ $scheduler
53+ ->addJob('* * * * *', function() {
54+ // just do something
55+ })->addJob('0 0 * * * *', $testFunc);
56+ $scheduler->run();
5357
54- echo date('d-m-Y H:i:s') . ' Run scheduler...' . PHP_EOL;
55- $Scheduler->run();
58+ // -----------
59+
60+ $logPath = '/path/to/log.log';
61+ $scheduler = new Scheduler('2021-07-05 06:10:00');
62+ $scheduler->addJob($e, function () use($logPath) {
63+ // run standalone php script
64+ $cmd = "/usr/bin/php /path/to/script.php >> {$logPath} 2>&1";
65+ system($cmd);
66+ });
67+ $scheduler->run();
5668```
0 commit comments