@@ -27,151 +27,151 @@ |
||
| 27 | 27 | |
| 28 | 28 | class Task { |
| 29 | 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(); |
|
| 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)) { |
|
| 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); |
|
| 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()); |
|
| 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 | - } |
|
| 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(); |
|
| 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)) { |
|
| 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); |
|
| 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()); |
|
| 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 | 175 | |
| 176 | 176 | } |
| 177 | 177 | |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | } |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - public function load (int $id) { |
|
| 38 | + public function load(int $id) { |
|
| 39 | 39 | $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}tasks` WHERE `id` = :id; ", array( |
| 40 | 40 | 'id' => array( |
| 41 | 41 | 'type' => PDO::PARAM_INT, |
@@ -50,23 +50,23 @@ discard block |
||
| 50 | 50 | $this->row = $row; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - public function getID () : int { |
|
| 53 | + public function getID() : int { |
|
| 54 | 54 | return $this->row['id']; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - public function getTitle () : string { |
|
| 57 | + public function getTitle() : string { |
|
| 58 | 58 | return $this->row['title']; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - public function getType () : string { |
|
| 61 | + public function getType() : string { |
|
| 62 | 62 | return $this->row['type']; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - public function getTypeParams () : array { |
|
| 65 | + public function getTypeParams() : array { |
|
| 66 | 66 | return $this->row['type_params']; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - public function getFile () : string { |
|
| 69 | + public function getFile() : string { |
|
| 70 | 70 | if (!PHPUtils::strEqs($this->getType(), "FILE")) { |
| 71 | 71 | throw new IllegalStateException("Task with id '" . $this->getID() . "' is not of type 'FILE', so cannot return file path in method Task::getFile()."); |
| 72 | 72 | } |
@@ -74,14 +74,14 @@ discard block |
||
| 74 | 74 | return $this->getTypeParams(); |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - public function getParams () : array { |
|
| 77 | + public function getParams() : array { |
|
| 78 | 78 | return unserialize($this->row['params']); |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | /** |
| 82 | 82 | * try to lock task |
| 83 | 83 | */ |
| 84 | - public function lock () : bool { |
|
| 84 | + public function lock() : bool { |
|
| 85 | 85 | if (Cache::contains("task-locks", "task-" . $this->getID)) { |
| 86 | 86 | return false; |
| 87 | 87 | } |
@@ -91,11 +91,11 @@ discard block |
||
| 91 | 91 | return true; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - public function unlock () { |
|
| 94 | + public function unlock() { |
|
| 95 | 95 | Cache::clear("task-locks", "task-" . $this->getID); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - public function execute () : bool { |
|
| 98 | + public function execute() : bool { |
|
| 99 | 99 | //first try to lock task, so other scripts cannot execute same task at same time |
| 100 | 100 | if (!$this->lock()) { |
| 101 | 101 | return false; |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | /** |
| 146 | 146 | * set last execution timestamp to now |
| 147 | 147 | */ |
| 148 | - public function setLastExecution () { |
|
| 148 | + public function setLastExecution() { |
|
| 149 | 149 | //update database, set last execution timestamp to now |
| 150 | 150 | Database::getInstance()->execute("UPDATE `{praefix}tasks` SET `last_execution` = NOW() WHERE `id` = :id; ", array( |
| 151 | 151 | 'id' => array( |
@@ -155,11 +155,11 @@ discard block |
||
| 155 | 155 | )); |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - public function deleteAfterExecution () : bool { |
|
| 158 | + public function deleteAfterExecution() : bool { |
|
| 159 | 159 | return $this->row['delete_after_execution'] == 1; |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | - public function delete () { |
|
| 162 | + public function delete() { |
|
| 163 | 163 | //delete from database |
| 164 | 164 | Database::getInstance()->execute("DELETE FROM `{praefix}tasks` WHERE `id` = :id; ", array( |
| 165 | 165 | 'id' => array( |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | )); |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | - public static function cast (Task $task) : Task { |
|
| 172 | + public static function cast(Task $task) : Task { |
|
| 173 | 173 | return $task; |
| 174 | 174 | } |
| 175 | 175 | |
@@ -18,42 +18,42 @@ |
||
| 18 | 18 | |
| 19 | 19 | class Tasks { |
| 20 | 20 | |
| 21 | - public static function schedule (int $limit = 3) { |
|
| 22 | - //execute overdued tasks |
|
| 23 | - foreach (self::getOverduedTasks($limit) as $task) { |
|
| 24 | - //cast task |
|
| 25 | - $task = Task::cast($task); |
|
| 26 | - |
|
| 27 | - //execute task |
|
| 28 | - $task->execute(); |
|
| 29 | - |
|
| 30 | - //check, if task is a one-time task |
|
| 31 | - if ($task->deleteAfterExecution()) { |
|
| 32 | - //delete task |
|
| 33 | - $task->delete(); |
|
| 34 | - } else { |
|
| 35 | - //update last execution timestamp |
|
| 36 | - $task->setLastExecution(); |
|
| 37 | - } |
|
| 38 | - } |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public static function getOverduedTasks (int $limit = 10) : array { |
|
| 42 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}tasks` WHERE (DATE_ADD(`last_execution`, INTERVAL `interval` MINUTE) < NOW() OR `last_execution` = '0000-00-00 00:00:00') AND `activated` = '1' LIMIT 0, :limit; ", array( |
|
| 43 | - 'limit' => array( |
|
| 44 | - 'type' => PDO::PARAM_INT, |
|
| 45 | - 'value' => $limit |
|
| 46 | - ) |
|
| 47 | - )); |
|
| 48 | - |
|
| 49 | - $tasks = array(); |
|
| 50 | - |
|
| 51 | - foreach ($rows as $row) { |
|
| 52 | - $tasks[] = new Task($row); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - return $tasks; |
|
| 56 | - } |
|
| 21 | + public static function schedule (int $limit = 3) { |
|
| 22 | + //execute overdued tasks |
|
| 23 | + foreach (self::getOverduedTasks($limit) as $task) { |
|
| 24 | + //cast task |
|
| 25 | + $task = Task::cast($task); |
|
| 26 | + |
|
| 27 | + //execute task |
|
| 28 | + $task->execute(); |
|
| 29 | + |
|
| 30 | + //check, if task is a one-time task |
|
| 31 | + if ($task->deleteAfterExecution()) { |
|
| 32 | + //delete task |
|
| 33 | + $task->delete(); |
|
| 34 | + } else { |
|
| 35 | + //update last execution timestamp |
|
| 36 | + $task->setLastExecution(); |
|
| 37 | + } |
|
| 38 | + } |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public static function getOverduedTasks (int $limit = 10) : array { |
|
| 42 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}tasks` WHERE (DATE_ADD(`last_execution`, INTERVAL `interval` MINUTE) < NOW() OR `last_execution` = '0000-00-00 00:00:00') AND `activated` = '1' LIMIT 0, :limit; ", array( |
|
| 43 | + 'limit' => array( |
|
| 44 | + 'type' => PDO::PARAM_INT, |
|
| 45 | + 'value' => $limit |
|
| 46 | + ) |
|
| 47 | + )); |
|
| 48 | + |
|
| 49 | + $tasks = array(); |
|
| 50 | + |
|
| 51 | + foreach ($rows as $row) { |
|
| 52 | + $tasks[] = new Task($row); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + return $tasks; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | } |
| 59 | 59 | |