This repository has been archived by the owner on May 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
queue-for-kirby.php
256 lines (214 loc) · 6.63 KB
/
queue-for-kirby.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
// Setup some panel things
if(class_exists('Panel')) require(__DIR__ . DS . 'panel' . DS . 'init.php');
class Job extends Obj
{
public function get($key, $default = null)
{
return isset($this->data[$key]) ? $this->data[$key] : $default;
}
}
class Queue
{
private static $actions = [];
public static $current_job;
/**
* Defines an action to perform when job is worked on
* @param string Name of the action
* @param Callable Closure with the action
*/
public static function define($name, $action)
{
static::$actions[$name] = $action;
}
/**
* Adds a new job to the queue
* @param string Name of the action to be performed
* @param mixed Any data you want to pass in
*/
public static function add($name, $data = null)
{
$id = uniqid();
$jobfile = static::path() . DS . $id . '.yml';
yaml::write($jobfile, [
'id' => $id,
'added' => date('c'),
'name' => $name,
'data' => $data
]);
}
/**
* Adds a failed job back to the queue
* @param string|Job ID or Job object to be retried
* @throws Error When a non-Job object is given
* @throws Error When failed Job with ID is not found
*/
public static function retry($failedJob)
{
if (is_string($failedJob)) {
$failedJob = static::_find_failed_by_id($failedJob);
}
if (!is_a($failedJob, 'Job')) {
throw new Error('queue::retry() expects a Job object');
}
f::move(
static::failedPath() . DS . $failedJob->id() . '.yml',
static::path() . DS . $failedJob->id() . '.yml'
);
}
/**
* Removes a failed job
* @param string|Job ID or Job object to be deleted
* @throws Error When a non-Job object is given
* @throws Error When failed Job with ID is not found
*/
public static function remove($failedJob)
{
if (is_string($failedJob)) {
$failedJob = static::_find_failed_by_id($failedJob);
}
if (!is_a($failedJob, 'Job')) {
throw new Error('queue::remove() expects a Job object');
}
f::remove(static::failedPath() . DS . $failedJob->id() . '.yml');
}
private static function failed($job, $error)
{
$jobfile = static::failedPath() . DS . $job->id() . '.yml';
yaml::write($jobfile, [
'id' => $job->id(),
'added' => $job->added(),
'name' => $job->name(),
'data' => $job->data(),
'error' => $error,
'tried' => date('c')
]);
}
/**
* Executes the first job in the queue folder
*/
public static function work()
{
// Protect ourselfs against multiple workers at once
if (static::isWorking()) exit();
static::setWorking();
register_shutdown_function(function(){
if(queue::$current_job) {
queue::failed(queue::$current_job, 'Job action terminated execution');
queue::stopWorking();
}
});
if (static::hasJobs()) {
static::$current_job = static::_get_next_job();
try {
if (!isset(static::$actions[static::$current_job->name()])
or !is_callable(static::$actions[static::$current_job->name()])) {
throw new Error("Action '" . static::$current_job->name() . "'' not defined");
}
if (call_user_func(static::$actions[static::$current_job->name()], static::$current_job) === false) {
throw new Error('Job returned false');
}
} catch (Exception $e) {
static::failed(static::$current_job, $e->getMessage());
} catch (Error $e) {
static::failed(static::$current_job, $e->getMessage());
}
}
static::stopWorking();
}
private static function _jobs($failed)
{
$path = static::path();
if($failed) $path = static::failedPath();
$jobs = dir::read($path);
$jobs = array_filter($jobs, function($job) {
return substr($job,0,1) != '.';
});
$jobs = array_map(function ($job) use ($path) {
return new Job(yaml::read($path . DS . $job));
}, $jobs);
return new Collection($jobs);
}
/**
* Returns all jobs in the queue
* @return Collection Collection with Job objects
*/
public static function jobs()
{
return static::_jobs(false);
}
/**
* Returns all failed jobs
* @return Collection Collection with Job objects
*/
public static function failedJobs()
{
return static::_jobs(true);
}
/**
* @return boolean
*/
public static function hasJobs()
{
return static::_get_next_jobfile() !== false;
}
/**
* Removes all jobs from the queue, including failed jobs
*/
public static function flush()
{
dir::clean(static::path());
}
private static function _find_failed_by_id($id)
{
$filename = static::failedPath() . DS . $id . '.yml';
if (!f::exists($filename)) throw new Error('Job not found');
return new Job(yaml::read($filename));
}
private static function _get_next_jobfile()
{
foreach(dir::read(static::path()) as $jobfile) {
// No .working or .DS_store
if (substr($jobfile,0,1) == '.') continue;
// Return first jobfile we find
return $jobfile;
}
return false;
}
private static function _get_next_job()
{
$jobfile = static::_get_next_jobfile();
$job = yaml::read(static::path() . DS . $jobfile);
f::remove(static::path() . DS . $jobfile);
return new Job($job);
}
/**
* Returns the full path of the queue folder
* @return string
*/
public static function path()
{
return kirby()->roots()->site() . DS . 'queue';
}
/**
* Returns the full path of the failed jobs folder
* @return string
*/
public static function failedPath()
{
return static::path() . DS . '.failed';
}
public static function isWorking()
{
return f::exists(static::path() . DS . '.working');
}
public static function setWorking()
{
dir::make(static::path() . DS . '.working');
}
public static function stopWorking()
{
queue::$current_job = null;
dir::remove(static::path() . DS . '.working');
}
}