Completed
Push — master ( 381a7e...8ac8f4 )
by Marcus
9s
created

CleanupJob   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A config() 0 3 1
A getTitle() 0 6 1
A getJobType() 0 4 1
B process() 0 67 5
1
<?php
2
3
/**
4
 * An queued job to clean out the QueuedJobDescriptor Table
5
 * which often gets too full
6
 *
7
 * @author Andrew Aitken-Fincham <[email protected]>
8
 */
9
class CleanupJob extends AbstractQueuedJob implements QueuedJob {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
11
	/**
12
	 * How we will determine "stale"
13
	 * Possible values: age, number
14
	 * @config
15
	 * @var string
16
	 */
17
	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...
18
19
	/**
20
	 * Value associated with cleanupMethod
21
	 * age => days, number => integer
22
	 * @config
23
	 * @var integer
24
	 */
25
	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...
26
27
	/**
28
	 * Which JobStatus values are OK to be deleted
29
	 * @config
30
	 * @var array
31
	 */
32
	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...
33
		"Complete",
34
		"Broken",
35
		// "Initialising",
36
		// "Running",
37
		// "New",
38
		// "Paused",
39
		// "Cancelled",
40
		// "Waiting",
41
	);
42
43
	/**
44
	 * Check whether is enabled or not for BC
45
	 * @config
46
	 * @var boolean
47
	 */
48
	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...
49
50
	/**
51
	 * Required because we aren't extending object
52
	 * @return Config_ForClass
53
	 */
54
	public function config() {
55
		return Config::inst()->forClass(get_called_class());
56
	}
57
58
	/**
59
	 * Defines the title of the job
60
	 * @return string
61
	 */
62
	public function getTitle() {
63
		return _t(
64
			'CleanupJob.Title',
65
			"Clean up old jobs from the database"
66
		);
67
	}
68
69
	/**
70
	 * Set immediacy of job
71
	 * @return int
72
	 */
73
	public function getJobType() {
74
		$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...
75
		return QueuedJob::IMMEDIATE;
76
	}
77
78
	/**
79
	 * Clear out stale jobs based on the cleanup values
80
	 */
81
	public function process() {
82
		$statusList = implode('\', \'', $this->config()->cleanup_statuses);
83
		switch($this->config()->cleanup_method) {
84
			// If Age, we need to get jobs that are at least n days old
85
			case "age":
86
				$cutOff = date("Y-m-d H:i:s",
87
					strtotime(SS_Datetime::now() .
88
						" - " .
89
						$this->config()->cleanup_value .
90
						" days"
91
					)
92
				);
93
				$stale = DB::query(
94
					'SELECT "ID" 
95
					FROM "QueuedJobDescriptor" 
96
					WHERE "JobStatus" 
97
					IN (\'' . $statusList . '\')
98
					AND "LastEdited" < \'' . $cutOff .'\''
99
				);
100
				$staleJobs = $stale->column("ID");
101
				break;
102
			// If Number, we need to save n records, then delete from the rest
103
			case "number":
104
				$fresh = DB::query(
105
					'SELECT "ID" 
106
					FROM "QueuedJobDescriptor" 
107
					ORDER BY "LastEdited" 
108
					ASC LIMIT ' . $this->config()->cleanup_value
109
				);
110
				$freshJobIDs = implode('\', \'', $fresh->column("ID"));
111
112
				$stale = DB::query(
113
					'SELECT "ID" 
114
					FROM "QueuedJobDescriptor" 
115
					WHERE "ID" 
116
					NOT IN (\'' . $freshJobIDs . '\') 
117
					AND "JobStatus" 
118
					IN (\'' . $statusList . '\')'
119
				);
120
				$staleJobs = $stale->column("ID");
121
				break;
122
			default:
123
				$this->addMessage("Incorrect configuration values set. Cleanup ignored");
124
				$this->isComplete = true;
125
				return;
126
		}
127
		if (empty($staleJobs)) {
128
			$this->addMessage("No jobs to clean up.");
129
			$this->isComplete = true;
130
			return;
131
		}
132
		$numJobs = count($staleJobs);
133
		$staleJobs = implode('\', \'', $staleJobs);
134
		DB::query('DELETE FROM "QueuedJobDescriptor"
135
			WHERE "ID"
136
			IN (\'' . $staleJobs . '\')'
137
		);
138
		$this->addMessage($numJobs . " jobs cleaned up.");
139
		// let's make sure there is a cleanupJob in the queue
140
		if (Config::inst()->get('CleanupJob', 'is_enabled')) {
141
		    $this->addMessage("Queueing the next Cleanup Job.");
142
			$cleanup = new CleanupJob();
143
			singleton('QueuedJobService')->queueJob($cleanup, date('Y-m-d H:i:s', time() + 86400));
144
		}
145
		$this->isComplete = true;
146
		return;
147
	}
148
}
149