Completed
Push — develop ( f87436...460a32 )
by John
08:04
created

CronManager::getTaskClassNames()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.1448
c 0
b 0
f 0
cc 5
nc 4
nop 0
1
<?php
2
3
namespace Alpha\Task;
4
5
require_once './vendor/autoload.php';
6
7
use Alpha\Util\Logging\Logger;
8
use Alpha\Util\Config\ConfigProvider;
9
10
/**
11
 * The main class responsible for running custom cron tasks found under the [webapp]/Task
12
 * directory.  This class should be executed from Linux cron via the CLI.
13
 *
14
 * To run: cd to app.root, then "php vendor/alphadevx/alpha/Alpha/Task/CronManager.php"
15
 *
16
 * @since 1.0
17
 *
18
 * @author John Collins <[email protected]>
19
 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
20
 * @copyright Copyright (c) 2019, John Collins (founder of Alpha Framework).
21
 * All rights reserved.
22
 *
23
 * <pre>
24
 * Redistribution and use in source and binary forms, with or
25
 * without modification, are permitted provided that the
26
 * following conditions are met:
27
 *
28
 * * Redistributions of source code must retain the above
29
 *   copyright notice, this list of conditions and the
30
 *   following disclaimer.
31
 * * Redistributions in binary form must reproduce the above
32
 *   copyright notice, this list of conditions and the
33
 *   following disclaimer in the documentation and/or other
34
 *   materials provided with the distribution.
35
 * * Neither the name of the Alpha Framework nor the names
36
 *   of its contributors may be used to endorse or promote
37
 *   products derived from this software without specific
38
 *   prior written permission.
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
41
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
42
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
45
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
51
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
52
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53
 * </pre>
54
 */
55
class CronManager
56
{
57
    /**
58
     * Trace logger.
59
     *
60
     * @var \Alpha\Util\Logging\Logger
61
     *
62
     * @since 1.0
63
     */
64
    private static $logger = null;
65
66
    /**
67
     * Constructor.
68
     *
69
     * @since 1.0
70
     */
71
    public function __construct()
72
    {
73
        $config = ConfigProvider::getInstance();
74
75
        self::$logger = new Logger('CronManager');
76
        self::$logger->setLogProviderFile($config->get('app.file.store.dir').'logs/tasks.log');
77
78
        self::$logger->debug('>>__construct()');
79
80
        self::$logger->info('New CronManager invoked');
81
82
        $taskList = self::getTaskClassNames();
83
84
        self::$logger->info('Found ['.count($taskList).'] tasks in the directory ['.$config->get('app.root').'src/Task]');
85
86
        foreach ($taskList as $taskClass) {
87
            $taskClass = $taskClass;
0 ignored issues
show
Bug introduced by
Why assign $taskClass to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
88
            self::$logger->info('Loading task ['.$taskClass.']');
89
            $task = new $taskClass();
90
91
            $startTime = microtime(true);
92
93
            self::$logger->info('Start time is ['.$startTime.'], maximum task run time is ['.$task->getMaxRunTime().']');
94
95
            // only continue to execute for the task max time
96
            set_time_limit($task->getMaxRunTime());
97
            $task->doTask();
98
99
            self::$logger->info('Done in ['.round(microtime(true)-$startTime, 5).'] seconds');
100
        }
101
102
        self::$logger->info('Finished processing all cron tasks');
103
104
        self::$logger->debug('<<__construct');
105
    }
106
107
    /**
108
     * Loops over the /tasks directory and builds an array of all of the task
109
     * class names in the system.
110
     *
111
     * @return array
112
     *
113
     * @since 1.0
114
     */
115
    public static function getTaskClassNames()
116
    {
117
        $config = ConfigProvider::getInstance();
118
119
        if (self::$logger == null) {
120
            self::$logger = new Logger('CronManager');
121
            self::$logger->setLogProviderFile($config->get('app.file.store.dir').'logs/tasks.log');
122
        }
123
        self::$logger->info('>>getTaskClassNames()');
124
125
        $classNameArray = array();
126
127
        if (file_exists($config->get('app.root').'src/Task')) {
128
            $handle = opendir($config->get('app.root').'src/Task');
129
130
            // loop over the custom task directory
131
            while (false !== ($file = readdir($handle))) {
132
                if (preg_match('/Task.php/', $file)) {
133
                    $classname = 'Task\\'.mb_substr($file, 0, -4);
134
135
                    array_push($classNameArray, $classname);
136
                }
137
            }
138
        }
139
140
        self::$logger->info('<<getTaskClassNames ['.var_export($classNameArray, true).']');
141
142
        return $classNameArray;
143
    }
144
}
145
146
// invoke a cron manager object
147
$processor = new CronManager();
148