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
+
2
3
Simple cron jobs manager. Keep your project cron jobs in your project!
3
4
4
5
# Installation
5
6
6
- Install composer in your project:
7
- ```
8
- curl -s https://getcomposer.org/installer | php
9
- ```
10
-
11
7
Require the package with composer:
8
+
12
9
```
13
- composer require azurre/php-scheduler
10
+ composer require azurre/php-cron- scheduler
14
11
```
15
12
16
13
# Usage
17
14
18
15
Add scheduler starter to cron:
16
+
19
17
``` bash
20
18
$ crontab -e
21
19
```
@@ -25,32 +23,46 @@ $ crontab -e
25
23
```
26
24
27
25
Sample of scheduler.php
26
+
28
27
``` php
29
28
$loader = require_once __DIR__ . '/vendor/autoload.php';
30
29
31
30
use Azurre\Component\Cron\Scheduler;
31
+ use Azurre\Component\Cron\Expression;
32
32
33
- $php = '/usr/bin/php';
34
- $path = dirname(__FILE__) . '/';
33
+ $e = new Expression();
35
34
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 * * *
40
38
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');
45
45
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
+ // ------------
51
47
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();
53
57
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();
56
68
```
0 commit comments