|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** AutoCrontabJob and related classes */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Battis; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* An object-oriented approach to scheduling regular cron jobs automatically |
|
9
|
|
|
* |
|
10
|
|
|
* @author Seth Battis <[email protected]> |
|
11
|
|
|
**/ |
|
12
|
|
|
abstract class AutoCrontabJob |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var \TiBeN\CrontabManager\CrontabRepository Crontab management */ |
|
15
|
|
|
private $cron = null; |
|
16
|
|
|
|
|
17
|
|
|
/** @var \Log A Pear Log handle */ |
|
18
|
|
|
protected $log = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Construct a new AutoCrontabJob |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $identifier Hopefully unique! |
|
24
|
|
|
* @param string $script Path to data collection script (probably `__FILE__`) |
|
25
|
|
|
* @param string|TiBeN\CrontabManager\CrontabJob Cron schedule or a complete |
|
|
|
|
|
|
26
|
|
|
* `TiBeN\CrontabManager\CrontabJob` |
|
27
|
|
|
* @param string (Optional) path to log file (defaults to same directory and |
|
|
|
|
|
|
28
|
|
|
* name as the script file) e.g. a script at `/var/www/foo/bar.php` would |
|
29
|
|
|
* have a log at `/var/www/foo/bar.log` |
|
30
|
|
|
* |
|
31
|
|
|
* @throws AutoCrontabJob_Exception CONSTRUCTOR_ERROR If parameters are not validated |
|
32
|
|
|
**/ |
|
33
|
|
|
public function __construct($identifier, $script, $schedule, $log = null) |
|
34
|
|
|
{ |
|
35
|
|
|
/* Make sure the scheduled script file exists... */ |
|
36
|
|
|
if (file_exists($script)) { |
|
37
|
|
|
/* try to make the identifier truly unique to this instance */ |
|
38
|
|
|
$_identifier = $identifier . '.' . md5($identifier . __FILE__ . __CLASS__); |
|
39
|
|
|
|
|
40
|
|
|
/* ensure that we're working with a valid Cron job */ |
|
41
|
|
|
$newJob = null; |
|
42
|
|
|
if (is_string($schedule)) { |
|
43
|
|
|
$newJob = \TiBeN\CrontabManager\CrontabJob::createFromCrontabLine($schedule . " php $script"); |
|
44
|
|
|
} elseif ($schedule instanceof \TiBeN\CrontabManager\CrontabJob) { |
|
45
|
|
|
$newJob = $schedule; |
|
46
|
|
|
} else { |
|
47
|
|
|
throw new AutoCrontabJobException( |
|
48
|
|
|
'Expected a string or TiBeN\CrontabManager\CrontabJob, received ' . print_r($schedule, true), |
|
|
|
|
|
|
49
|
|
|
AutoCrontabJobException::CONSTRUCTOR_ERROR |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
$newJob->comments = implode(' ', [ |
|
53
|
|
|
$newJob->comments, |
|
54
|
|
|
"Created by $script:" . get_class($this) . ' ' . date('Y-m-d h:ia') . " (Job ID $_identifier)" |
|
55
|
|
|
]); |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/* update cron if this job already exists */ |
|
59
|
|
|
$this->cron = new \TiBeN\CrontabManager\CrontabRepository(new \TiBeN\CrontabManager\CrontabAdapter()); |
|
60
|
|
|
$jobs = $this->cron->getJobs(); |
|
61
|
|
|
$jobExists = false; |
|
62
|
|
|
// TODO Rewrite whenever this is fixed: https://github.com/TiBeN/CrontabManager/issues/3 |
|
63
|
|
|
foreach ($jobs as $job) { |
|
64
|
|
|
if (preg_match("/$_identifier/", $job->formatCrontabLine())) { |
|
65
|
|
|
$jobExists = true; |
|
66
|
|
|
$job->minutes = $newJob->minutes; |
|
67
|
|
|
$job->hours = $newJob->hours; |
|
68
|
|
|
$job->dayOfMonth = $newJob->dayOfMonth; |
|
69
|
|
|
$job->months = $newJob->months; |
|
70
|
|
|
$job->dayOfWeek = $newJob->dayOfWeek; |
|
71
|
|
|
break; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/* ... or add this as a new job if it doesn't exist */ |
|
76
|
|
|
if (!$jobExists) { |
|
77
|
|
|
$this->cron->addJob($newJob); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/* set up log file */ |
|
81
|
|
|
if (empty($log)) { |
|
82
|
|
|
$log = dirname($script) . '/' . basename($script, '.php') . '.log'; |
|
83
|
|
|
} |
|
84
|
|
|
$this->log = \Log::singleton('file', $log); |
|
85
|
|
|
|
|
86
|
|
|
/* update cron to enable scheduled jobs */ |
|
87
|
|
|
$this->cron->persist(); |
|
88
|
|
|
} else { |
|
89
|
|
|
throw new AutoCrontabJobException( |
|
90
|
|
|
"PHP script '$script' does not exist", |
|
91
|
|
|
AutoCrontabJobException::CONSTRUCTOR_ERROR |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Scheduled job |
|
98
|
|
|
* |
|
99
|
|
|
* Override this method to do something scheduled by Cron. |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
**/ |
|
103
|
|
|
abstract public function scheduledJob(); |
|
104
|
|
|
} |
|
105
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths