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.

CleanupJob   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 8
dl 0
loc 159
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 7 1
A getJobType() 0 5 1
B process() 0 70 6
A reenqueue() 0 11 2
1
<?php
2
3
namespace Symbiote\QueuedJobs\Jobs;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\ORM\DB;
8
use SilverStripe\ORM\FieldType\DBDatetime;
9
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
10
use Symbiote\QueuedJobs\Services\QueuedJob;
11
use Symbiote\QueuedJobs\Services\QueuedJobService;
12
13
/**
14
 * An queued job to clean out the QueuedJobDescriptor Table
15
 * which often gets too full
16
 *
17
 * @author Andrew Aitken-Fincham <[email protected]>
18
 */
19
class CleanupJob extends AbstractQueuedJob
20
{
21
    use Configurable;
22
23
    /**
24
     * How we will determine "stale"
25
     * Possible values: age, number
26
     * @config
27
     * @var string
28
     */
29
    private static $cleanup_method = "age";
30
31
    /**
32
     * Value associated with cleanupMethod
33
     * age => days, number => integer
34
     * @config
35
     * @var integer
36
     */
37
    private static $cleanup_value = 30;
38
39
    /**
40
     * Which JobStatus values are OK to be deleted
41
     * @config
42
     * @var array
43
     */
44
    private static $cleanup_statuses = array(
45
        "Complete",
46
        "Broken",
47
        // "Initialising",
48
        // "Running",
49
        // "New",
50
        // "Paused",
51
        // "Cancelled",
52
        // "Waiting",
53
    );
54
55
    /**
56
     * Database query limit
57
     *
58
     * @config
59
     * @var integer
60
     */
61
    private static $query_limit = 100000;
62
63
    /**
64
     * Check whether is enabled or not for BC
65
     * @config
66
     * @var boolean
67
     */
68
    private static $is_enabled = false;
69
70
    /**
71
     * Defines the title of the job
72
     * @return string
73
     */
74
    public function getTitle()
75
    {
76
        return _t(
77
            __CLASS__ . '.Title',
78
            "Clean up old jobs from the database"
79
        );
80
    }
81
82
    /**
83
     * Set immediacy of job
84
     * @return int
85
     */
86
    public function getJobType()
87
    {
88
        $this->totalSteps = '1';
0 ignored issues
show
Documentation Bug introduced by
The property $totalSteps was declared of type integer, but '1' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
89
        return QueuedJob::IMMEDIATE;
90
    }
91
92
    /**
93
     * Clear out stale jobs based on the cleanup values
94
     */
95
    public function process()
96
    {
97
        // construct limit statement if query_limit is valid int value
98
        $limit = '';
99
        $query_limit = $this->config()->get('query_limit');
100
        if (is_numeric($query_limit) && $query_limit >= 0) {
101
            $limit = ' LIMIT ' . ((int)$query_limit);
102
        }
103
104
        $statusList = implode('\', \'', $this->config()->get('cleanup_statuses'));
105
        switch ($this->config()->get('cleanup_method')) {
106
            // If Age, we need to get jobs that are at least n days old
107
            case "age":
108
                $cutOff = date(
109
                    "Y-m-d H:i:s",
110
                    strtotime(DBDatetime::now() .
111
                        " - " .
112
                        $this->config()->cleanup_value .
113
                        " days")
114
                );
115
                $stale = DB::query(
116
                    'SELECT "ID"
117
					FROM "QueuedJobDescriptor"
118
					WHERE "JobStatus"
119
					IN (\'' . $statusList . '\')
120
					AND "LastEdited" < \'' . $cutOff . '\'' . $limit
121
                );
122
                $staleJobs = $stale->column("ID");
123
                break;
124
            // If Number, we need to save n records, then delete from the rest
125
            case "number":
126
                $fresh = DB::query(
127
                    'SELECT "ID"
128
					FROM "QueuedJobDescriptor"
129
					ORDER BY "LastEdited"
130
					ASC LIMIT ' . $this->config()->cleanup_value
131
                );
132
                $freshJobIDs = implode('\', \'', $fresh->column("ID"));
133
134
                $stale = DB::query(
135
                    'SELECT "ID"
136
					FROM "QueuedJobDescriptor"
137
					WHERE "ID"
138
					NOT IN (\'' . $freshJobIDs . '\')
139
					AND "JobStatus"
140
					IN (\'' . $statusList . '\')' . $limit
141
                );
142
                $staleJobs = $stale->column("ID");
143
                break;
144
            default:
145
                $this->addMessage("Incorrect configuration values set. Cleanup ignored");
146
                $this->isComplete = true;
147
                return;
148
        }
149
        if (empty($staleJobs)) {
150
            $this->addMessage("No jobs to clean up.");
151
            $this->isComplete = true;
152
            $this->reenqueue();
153
            return;
154
        }
155
        $numJobs = count($staleJobs);
156
        $staleJobs = implode('\', \'', $staleJobs);
157
        DB::query('DELETE FROM "QueuedJobDescriptor"
158
			WHERE "ID"
159
			IN (\'' . $staleJobs . '\')');
160
        $this->addMessage($numJobs . " jobs cleaned up.");
161
        // let's make sure there is a cleanupJob in the queue
162
        $this->reenqueue();
163
        $this->isComplete = true;
164
    }
165
166
    private function reenqueue()
167
    {
168
        if ($this->config()->get('is_enabled')) {
169
            $this->addMessage("Queueing the next Cleanup Job.");
170
            $cleanup = Injector::inst()->create(CleanupJob::class);
171
            QueuedJobService::singleton()->queueJob(
172
                $cleanup,
173
                DBDatetime::create()->setValue(DBDatetime::now()->getTimestamp() + 86400)->Rfc2822()
174
            );
175
        }
176
    }
177
}
178