|
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: JuKuCMS |
|
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(); |
|
|
|
|
|
|
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 static function cast (Task $task) : Task { |
|
159
|
|
|
return $task; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
?> |
|
|
|
|
|
|
165
|
|
|
|