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
02:07
created

TestQueuedJob   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 40
loc 40
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 2
A getJobType() 3 3 1
A getTitle() 3 3 1
A setup() 3 3 1
A process() 17 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Symbiote\QueuedJobs\Tests\TestQueuedJob.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use Symbiote\QueuedJobs\Tests\QueuedJobsTest\TestQJService;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Symbiote\QueuedJobs\Tests\TestQJService.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
        $svc = $this->getService();
387
        $job = new TestExceptingJob();
388
		$job->firstJob = true;
389
		$id = $svc->queueJob($job);
390
		$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...
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);
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...
395
396
        $svc->runJob($id);
397
398
        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...
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
500
	public function testInit($descriptor) {
501
		return $this->initialiseJob($descriptor);
502
	}
503
}
504
505 View Code Duplication
class TestQueuedJob extends AbstractQueuedJob implements QueuedJob {
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: addMessage, getJobData, getSignature, jobFinished, prepareForRestart, setJobData
Loading history...
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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