Skip to content

Commit

Permalink
Merge pull request #224 from JoshuaCarter/3.1
Browse files Browse the repository at this point in the history
Adds CleanupJob query_limit
  • Loading branch information
Joshua Carter authored Nov 21, 2018
2 parents b0258b2 + 437dd2f commit a3b7dae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,14 @@ Each of these methods will have a value associated with it - this is an integer,
For "age", this will be converted into days; for "number", it is the minimum number of records to keep, sorted by LastEdited.
The default value is 30, as we are expecting days.

You can also determine which JobStatuses are allowed to be cleaned up. The default setting is to clean up "Broken" and "Complete" jobs. All other statuses can be configured with `cleanup_statuses`.
You can determine which JobStatuses are allowed to be cleaned up. The default setting is to clean up "Broken" and "Complete" jobs. All other statuses can be configured with `cleanup_statuses`. You can also define `query_limit` to limit the number of rows queried/deleted by the cleanup job (defaults to 100k).

The default configuration looks like this:

```yaml
CleanupJob:
is_enabled: false
query_limit: 100000
cleanup_method: "age"
cleanup_value: 30
cleanup_statuses:
Expand Down
22 changes: 18 additions & 4 deletions code/jobs/CleanupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class CleanupJob extends AbstractQueuedJob implements QueuedJob {
// "Waiting",
);

/**
* Database query limit
* @config
* @var integer
*/
private static $query_limit = 100000;

/**
* Check whether is enabled or not for BC
* @config
Expand Down Expand Up @@ -79,6 +86,13 @@ public function getJobType() {
* Clear out stale jobs based on the cleanup values
*/
public function process() {
// construct limit statement if query_limit is valid int value
$limit = '';
$query_limit = $this->config()->get('query_limit');
if (is_numeric($query_limit) && $query_limit >= 0) {
$limit = ' LIMIT ' . ((int)$query_limit);
}

$statusList = implode('\', \'', $this->config()->cleanup_statuses);
switch($this->config()->cleanup_method) {
// If Age, we need to get jobs that are at least n days old
Expand All @@ -94,8 +108,8 @@ public function process() {
'SELECT "ID"
FROM "QueuedJobDescriptor"
WHERE "JobStatus"
IN (\'' . $statusList . '\')
AND "LastEdited" < \'' . $cutOff .'\''
IN (\'' . $statusList . '\')
AND "LastEdited" < \'' . $cutOff . '\'' . $limit
);
$staleJobs = $stale->column("ID");
break;
Expand All @@ -115,7 +129,7 @@ public function process() {
WHERE "ID"
NOT IN (\'' . $freshJobIDs . '\')
AND "JobStatus"
IN (\'' . $statusList . '\')'
IN (\'' . $statusList . '\')' . $limit
);
$staleJobs = $stale->column("ID");
break;
Expand Down Expand Up @@ -151,4 +165,4 @@ private function reenqueue()
}
}

}
}

0 comments on commit a3b7dae

Please sign in to comment.