-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCronJobber.php
182 lines (163 loc) · 4.4 KB
/
CronJobber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* CronJobberPhp (http://github.com/CoreyLoose/CronJobberPhp)
*
* Licensed under The Clear BSD License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2011, Corey Losenegger (http://coreyloose.com)
* @license Clear BSD (http://labs.metacarta.com/license-explanation.html)
*/
class libs_CronJobberPhp_CronJobber
{
const JOB_FILE_NAME = 'jobs';
const LOG_FILE_NAME = '.log';
const LOCK_DIR_NAME = '.lock';
public $logFileLocation;
public $lockFileDir;
private $_cliParams;
public function __construct( $cliParams = array() )
{
$this->_cliParams = $cliParams;
$this->jobFile = dirname(__FILE__).'/'.self::JOB_FILE_NAME;
$this->logFileLocation = dirname(__FILE__).'/'.self::LOG_FILE_NAME;
$this->lockFileDir = dirname(__FILE__).'/'.self::LOCK_DIR_NAME;
}
public function run()
{
$jobs =
$this->createJobs(
$this->parseJobFile(
$this->loadJobFile($this->jobFile)
),
$this->_cliParams
);
$logs =
$this->parseLogFile(
$this->loadLogFileIfExists($this->logFileLocation)
);
$this->ensureLockDirectoryExists($this->lockFileDir);
foreach( $jobs as $job ) {
if( !isset($logs[$job->hash])
|| $job->shouldRunNow($logs[$job->hash]) )
{
if( $this->getLockForJob($job, $this->lockFileDir) ) {
$job->runAsync();
$logs[$job->hash] = time();
}
}
}
$this->writeLogFile($this->logFileLocation, $logs);
}
public function loadJobFile( $file )
{
if( !file_exists($file) ) {
throw new Exception('Unable to load job file: "'.$file.'"');
}
return file_get_contents($file);
}
public function loadLogFileIfExists( $file )
{
if( !file_exists($file) ) {
return FALSE;
}
return file_get_contents($file);
}
public function writeLogFile( $fileLocation, $logHashes )
{
$fileContents = '';
foreach( $logHashes as $hash => $time ) {
$fileContents .= $hash.' '.date('Y-m-d H:i:s', $time)."\n";
}
file_put_contents($fileLocation, $fileContents);
}
public function parseJobFile( $fileContents )
{
$jobsHash = array();
foreach( explode("\n",$fileContents) as $jobLine ) {
$trimmedJobLine = trim($jobLine);
if( $trimmedJobLine == FALSE || $trimmedJobLine[0] == '#' ) {
continue;
}
$newJobHash = $this->hashJobLine($trimmedJobLine);
if( isset($jobsHash[$newJobHash]) ) {
throw new Exception('Duplicate job: "'.$trimmedJobLine.'"');
}
$jobsHash[$newJobHash] = $trimmedJobLine;
}
return $jobsHash;
}
public function parseLogFile( $logFileContents )
{
$logHashes = array();
foreach( explode("\n",$logFileContents) as $logLine ) {
$trimmedLogLine = trim($logLine);
if( $trimmedLogLine == FALSE ) {
continue;
}
$logBits = explode(' ', $trimmedLogLine);
if( count($logBits) < 3 ) {
throw new Exception('Invalid Log line "'.$trimmedLogLine.'"');
}
$logHash = $logBits[0];
$logTime = $logBits[1].' '.$logBits[2];
if( isset($logHashes[$logHash]) ) {
throw new Exception('Duplicate log: "'.$logHash.'"');
}
$logHashes[$logHash] = strtotime($logTime);
}
return $logHashes;
}
public function createJobs( $jobsHash, $replaceVars = array() )
{
$jobs = array();
foreach( $jobsHash as $hash => $job ) {
$firstSpaceLocation = strpos($job, ' ');
$timeStr = substr($job, 0, $firstSpaceLocation);
$cmd = trim(substr($job, $firstSpaceLocation));
if( !empty($replaceVars) ) {
$cmd = $this->processReplaceVarsForCmd($cmd, $replaceVars);
}
$jobs[] = new libs_CronJobberPhp_Job($timeStr, $cmd, $hash);
}
return $jobs;
}
public function processReplaceVarsForCmd( $cmd, $replaceVars )
{
$search = array();
$replace = array();
foreach( $replaceVars as $findKey => $replaceVal ) {
$search[] = '{'.$findKey.'}';
$replace[] = $replaceVal;
}
return str_replace($search, $replace, $cmd);
}
public function ensureLockDirectoryExists( $lockDir )
{
if( !file_exists($lockDir) ) {
mkdir($lockDir);
}
}
public function getLockForJob( libs_CronJobberPhp_Job $job, $lockDir )
{
$lockFile = $lockDir.'/'.$job->hash;
if( file_exists($lockFile) ) {
return FALSE;
}
touch($lockFile);
return TRUE;
}
public function releaseLockForJob( libs_CronJobberPhp_Job $job, $lockDir )
{
$lockFile = $lockDir.'/'.$job->hash;
if( !file_exists($lockFile) ) {
return FALSE;
}
unlink($lockFile);
return TRUE;
}
public function hashJobLine( $line )
{
return md5($line);
}
}