GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#148)
by Daniel
01:59
created

QueuedJobsTest::testCheckdefaultJobs()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 94
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 94
rs 8.4378
c 0
b 0
f 0
cc 1
eloc 60
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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";
0 ignored issues
show
Documentation introduced by
The property runningAs does not exist on object<Symbiote\QueuedJo...JobsTest\TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
84
        $jobId = $svc->queueJob($job);
0 ignored issues
show
Unused Code introduced by
$jobId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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';
0 ignored issues
show
Documentation introduced by
The property randomParam does not exist on object<Symbiote\QueuedJo...JobsTest\TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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;
0 ignored issues
show
Documentation introduced by
The property testingStartJob does not exist on object<Symbiote\QueuedJo...JobsTest\TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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;
0 ignored issues
show
Documentation introduced by
The property firstJob does not exist on object<Symbiote\QueuedJo...JobsTest\TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
201
        $id = $svc->queueJob($job);
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
202
203
        $job = new TestQueuedJob(QueuedJob::IMMEDIATE);
204
        $job->secondJob = true;
0 ignored issues
show
Documentation introduced by
The property secondJob does not exist on object<Symbiote\QueuedJo...JobsTest\TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
205
        $id = $svc->queueJob($job);
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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';
0 ignored issues
show
Documentation introduced by
The property randomParam does not exist on object<Symbiote\QueuedJo...JobsTest\TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
236
        $id2 = $svc->queueJob($job);
0 ignored issues
show
Unused Code introduced by
$id2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
237
238
        $job = new TestQueuedJob();
239
        // to get around the signature checks
240
        $job->randomParam = 'mo';
0 ignored issues
show
Documentation introduced by
The property randomParam does not exist on object<Symbiote\QueuedJo...JobsTest\TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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;
0 ignored issues
show
Documentation introduced by
The property firstJob does not exist on object<Symbiote\QueuedJo...JobsTest\TestQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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
    {
387
        $svc = $this->getService();
388
        $job = new TestExceptingJob();
389
        $job->firstJob = true;
390
        $id = $svc->queueJob($job);
391
        $descriptor = QueuedJobDescriptor::get()->byID($id);
0 ignored issues
show
Unused Code introduced by
$descriptor is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
392
393
        // we want to set the memory limit _really_ low so that our first run triggers
394
        $mem = Config::inst()->get('QueuedJobService', 'memory_limit');
395
        Config::inst()->update('QueuedJobService', 'memory_limit', 1);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SilverStripe\Config\Coll...nfigCollectionInterface as the method update() does only exist in the following implementations of said interface: SilverStripe\Config\Coll...s\DeltaConfigCollection, SilverStripe\Config\Coll...\MemoryConfigCollection.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
396
397
        $svc->runJob($id);
398
399
        Config::inst()->update('QueuedJobService', 'memory_limit', $mem);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SilverStripe\Config\Coll...nfigCollectionInterface as the method update() does only exist in the following implementations of said interface: SilverStripe\Config\Coll...s\DeltaConfigCollection, SilverStripe\Config\Coll...\MemoryConfigCollection.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
400
401
        $descriptor = QueuedJobDescriptor::get()->byID($id);
402
403
        $this->assertEquals(QueuedJob::STATUS_BROKEN, $descriptor->JobStatus);
404
    }
405
406
    public function testCheckdefaultJobs()
407
    {
408
        // Create a job and add it to the queue
409
        $svc = $this->getService();
410
        $testDefaultJobsArray = array(
411
            'ArbitraryName' => array(
412
                # I'll get restarted and create an alert email
413
                'type' => 'TestQueuedJob',
414
                'filter' => array(
415
                    'JobTitle' => "A Test job"
416
                ),
417
                'recreate' => 1,
418
                'construct' => array(
419
                    'queue' => QueuedJob::QUEUED
420
                ),
421
                'startDateFormat' => 'Y-m-d 02:00:00',
422
                'startTimeString' => 'tomorrow',
423
                'email' => '[email protected]'
424
            ));
425
        $svc->defaultJobs = $testDefaultJobsArray;
426
        $jobConfig = $testDefaultJobsArray['ArbitraryName'];
427
428
        $activeJobs = QueuedJobDescriptor::get()->filter(
429
            'JobStatus',
430
            array(
431
                QueuedJob::STATUS_NEW,
432
                QueuedJob::STATUS_INIT,
433
                QueuedJob::STATUS_RUN,
434
                QueuedJob::STATUS_WAIT,
435
                QueuedJob::STATUS_PAUSED
436
            )
437
        );
438
        //assert no jobs currently active
439
        $this->assertEquals(0, $activeJobs->count());
440
441
        //add a default job to the queue
442
        $svc->checkdefaultJobs();
443
        $this->assertEquals(1, $activeJobs->count());
444
        $descriptor = $activeJobs->filter(array_merge(
445
            array('Implementation' => $jobConfig['type']),
446
            $jobConfig['filter']
447
        ))->first();
448
        // Verify initial state is new
449
        $this->assertEquals(QueuedJob::STATUS_NEW, $descriptor->JobStatus);
450
451
        //update Job to paused
452
        $descriptor->JobStatus = QueuedJob::STATUS_PAUSED;
453
        $descriptor->write();
454
        //check defaults the paused job shoudl be ignored
455
        $svc->checkdefaultJobs();
456
        $this->assertEquals(1, $activeJobs->count());
457
        //assert we now still have 1 of our job (paused)
458
        $this->assertEquals(1, QueuedJobDescriptor::get()->count());
459
460
        //update Job to broken
461
        $descriptor->JobStatus = QueuedJob::STATUS_BROKEN;
462
        $descriptor->write();
463
        //check and add job for broken job
464
        $svc->checkdefaultJobs();
465
        $this->assertEquals(1, $activeJobs->count());
466
        //assert we now have 2 of our job (one good one broken)
467
        $this->assertEquals(2, QueuedJobDescriptor::get()->count());
468
469
        //test not adding a job when job is there already
470
        $svc->checkdefaultJobs();
471
        $this->assertEquals(1, $activeJobs->count());
472
        //assert we now have 2 of our job (one good one broken)
473
        $this->assertEquals(2, QueuedJobDescriptor::get()->count());
474
475
        //test add jobs with various start dates
476
        $job = $activeJobs->first();
477
        date('Y-m-d 02:00:00', strtotime('+1 day'));
478
        $this->assertEquals(date('Y-m-d 02:00:00', strtotime('+1 day')), $job->StartAfter);
479
        //swap start time to midday
480
        $testDefaultJobsArray['ArbitraryName']['startDateFormat'] = 'Y-m-d 12:00:00';
481
        //clean up then add new jobs
482
        $svc->defaultJobs = $testDefaultJobsArray;
483
        $activeJobs->removeAll();
484
        $svc->checkdefaultJobs();
485
        //assert one jobs currently active
486
        $this->assertEquals(1, $activeJobs->count());
487
        $job = $activeJobs->first();
488
        $this->assertEquals(date('Y-m-d 12:00:00', strtotime('+1 day')), $job->StartAfter);
489
        //test alert email
490
        $email = $this->findEmail('[email protected]');
491
        $this->assertNotNull($email);
492
493
        //test broken job config
494
        unset($testDefaultJobsArray['ArbitraryName']['startDateFormat']);
495
        //clean up then add new jobs
496
        $svc->defaultJobs = $testDefaultJobsArray;
497
        $activeJobs->removeAll();
498
        $svc->checkdefaultJobs();
499
    }
500
}   
501