diff --git a/README.md b/README.md index b87c73e6..8827f9e1 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/code/jobs/CleanupJob.php b/code/jobs/CleanupJob.php index 67f6fa2f..60c21b7d 100644 --- a/code/jobs/CleanupJob.php +++ b/code/jobs/CleanupJob.php @@ -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 @@ -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 @@ -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; @@ -115,7 +129,7 @@ public function process() { WHERE "ID" NOT IN (\'' . $freshJobIDs . '\') AND "JobStatus" - IN (\'' . $statusList . '\')' + IN (\'' . $statusList . '\')' . $limit ); $staleJobs = $stale->column("ID"); break; @@ -151,4 +165,4 @@ private function reenqueue() } } -} +} \ No newline at end of file