1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* |
6
|
|
|
* @author Marcus Nyeholt <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
class QueuedJobsTest extends SapphireTest { |
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* We need the DB for this test |
12
|
|
|
* |
13
|
|
|
* @var bool |
14
|
|
|
*/ |
15
|
|
|
protected $usesDatabase = true; |
16
|
|
|
|
17
|
|
|
public function setUp() { |
18
|
|
|
parent::setUp(); |
19
|
|
|
|
20
|
|
|
Config::nest(); |
21
|
|
|
// Two restarts are allowed per job |
22
|
|
|
Config::inst()->update('QueuedJobService', 'stall_threshold', 2); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function tearDown() { |
26
|
|
|
Config::unnest(); |
27
|
|
|
parent::tearDown(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return QueuedJobService |
33
|
|
|
*/ |
34
|
|
|
protected function getService() { |
35
|
|
|
return singleton("TestQJService"); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testQueueJob() { |
39
|
|
|
$svc = $this->getService(); |
40
|
|
|
|
41
|
|
|
// lets create a new job and add it tio the queue |
42
|
|
|
$job = new TestQueuedJob(); |
43
|
|
|
$jobId = $svc->queueJob($job); |
44
|
|
|
$list = $svc->getJobList(); |
45
|
|
|
|
46
|
|
|
$this->assertEquals(1, $list->count()); |
|
|
|
|
47
|
|
|
|
48
|
|
|
$myJob = null; |
49
|
|
|
foreach ($list as $job) { |
50
|
|
|
if ($job->Implementation == 'TestQueuedJob') { |
51
|
|
|
$myJob = $job; |
52
|
|
|
break; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->assertNotNull($myJob); |
|
|
|
|
57
|
|
|
$this->assertTrue($jobId > 0); |
|
|
|
|
58
|
|
|
$this->assertEquals('TestQueuedJob', $myJob->Implementation); |
|
|
|
|
59
|
|
|
$this->assertNotNull($myJob->SavedJobData); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testJobRunAs() { |
63
|
|
|
$svc = $this->getService(); |
64
|
|
|
$list = $svc->getJobList(); |
65
|
|
|
foreach ($list as $job) { |
66
|
|
|
$job->delete(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->logInWithPermission('DUMMY'); |
70
|
|
|
|
71
|
|
|
// lets create a new job and add it tio the queue |
72
|
|
|
$job = new TestQueuedJob(); |
73
|
|
|
$job->runningAs = "DUMMY"; |
|
|
|
|
74
|
|
|
$jobId = $svc->queueJob($job); |
|
|
|
|
75
|
|
|
$list = $svc->getJobList(); |
76
|
|
|
|
77
|
|
|
$myJob = $list->First(); |
78
|
|
|
|
79
|
|
|
$this->assertEquals("[email protected]", $myJob->RunAs()->Email); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testQueueSignature() { |
83
|
|
|
$svc = $this->getService(); |
84
|
|
|
|
85
|
|
|
// lets create a new job and add it tio the queue |
86
|
|
|
$job = new TestQueuedJob(); |
87
|
|
|
$jobId = $svc->queueJob($job); |
88
|
|
|
|
89
|
|
|
$newJob = new TestQueuedJob(); |
90
|
|
|
$newId = $svc->queueJob($newJob); |
91
|
|
|
|
92
|
|
|
$this->assertEquals($jobId, $newId); |
|
|
|
|
93
|
|
|
|
94
|
|
|
// now try another, but with different params |
95
|
|
|
$newJob = new TestQueuedJob(); |
96
|
|
|
$newJob->randomParam = 'stuff'; |
|
|
|
|
97
|
|
|
$newId = $svc->queueJob($newJob); |
98
|
|
|
|
99
|
|
|
$this->assertNotEquals($jobId, $newId); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testProcessJob() { |
103
|
|
|
$job = new TestQueuedJob(); |
104
|
|
|
$job->setup(); |
105
|
|
|
$job->process(); |
106
|
|
|
// we should now have some data |
107
|
|
|
$data = $job->getJobData(); |
108
|
|
|
$this->assertNotNull($data->messages); |
|
|
|
|
109
|
|
|
$this->assertFalse($data->isComplete); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$jd = $data->jobData; |
112
|
|
|
$this->assertTrue(isset($jd->times)); |
|
|
|
|
113
|
|
|
$this->assertEquals(1, count($jd->times)); |
|
|
|
|
114
|
|
|
|
115
|
|
|
// now take the 'saved' data and try restoring the job |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testResumeJob() { |
119
|
|
|
$job = new TestQueuedJob(); |
120
|
|
|
$job->setup(); |
121
|
|
|
$job->process(); |
122
|
|
|
// we should now have some data |
123
|
|
|
$data = $job->getJobData(); |
124
|
|
|
|
125
|
|
|
// so create a new job and restore it from this data |
126
|
|
|
|
127
|
|
|
$job = new TestQueuedJob(); |
128
|
|
|
$job->setup(); |
129
|
|
|
|
130
|
|
|
$job->setJobData($data->totalSteps, $data->currentStep, $data->isComplete, $data->jobData, $data->messages); |
131
|
|
|
$job->process(); |
132
|
|
|
|
133
|
|
|
$data = $job->getJobData(); |
134
|
|
|
$this->assertFalse($data->isComplete); |
|
|
|
|
135
|
|
|
$jd = $data->jobData; |
136
|
|
|
$this->assertTrue(isset($jd->times)); |
|
|
|
|
137
|
|
|
$this->assertEquals(2, count($jd->times)); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testInitialiseJob() { |
141
|
|
|
// okay, lets test it out on the actual service |
142
|
|
|
$svc = $this->getService(); |
143
|
|
|
// lets create a new job and add it to the queue |
144
|
|
|
$job = new TestQueuedJob(); |
145
|
|
|
$id = $svc->queueJob($job); |
146
|
|
|
|
147
|
|
|
$descriptor = DataObject::get_by_id('QueuedJobDescriptor', $id); |
148
|
|
|
|
149
|
|
|
$job = $svc->testInit($descriptor); |
150
|
|
|
$this->assertInstanceOf('TestQueuedJob', $job, 'Job has been triggered'); |
|
|
|
|
151
|
|
|
|
152
|
|
|
$descriptor = DataObject::get_by_id('QueuedJobDescriptor', $id); |
153
|
|
|
|
154
|
|
|
$this->assertEquals(QueuedJob::STATUS_INIT, $descriptor->JobStatus); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testStartJob() { |
158
|
|
|
// okay, lets test it out on the actual service |
159
|
|
|
$svc = $this->getService(); |
160
|
|
|
// lets create a new job and add it to the queue |
161
|
|
|
|
162
|
|
|
$this->logInWithPermission('DUMMYUSER'); |
163
|
|
|
|
164
|
|
|
$job = new TestQueuedJob(); |
165
|
|
|
$job->testingStartJob = true; |
|
|
|
|
166
|
|
|
$id = $svc->queueJob($job); |
167
|
|
|
|
168
|
|
|
$this->logInWithPermission('ADMIN'); |
169
|
|
|
|
170
|
|
|
$result = $svc->runJob($id); |
171
|
|
|
$this->assertTrue($result); |
|
|
|
|
172
|
|
|
|
173
|
|
|
// we want to make sure that the current user is the runas user of the job |
174
|
|
|
$descriptor = DataObject::get_by_id('QueuedJobDescriptor', $id); |
175
|
|
|
$this->assertEquals('Complete', $descriptor->JobStatus); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testImmediateQueuedJob() { |
179
|
|
|
// okay, lets test it out on the actual service |
180
|
|
|
$svc = $this->getService(); |
181
|
|
|
// lets create a new job and add it to the queue |
182
|
|
|
|
183
|
|
|
$job = new TestQueuedJob(QueuedJob::IMMEDIATE); |
184
|
|
|
$job->firstJob = true; |
|
|
|
|
185
|
|
|
$id = $svc->queueJob($job); |
|
|
|
|
186
|
|
|
|
187
|
|
|
$job = new TestQueuedJob(QueuedJob::IMMEDIATE); |
188
|
|
|
$job->secondJob = true; |
|
|
|
|
189
|
|
|
$id = $svc->queueJob($job); |
|
|
|
|
190
|
|
|
|
191
|
|
|
$jobs = $svc->getJobList(QueuedJob::IMMEDIATE); |
192
|
|
|
$this->assertEquals(2, $jobs->count()); |
|
|
|
|
193
|
|
|
|
194
|
|
|
// now fake a shutdown |
195
|
|
|
$svc->onShutdown(); |
196
|
|
|
|
197
|
|
|
$jobs = $svc->getJobList(QueuedJob::IMMEDIATE); |
198
|
|
|
$this->assertInstanceOf('DataList', $jobs); |
|
|
|
|
199
|
|
|
$this->assertEquals(0, $jobs->count()); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function testNextJob() { |
203
|
|
|
$svc = $this->getService(); |
204
|
|
|
$list = $svc->getJobList(); |
205
|
|
|
|
206
|
|
|
foreach ($list as $job) { |
207
|
|
|
$job->delete(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$list = $svc->getJobList(); |
211
|
|
|
$this->assertEquals(0, $list->count()); |
|
|
|
|
212
|
|
|
|
213
|
|
|
$job = new TestQueuedJob(); |
214
|
|
|
$id1 = $svc->queueJob($job); |
215
|
|
|
|
216
|
|
|
$job = new TestQueuedJob(); |
217
|
|
|
// to get around the signature checks |
218
|
|
|
$job->randomParam = 'me'; |
|
|
|
|
219
|
|
|
$id2 = $svc->queueJob($job); |
|
|
|
|
220
|
|
|
|
221
|
|
|
$job = new TestQueuedJob(); |
222
|
|
|
// to get around the signature checks |
223
|
|
|
$job->randomParam = 'mo'; |
|
|
|
|
224
|
|
|
$id3 = $svc->queueJob($job); |
225
|
|
|
|
226
|
|
|
$this->assertEquals(2, $id3 - $id1); |
|
|
|
|
227
|
|
|
|
228
|
|
|
$list = $svc->getJobList(); |
229
|
|
|
$this->assertEquals(3, $list->count()); |
|
|
|
|
230
|
|
|
|
231
|
|
|
// okay, lets get the first one and initialise it, then make sure that a subsequent init attempt fails |
232
|
|
|
$job = $svc->getNextPendingJob(); |
233
|
|
|
|
234
|
|
|
$this->assertEquals($id1, $job->ID); |
|
|
|
|
235
|
|
|
$svc->testInit($job); |
236
|
|
|
|
237
|
|
|
// now try and get another, it should be === false |
238
|
|
|
$next = $svc->getNextPendingJob(); |
239
|
|
|
|
240
|
|
|
$this->assertFalse($next); |
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Verify that broken jobs are correctly verified for health and restarted as necessary |
245
|
|
|
* |
246
|
|
|
* Order of checkJobHealth() and getNextPendingJob() is important |
247
|
|
|
* |
248
|
|
|
* Execution of this job is broken into several "loops", each of which represents one invocation |
249
|
|
|
* of ProcessJobQueueTask |
250
|
|
|
*/ |
251
|
|
|
public function testJobHealthCheck() { |
252
|
|
|
// Create a job and add it to the queue |
253
|
|
|
$svc = $this->getService(); |
254
|
|
|
$job = new TestQueuedJob(QueuedJob::IMMEDIATE); |
255
|
|
|
$job->firstJob = true; |
|
|
|
|
256
|
|
|
$id = $svc->queueJob($job); |
257
|
|
|
$descriptor = QueuedJobDescriptor::get()->byID($id); |
258
|
|
|
|
259
|
|
|
// Verify initial state is new and LastProcessedCount is not marked yet |
260
|
|
|
$this->assertEquals(QueuedJob::STATUS_NEW, $descriptor->JobStatus); |
|
|
|
|
261
|
|
|
$this->assertEquals(0, $descriptor->StepsProcessed); |
|
|
|
|
262
|
|
|
$this->assertEquals(-1, $descriptor->LastProcessedCount); |
|
|
|
|
263
|
|
|
$this->assertEquals(0, $descriptor->ResumeCounts); |
|
|
|
|
264
|
|
|
|
265
|
|
|
// Loop 1 - Pick up new job and attempt to run it |
266
|
|
|
// Job health should not attempt to cleanup unstarted jobs |
267
|
|
|
$svc->checkJobHealth(QueuedJob::IMMEDIATE); |
268
|
|
|
$nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE); |
269
|
|
|
|
270
|
|
|
// Ensure that this is the next job ready to go |
271
|
|
|
$descriptor = QueuedJobDescriptor::get()->byID($id); |
272
|
|
|
$this->assertEquals($nextJob->ID, $descriptor->ID); |
|
|
|
|
273
|
|
|
$this->assertEquals(QueuedJob::STATUS_NEW, $descriptor->JobStatus); |
|
|
|
|
274
|
|
|
$this->assertEquals(0, $descriptor->StepsProcessed); |
|
|
|
|
275
|
|
|
$this->assertEquals(-1, $descriptor->LastProcessedCount); |
|
|
|
|
276
|
|
|
$this->assertEquals(0, $descriptor->ResumeCounts); |
|
|
|
|
277
|
|
|
|
278
|
|
|
// Run 1 - Start the job (no work is done) |
279
|
|
|
$descriptor->JobStatus = QueuedJob::STATUS_INIT; |
280
|
|
|
$descriptor->write(); |
281
|
|
|
|
282
|
|
|
// Assume that something bad happens at this point, the process dies during execution, and |
283
|
|
|
// the task is re-initiated somewhere down the track |
284
|
|
|
|
285
|
|
|
// Loop 2 - Detect broken job, and mark it for future checking. |
286
|
|
|
$svc->checkJobHealth(QueuedJob::IMMEDIATE); |
287
|
|
|
$nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE); |
288
|
|
|
|
289
|
|
|
// Note that we don't immediately try to restart it until StepsProcessed = LastProcessedCount |
290
|
|
|
$descriptor = QueuedJobDescriptor::get()->byID($id); |
291
|
|
|
$this->assertFalse($nextJob); // Don't run it this round please! |
|
|
|
|
292
|
|
|
$this->assertEquals(QueuedJob::STATUS_INIT, $descriptor->JobStatus); |
|
|
|
|
293
|
|
|
$this->assertEquals(0, $descriptor->StepsProcessed); |
|
|
|
|
294
|
|
|
$this->assertEquals(0, $descriptor->LastProcessedCount); |
|
|
|
|
295
|
|
|
$this->assertEquals(0, $descriptor->ResumeCounts); |
|
|
|
|
296
|
|
|
|
297
|
|
|
// Loop 3 - We've previously marked this job as broken, so restart it this round |
298
|
|
|
// If no more work has been done on the job at this point, assume that we are able to |
299
|
|
|
// restart it |
300
|
|
|
$svc->checkJobHealth(QueuedJob::IMMEDIATE); |
301
|
|
|
$nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE); |
302
|
|
|
|
303
|
|
|
// This job is resumed and exeuction is attempted this round |
304
|
|
|
$descriptor = QueuedJobDescriptor::get()->byID($id); |
305
|
|
|
$this->assertEquals($nextJob->ID, $descriptor->ID); |
|
|
|
|
306
|
|
|
$this->assertEquals(QueuedJob::STATUS_WAIT, $descriptor->JobStatus); |
|
|
|
|
307
|
|
|
$this->assertEquals(0, $descriptor->StepsProcessed); |
|
|
|
|
308
|
|
|
$this->assertEquals(0, $descriptor->LastProcessedCount); |
|
|
|
|
309
|
|
|
$this->assertEquals(1, $descriptor->ResumeCounts); |
|
|
|
|
310
|
|
|
|
311
|
|
|
// Run 2 - First restart (work is done) |
312
|
|
|
$descriptor->JobStatus = QueuedJob::STATUS_RUN; |
313
|
|
|
$descriptor->StepsProcessed++; // Essentially delays the next restart by 1 loop |
314
|
|
|
$descriptor->write(); |
315
|
|
|
|
316
|
|
|
// Once again, at this point, assume the job fails and crashes |
317
|
|
|
|
318
|
|
|
// Loop 4 - Assuming a job has LastProcessedCount < StepsProcessed we are in the same |
319
|
|
|
// situation as step 2. |
320
|
|
|
// Because the last time the loop ran, StepsProcessed was incremented, |
321
|
|
|
// this indicates that it's likely that another task could be working on this job, so |
322
|
|
|
// don't run this. |
323
|
|
|
$svc->checkJobHealth(QueuedJob::IMMEDIATE); |
324
|
|
|
$nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE); |
325
|
|
|
|
326
|
|
|
$descriptor = QueuedJobDescriptor::get()->byID($id); |
327
|
|
|
$this->assertFalse($nextJob); // Don't run jobs we aren't sure should be restarted |
|
|
|
|
328
|
|
|
$this->assertEquals(QueuedJob::STATUS_RUN, $descriptor->JobStatus); |
|
|
|
|
329
|
|
|
$this->assertEquals(1, $descriptor->StepsProcessed); |
|
|
|
|
330
|
|
|
$this->assertEquals(1, $descriptor->LastProcessedCount); |
|
|
|
|
331
|
|
|
$this->assertEquals(1, $descriptor->ResumeCounts); |
|
|
|
|
332
|
|
|
|
333
|
|
|
// Loop 5 - Job is again found to not have been restarted since last iteration, so perform second |
334
|
|
|
// restart. The job should be attempted to run this loop |
335
|
|
|
$svc->checkJobHealth(QueuedJob::IMMEDIATE); |
336
|
|
|
$nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE); |
337
|
|
|
|
338
|
|
|
// This job is resumed and exeuction is attempted this round |
339
|
|
|
$descriptor = QueuedJobDescriptor::get()->byID($id); |
340
|
|
|
$this->assertEquals($nextJob->ID, $descriptor->ID); |
|
|
|
|
341
|
|
|
$this->assertEquals(QueuedJob::STATUS_WAIT, $descriptor->JobStatus); |
|
|
|
|
342
|
|
|
$this->assertEquals(1, $descriptor->StepsProcessed); |
|
|
|
|
343
|
|
|
$this->assertEquals(1, $descriptor->LastProcessedCount); |
|
|
|
|
344
|
|
|
$this->assertEquals(2, $descriptor->ResumeCounts); |
|
|
|
|
345
|
|
|
|
346
|
|
|
// Run 3 - Second and last restart (no work is done) |
347
|
|
|
$descriptor->JobStatus = QueuedJob::STATUS_RUN; |
348
|
|
|
$descriptor->write(); |
349
|
|
|
|
350
|
|
|
// Loop 6 - As no progress has been made since loop 3, we can mark this as dead |
351
|
|
|
$svc->checkJobHealth(QueuedJob::IMMEDIATE); |
352
|
|
|
$nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE); |
353
|
|
|
|
354
|
|
|
// Since no StepsProcessed has been done, don't wait another loop to mark this as dead |
355
|
|
|
$descriptor = QueuedJobDescriptor::get()->byID($id); |
356
|
|
|
$this->assertEquals(QueuedJob::STATUS_PAUSED, $descriptor->JobStatus); |
|
|
|
|
357
|
|
|
$this->assertEmpty($nextJob); |
|
|
|
|
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
public function testExceptionWithMemoryExhaustion() { |
361
|
|
|
$svc = $this->getService(); |
362
|
|
|
$job = new TestExceptingJob(); |
363
|
|
|
$job->firstJob = true; |
|
|
|
|
364
|
|
|
$id = $svc->queueJob($job); |
365
|
|
|
$descriptor = QueuedJobDescriptor::get()->byID($id); |
|
|
|
|
366
|
|
|
|
367
|
|
|
// we want to set the memory limit _really_ low so that our first run triggers |
368
|
|
|
$mem = Config::inst()->get('QueuedJobService', 'memory_limit'); |
369
|
|
|
Config::inst()->update('QueuedJobService', 'memory_limit', 1); |
370
|
|
|
|
371
|
|
|
$svc->runJob($id); |
372
|
|
|
|
373
|
|
|
Config::inst()->update('QueuedJobService', 'memory_limit', $mem); |
374
|
|
|
|
375
|
|
|
$descriptor = QueuedJobDescriptor::get()->byID($id); |
376
|
|
|
|
377
|
|
|
$this->assertEquals(QueuedJob::STATUS_BROKEN, $descriptor->JobStatus); |
|
|
|
|
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
public function testCheckdefaultJobs() { |
381
|
|
|
// Create a job and add it to the queue |
382
|
|
|
$svc = $this->getService(); |
383
|
|
|
$testDefaultJobsArray = array( |
384
|
|
|
'ArbitraryName' => array( |
385
|
|
|
# I'll get restarted and create an alert email |
386
|
|
|
'type' => 'TestQueuedJob', |
387
|
|
|
'filter' => array( |
388
|
|
|
'JobTitle' => "A Test job" |
389
|
|
|
), |
390
|
|
|
'recreate' => 1, |
391
|
|
|
'construct' => array( |
392
|
|
|
'queue' => QueuedJob::QUEUED |
393
|
|
|
), |
394
|
|
|
'startDateFormat' => 'Y-m-d 02:00:00', |
395
|
|
|
'startTimeString' => 'tomorrow', |
396
|
|
|
'email' => '[email protected]' |
397
|
|
|
)); |
398
|
|
|
$svc->defaultJobs = $testDefaultJobsArray; |
399
|
|
|
$jobConfig = $testDefaultJobsArray['ArbitraryName']; |
400
|
|
|
|
401
|
|
|
$activeJobs = QueuedJobDescriptor::get()->filter( |
402
|
|
|
'JobStatus', array( |
403
|
|
|
QueuedJob::STATUS_NEW, |
404
|
|
|
QueuedJob::STATUS_INIT, |
405
|
|
|
QueuedJob::STATUS_RUN, |
406
|
|
|
QueuedJob::STATUS_WAIT, |
407
|
|
|
) |
408
|
|
|
); |
409
|
|
|
//assert no jobs currently active |
410
|
|
|
$this->assertEquals(0, $activeJobs->count()); |
|
|
|
|
411
|
|
|
|
412
|
|
|
//add a default job to the queue |
413
|
|
|
$svc->checkdefaultJobs(); |
414
|
|
|
$this->assertEquals(1, $activeJobs->count()); |
|
|
|
|
415
|
|
|
$descriptor = $activeJobs->filter(array_merge( |
416
|
|
|
array('Implementation' => $jobConfig['type']), $jobConfig['filter'] |
417
|
|
|
))->first(); |
418
|
|
|
// Verify initial state is new |
419
|
|
|
$this->assertEquals(QueuedJob::STATUS_NEW, $descriptor->JobStatus); |
|
|
|
|
420
|
|
|
|
421
|
|
|
//update Job to broken |
422
|
|
|
$descriptor->JobStatus = QueuedJob::STATUS_BROKEN; |
423
|
|
|
$descriptor->write(); |
424
|
|
|
//check and add job for broken job |
425
|
|
|
$svc->checkdefaultJobs(); |
426
|
|
|
$this->assertEquals(1, $activeJobs->count()); |
|
|
|
|
427
|
|
|
//assert we now have 2 of our job (one good one broken) |
428
|
|
|
$this->assertEquals(2, QueuedJobDescriptor::get()->count()); |
|
|
|
|
429
|
|
|
|
430
|
|
|
//test not adding a job when job is there already |
431
|
|
|
$svc->checkdefaultJobs(); |
432
|
|
|
$this->assertEquals(1, $activeJobs->count()); |
|
|
|
|
433
|
|
|
//assert we now have 2 of our job (one good one broken) |
434
|
|
|
$this->assertEquals(2, QueuedJobDescriptor::get()->count()); |
|
|
|
|
435
|
|
|
|
436
|
|
|
//test add jobs with various start dates |
437
|
|
|
$job = $activeJobs->first(); |
438
|
|
|
date('Y-m-d 02:00:00', strtotime('+1 day')); |
439
|
|
|
$this->assertEquals(date('Y-m-d 02:00:00', strtotime('+1 day')), $job->StartAfter); |
|
|
|
|
440
|
|
|
//swap start time to midday |
441
|
|
|
$testDefaultJobsArray['ArbitraryName']['startDateFormat'] = 'Y-m-d 12:00:00'; |
442
|
|
|
//clean up then add new jobs |
443
|
|
|
$svc->defaultJobs = $testDefaultJobsArray; |
444
|
|
|
$activeJobs->removeAll(); |
445
|
|
|
$svc->checkdefaultJobs(); |
446
|
|
|
//assert one jobs currently active |
447
|
|
|
$this->assertEquals(1, $activeJobs->count()); |
|
|
|
|
448
|
|
|
$job = $activeJobs->first(); |
449
|
|
|
$this->assertEquals(date('Y-m-d 12:00:00', strtotime('+1 day')), $job->StartAfter); |
|
|
|
|
450
|
|
|
//test alert email |
451
|
|
|
$email = $this->findEmail('[email protected]'); |
452
|
|
|
$this->assertNotNull($email); |
|
|
|
|
453
|
|
|
|
454
|
|
|
//test broken job config |
455
|
|
|
unset($testDefaultJobsArray['ArbitraryName']['startDateFormat']); |
456
|
|
|
//clean up then add new jobs |
457
|
|
|
$svc->defaultJobs = $testDefaultJobsArray; |
458
|
|
|
$activeJobs->removeAll(); |
459
|
|
|
$svc->checkdefaultJobs(); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
// stub class to be able to call init from an external context |
465
|
|
|
class TestQJService extends QueuedJobService { |
|
|
|
|
466
|
|
|
public function testInit($descriptor) { |
467
|
|
|
return $this->initialiseJob($descriptor); |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
class TestExceptingJob extends AbstractQueuedJob implements QueuedJob { |
|
|
|
|
472
|
|
|
private $type = QueuedJob::QUEUED; |
473
|
|
|
|
474
|
|
|
public function __construct($type = null) { |
475
|
|
|
$this->type = QueuedJob::IMMEDIATE; |
476
|
|
|
$this->times = array(); |
|
|
|
|
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
public function getJobType() { |
480
|
|
|
return $this->type; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
public function getTitle() { |
484
|
|
|
return "A Test job throwing exceptions"; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
public function setup() { |
488
|
|
|
$this->totalSteps = 1; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
public function process() { |
492
|
|
|
throw new Exception("just excepted"); |
493
|
|
|
} |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
class TestQueuedJob extends AbstractQueuedJob implements QueuedJob { |
|
|
|
|
497
|
|
|
private $type = QueuedJob::QUEUED; |
498
|
|
|
|
499
|
|
|
public function __construct($type = null) { |
500
|
|
|
if ($type) { |
501
|
|
|
$this->type = $type; |
502
|
|
|
} |
503
|
|
|
$this->times = array(); |
|
|
|
|
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
public function getJobType() { |
507
|
|
|
return $this->type; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
public function getTitle() { |
511
|
|
|
return "A Test job"; |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
public function setup() { |
515
|
|
|
$this->totalSteps = 5; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
View Code Duplication |
public function process() { |
|
|
|
|
519
|
|
|
$times = $this->times; |
|
|
|
|
520
|
|
|
// needed due to quirks with __set |
521
|
|
|
$times[] = date('Y-m-d H:i:s'); |
522
|
|
|
$this->times = $times; |
|
|
|
|
523
|
|
|
|
524
|
|
|
$this->addMessage("Updated time to " . date('Y-m-d H:i:s')); |
525
|
|
|
sleep(1); |
526
|
|
|
|
527
|
|
|
// make sure we're incrementing |
528
|
|
|
$this->currentStep++; |
529
|
|
|
|
530
|
|
|
// and checking whether we're complete |
531
|
|
|
if ($this->currentStep == 5) { |
532
|
|
|
$this->isComplete = true; |
533
|
|
|
} |
534
|
|
|
} |
535
|
|
|
} |
536
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.