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