JuKu /
JuKuCMS
| 1 | <?php |
||||
| 2 | |||||
| 3 | /** |
||||
| 4 | * Copyright (c) 2018 Justin Kuenzel (jukusoft.com) |
||||
| 5 | * |
||||
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||||
| 7 | * you may not use this file except in compliance with the License. |
||||
| 8 | * You may obtain a copy of the License at |
||||
| 9 | * |
||||
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
||||
| 11 | * |
||||
| 12 | * Unless required by applicable law or agreed to in writing, software |
||||
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
||||
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
| 15 | * See the License for the specific language governing permissions and |
||||
| 16 | * limitations under the License. |
||||
| 17 | */ |
||||
| 18 | |||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * Project: RocketCMS |
||||
| 22 | * License: Apache 2.0 license |
||||
| 23 | * User: Justin |
||||
| 24 | * Date: 16.04.2018 |
||||
| 25 | * Time: 20:29 |
||||
| 26 | */ |
||||
| 27 | |||||
| 28 | class Task { |
||||
| 29 | |||||
| 30 | protected $row = null; |
||||
| 31 | |||||
| 32 | public function __construct(array $row = array()) { |
||||
| 33 | if (!empty($row)) { |
||||
| 34 | $this->row = $row; |
||||
| 35 | } |
||||
| 36 | } |
||||
| 37 | |||||
| 38 | public function load (int $id) { |
||||
| 39 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}tasks` WHERE `id` = :id; ", array( |
||||
| 40 | 'id' => array( |
||||
| 41 | 'type' => PDO::PARAM_INT, |
||||
| 42 | 'value' => $id |
||||
| 43 | ) |
||||
| 44 | )); |
||||
| 45 | |||||
| 46 | if (!$row || empty($row)) { |
||||
| 47 | throw new IllegalArgumentException("Task with id '" . $id . "' doesnt exists in database!"); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | $this->row = $row; |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | public function getID () : int { |
||||
| 54 | return $this->row['id']; |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | public function getTitle () : string { |
||||
| 58 | return $this->row['title']; |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | public function getType () : string { |
||||
| 62 | return $this->row['type']; |
||||
| 63 | } |
||||
| 64 | |||||
| 65 | public function getTypeParams () : array { |
||||
| 66 | return $this->row['type_params']; |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | public function getFile () : string { |
||||
| 70 | if (!PHPUtils::strEqs($this->getType(), "FILE")) { |
||||
| 71 | throw new IllegalStateException("Task with id '" . $this->getID() . "' is not of type 'FILE', so cannot return file path in method Task::getFile()."); |
||||
| 72 | } |
||||
| 73 | |||||
| 74 | return $this->getTypeParams(); |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 75 | } |
||||
| 76 | |||||
| 77 | public function getParams () : array { |
||||
| 78 | return unserialize($this->row['params']); |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | /** |
||||
| 82 | * try to lock task |
||||
| 83 | */ |
||||
| 84 | public function lock () : bool { |
||||
| 85 | if (Cache::contains("task-locks", "task-" . $this->getID)) { |
||||
|
0 ignored issues
–
show
|
|||||
| 86 | return false; |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | Cache::put("task-locks", "task-" . $this->getID, time()); |
||||
| 90 | |||||
| 91 | return true; |
||||
| 92 | } |
||||
| 93 | |||||
| 94 | public function unlock () { |
||||
| 95 | Cache::clear("task-locks", "task-" . $this->getID); |
||||
|
0 ignored issues
–
show
|
|||||
| 96 | } |
||||
| 97 | |||||
| 98 | public function execute () : bool { |
||||
| 99 | //first try to lock task, so other scripts cannot execute same task at same time |
||||
| 100 | if (!$this->lock()) { |
||||
| 101 | return false; |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | try { |
||||
| 105 | $array = explode(":", $this->getTypeParams()); |
||||
|
0 ignored issues
–
show
$this->getTypeParams() of type array is incompatible with the type string expected by parameter $string of explode().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 106 | $params = $this->getParams(); |
||||
| 107 | |||||
| 108 | switch (strtolower($this->getType())) { |
||||
| 109 | case "file": |
||||
| 110 | $file = $this->getFile(); |
||||
| 111 | |||||
| 112 | //check, if file exists |
||||
| 113 | if (file_exists(ROOT_PATH . $file)) { |
||||
| 114 | require(ROOT_PATH . $file); |
||||
| 115 | } else { |
||||
| 116 | throw new IllegalStateException("required file for task with id '" . $this->getID() . "' not found: " . $file); |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | break; |
||||
| 120 | case "function": |
||||
| 121 | $class_method = $array[0]; |
||||
| 122 | |||||
| 123 | call_user_func($class_method, $params); |
||||
| 124 | break; |
||||
| 125 | case "class_static_method": |
||||
| 126 | $class_name = $array[0]; |
||||
| 127 | $class_method = $array[1]; |
||||
| 128 | |||||
| 129 | call_user_func(array($class_name, $class_method), $params); |
||||
| 130 | break; |
||||
| 131 | default: |
||||
| 132 | throw new IllegalStateException("unknown task type '" . $this->getType() . "' for task id '" . $this->getID() . "'!"); |
||||
| 133 | break; |
||||
| 134 | } |
||||
| 135 | } catch (Exception $e) { |
||||
| 136 | echo $e->getTraceAsString(); |
||||
| 137 | } finally { |
||||
| 138 | //unlock task |
||||
| 139 | $this->unlock(); |
||||
| 140 | } |
||||
| 141 | |||||
| 142 | return true; |
||||
| 143 | } |
||||
| 144 | |||||
| 145 | /** |
||||
| 146 | * set last execution timestamp to now |
||||
| 147 | */ |
||||
| 148 | public function setLastExecution () { |
||||
| 149 | //update database, set last execution timestamp to now |
||||
| 150 | Database::getInstance()->execute("UPDATE `{praefix}tasks` SET `last_execution` = NOW() WHERE `id` = :id; ", array( |
||||
| 151 | 'id' => array( |
||||
| 152 | 'type' => PDO::PARAM_INT, |
||||
| 153 | 'value' => $this->getID() |
||||
| 154 | ) |
||||
| 155 | )); |
||||
| 156 | } |
||||
| 157 | |||||
| 158 | public function deleteAfterExecution () : bool { |
||||
| 159 | return $this->row['delete_after_execution'] == 1; |
||||
| 160 | } |
||||
| 161 | |||||
| 162 | public function delete () { |
||||
| 163 | //delete from database |
||||
| 164 | Database::getInstance()->execute("DELETE FROM `{praefix}tasks` WHERE `id` = :id; ", array( |
||||
| 165 | 'id' => array( |
||||
| 166 | 'type' => PDO::PARAM_INT, |
||||
| 167 | 'value' => $this->getID() |
||||
| 168 | ) |
||||
| 169 | )); |
||||
| 170 | } |
||||
| 171 | |||||
| 172 | public static function cast (Task $task) : Task { |
||||
| 173 | return $task; |
||||
| 174 | } |
||||
| 175 | |||||
| 176 | public static function createStaticMethodTask (string $title, string $classname, string $method, int $interval, string $unique_name = null, string $owner = "user", $params = array(), bool $delete_after_execution = false) : string { |
||||
| 177 | $type_params = $classname . ":" . $method; |
||||
| 178 | |||||
| 179 | if ($unique_name == null) { |
||||
|
0 ignored issues
–
show
|
|||||
| 180 | //generate random unique string |
||||
| 181 | $unique_name = PHPUtils::randomString(20); |
||||
| 182 | } |
||||
| 183 | |||||
| 184 | Database::getInstance()->execute("INSERT INTO `{praefix}tasks` ( |
||||
| 185 | `id`, `title`, `unique_name`, `type`, `type_params`, `params`, `owner`, `delete_after_execution`, `interval`, `last_execution`, `activated` |
||||
| 186 | ) VALUES ( |
||||
| 187 | NULL, :title, :unique_name, 'CLASS_STATIC_METHOD', :type_params, :params, :owner, :delete_after_execution, :interval_minutes, '0000-00-00 00:00:00', 1 |
||||
| 188 | ) ON DUPLICATE KEY UPDATE `title` = :title, `type` = 'CLASS_STATIC_METHOD', `type_params` = :type_params, `params` = :params, `owner` = :owner, `delete_after_execution` = :delete_after_execution, `interval` = :interval_minutes; ", array( |
||||
| 189 | 'title' => $title, |
||||
| 190 | 'unique_name' => $unique_name, |
||||
| 191 | 'type_params' => $type_params, |
||||
| 192 | 'params' => serialize($params), |
||||
| 193 | 'owner' => $owner, |
||||
| 194 | 'delete_after_execution' => ($delete_after_execution ? 1 : 0), |
||||
| 195 | 'interval_minutes' => $interval |
||||
| 196 | )); |
||||
| 197 | |||||
| 198 | return Database::getInstance()->lastInsertId(); |
||||
| 199 | } |
||||
| 200 | |||||
| 201 | } |
||||
| 202 | |||||
| 203 | ?> |
||||
|
0 ignored issues
–
show
It is not recommended to use PHP's closing tag
?> in files other than templates.
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore. A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever. Loading history...
|
|||||
| 204 |