1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Appserver\Core\Scanner\CronScanner |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/appserver |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Appserver\Core\Scanner; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Appserver\Core\Api\Node\JobNodeInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The executor thread for a CRON job. |
27
|
|
|
* |
28
|
|
|
* @author Tim Wagner <[email protected]> |
29
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
31
|
|
|
* @link https://github.com/appserver-io/appserver |
32
|
|
|
* @link http://www.appserver.io |
33
|
|
|
*/ |
34
|
|
|
class CronJob extends \Thread |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initializes the CRON job with the job information and starts immediately. |
39
|
|
|
* |
40
|
|
|
* @param \AppserverIo\Appserver\Core\Api\Node\JobNodeInterface $jobNode The job information |
41
|
|
|
*/ |
42
|
|
|
public function __construct(JobNodeInterface $jobNode) |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
// initialize the job information |
46
|
|
|
$this->jobNode = $jobNode; |
|
|
|
|
47
|
|
|
|
48
|
|
|
// immediately start the job |
49
|
|
|
$this->start(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Main method that executes the CRON job in a separate thread. |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function run() |
58
|
|
|
{ |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
// register shutdown handler |
62
|
|
|
register_shutdown_function(array(&$this, "shutdown")); |
63
|
|
|
|
64
|
|
|
// store the actual directory |
65
|
|
|
$actualDir = getcwd(); |
66
|
|
|
|
67
|
|
|
// load the node data with the command information |
68
|
|
|
$executeNode = $this->jobNode->getExecute(); |
69
|
|
|
|
70
|
|
|
// try to load the script from the configuration |
71
|
|
|
if ($script = $executeNode->getScript()) { |
72
|
|
|
// change the working directory |
73
|
|
|
if ($execDir = $executeNode->getDirectory()) { |
74
|
|
|
chdir($execDir); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// check if the configured script is a file |
78
|
|
|
if (is_file($script) && is_executable($script)) { |
79
|
|
|
// initialize the exec params |
80
|
|
|
$output = array(); |
81
|
|
|
$returnVar = 0; |
82
|
|
|
|
83
|
|
|
// execute the script on the command line |
84
|
|
|
exec($executeNode, $output, $returnVar); |
85
|
|
|
|
86
|
|
|
// restore the old directory |
87
|
|
|
if ($execDir && $actualDir) { |
88
|
|
|
chdir($actualDir); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// query whether the script has been executed or not |
92
|
|
|
if ($returnVar !== 0) { |
93
|
|
|
throw new \Exception(implode(PHP_EOL, $output)); |
94
|
|
|
} else { |
95
|
|
|
error_log(implode(PHP_EOL, $output)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} else { |
99
|
|
|
throw new \Exception(sprintf('Script %s is not a file or not executable', $script)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} else { |
103
|
|
|
throw new \Exception(sprintf('Can\t find a script configured in job', $this->jobNode->getName())); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} catch (\Exception $e) { |
107
|
|
|
// restore the old directory |
108
|
|
|
chdir($actualDir); |
|
|
|
|
109
|
|
|
// log the exception |
110
|
|
|
error_log($e->__toString()); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Does shutdown logic for request handler if something went wrong and |
116
|
|
|
* produces a fatal error for example. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
public function shutdown() |
121
|
|
|
{ |
122
|
|
|
|
123
|
|
|
// check if there was a fatal error caused shutdown |
124
|
|
|
if ($lastError = error_get_last()) { |
125
|
|
|
// initialize type + message |
126
|
|
|
$type = 0; |
127
|
|
|
$message = ''; |
128
|
|
|
// extract the last error values |
129
|
|
|
extract($lastError); |
130
|
|
|
// query whether we've a fatal/user error |
131
|
|
|
if ($type === E_ERROR || $type === E_USER_ERROR) { |
132
|
|
|
error_log($message); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|