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
Push — master ( 646ea8...4450cc )
by Robbie
13s
created

CleanupJob::reenqueue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Symbiote\QueuedJobs\Jobs;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\ORM\DB;
7
use SilverStripe\ORM\FieldType\DBDatetime;
8
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
9
use Symbiote\QueuedJobs\Services\QueuedJob;
10
11
/**
12
 * An queued job to clean out the QueuedJobDescriptor Table
13
 * which often gets too full
14
 *
15
 * @author Andrew Aitken-Fincham <[email protected]>
16
 */
17
class CleanupJob extends AbstractQueuedJob implements QueuedJob
18
{
19
20
    /**
21
     * How we will determine "stale"
22
     * Possible values: age, number
23
     * @config
24
     * @var string
25
     */
26
    private static $cleanup_method = "age";
0 ignored issues
show
Unused Code introduced by
The property $cleanup_method is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
27
28
    /**
29
     * Value associated with cleanupMethod
30
     * age => days, number => integer
31
     * @config
32
     * @var integer
33
     */
34
    private static $cleanup_value = 30;
0 ignored issues
show
Unused Code introduced by
The property $cleanup_value is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
36
    /**
37
     * Which JobStatus values are OK to be deleted
38
     * @config
39
     * @var array
40
     */
41
    private static $cleanup_statuses = array(
0 ignored issues
show
Unused Code introduced by
The property $cleanup_statuses is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
42
        "Complete",
43
        "Broken",
44
        // "Initialising",
45
        // "Running",
46
        // "New",
47
        // "Paused",
48
        // "Cancelled",
49
        // "Waiting",
50
    );
51
52
    /**
53
     * Check whether is enabled or not for BC
54
     * @config
55
     * @var boolean
56
     */
57
    private static $is_enabled = false;
0 ignored issues
show
Unused Code introduced by
The property $is_enabled is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
58
59
    /**
60
     * Required because we aren't extending object
61
     * @return Config_ForClass
62
     */
63
    public function config()
64
    {
65
        return Config::forClass(get_called_class());
66
    }
67
68
    /**
69
     * Defines the title of the job
70
     * @return string
71
     */
72
    public function getTitle()
73
    {
74
        return _t(
75
            __CLASS__ . '.Title',
76
            "Clean up old jobs from the database"
77
        );
78
    }
79
80
    /**
81
     * Set immediacy of job
82
     * @return int
83
     */
84
    public function getJobType()
85
    {
86
        $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...
87
        return QueuedJob::IMMEDIATE;
88
    }
89
90
    /**
91
     * Clear out stale jobs based on the cleanup values
92
     */
93
    public function process()
94
    {
95
        $statusList = implode('\', \'', $this->config()->cleanup_statuses);
96
        switch ($this->config()->cleanup_method) {
97
            // If Age, we need to get jobs that are at least n days old
98
            case "age":
99
                $cutOff = date(
100
                    "Y-m-d H:i:s",
101
                    strtotime(DBDatetime::now() .
102
                        " - " .
103
                        $this->config()->cleanup_value .
104
                        " days")
105
                );
106
                $stale = DB::query(
107
                    'SELECT "ID"
108
					FROM "QueuedJobDescriptor"
109
					WHERE "JobStatus"
110
					IN (\'' . $statusList . '\')
111
					AND "LastEdited" < \'' . $cutOff .'\''
112
                );
113
                $staleJobs = $stale->column("ID");
114
                break;
115
            // If Number, we need to save n records, then delete from the rest
116
            case "number":
117
                $fresh = DB::query(
118
                    'SELECT "ID"
119
					FROM "QueuedJobDescriptor"
120
					ORDER BY "LastEdited"
121
					ASC LIMIT ' . $this->config()->cleanup_value
122
                );
123
                $freshJobIDs = implode('\', \'', $fresh->column("ID"));
124
125
                $stale = DB::query(
126
                    'SELECT "ID"
127
					FROM "QueuedJobDescriptor"
128
					WHERE "ID"
129
					NOT IN (\'' . $freshJobIDs . '\')
130
					AND "JobStatus"
131
					IN (\'' . $statusList . '\')'
132
                );
133
                $staleJobs = $stale->column("ID");
134
                break;
135
            default:
136
                $this->addMessage("Incorrect configuration values set. Cleanup ignored");
137
                $this->isComplete = true;
138
                return;
139
        }
140
        if (empty($staleJobs)) {
141
            $this->addMessage("No jobs to clean up.");
142
            $this->isComplete = true;
143
            $this->reenqueue();
144
            return;
145
        }
146
        $numJobs = count($staleJobs);
147
        $staleJobs = implode('\', \'', $staleJobs);
148
        DB::query('DELETE FROM "QueuedJobDescriptor"
149
			WHERE "ID"
150
			IN (\'' . $staleJobs . '\')');
151
        $this->addMessage($numJobs . " jobs cleaned up.");
152
        // let's make sure there is a cleanupJob in the queue
153
        $this->reenqueue();
154
        $this->isComplete = true;
155
        return;
156
    }
157
158
159
    private function reenqueue()
160
    {
161
        if (Config::inst()->get('CleanupJob', 'is_enabled')) {
162
            $this->addMessage("Queueing the next Cleanup Job.");
163
            $cleanup = new CleanupJob();
164
            singleton('QueuedJobService')->queueJob($cleanup, date('Y-m-d H:i:s', time() + 86400));
165
        }
166
    }
167
}
168