CronJob::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug Best Practice introduced by
The property jobNode does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $actualDir does not seem to be defined for all execution paths leading up to this point.
Loading history...
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