src/Jobs/CleanupJob.php
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()->get('cleanup_statuses'));
        switch ($this->config()->get('cleanup_method')) {
            // If Age, we need to get jobs that are at least n days old
            case "age":
                $cutOff = date(
                    "Y-m-d H:i:s",
                    strtotime(DBDatetime::now() .
                        " - " .
                        $this->config()->cleanup_value .
                        " days")
                );
                $stale = DB::query(
                    'SELECT "ID"
					FROM "QueuedJobDescriptor"
					WHERE "JobStatus"
					IN (\'' . $statusList . '\')
					AND "LastEdited" < \'' . $cutOff . '\'' . $limit
                );
                $staleJobs = $stale->column("ID");
                break;
            // If Number, we need to save n records, then delete from the rest
            case "number":
                $fresh = DB::query(
                    'SELECT "ID"
					FROM "QueuedJobDescriptor"
					ORDER BY "LastEdited"
					ASC LIMIT ' . $this->config()->cleanup_value
                );
                $freshJobIDs = implode('\', \'', $fresh->column("ID"));

                $stale = DB::query(
                    'SELECT "ID"
					FROM "QueuedJobDescriptor"
					WHERE "ID"
					NOT IN (\'' . $freshJobIDs . '\')
					AND "JobStatus"
					IN (\'' . $statusList . '\')' . $limit
                );
                $staleJobs = $stale->column("ID");
                break;
            default:
                $this->addMessage("Incorrect configuration values set. Cleanup ignored");
                $this->isComplete = true;
                return;
        }
        if (empty($staleJobs)) {
            $this->addMessage("No jobs to clean up.");
            $this->isComplete = true;
            $this->reenqueue();
            return;
        }
        $numJobs = count($staleJobs);
        $staleJobs = implode('\', \'', $staleJobs);
        DB::query('DELETE FROM "QueuedJobDescriptor"
			WHERE "ID"
			IN (\'' . $staleJobs . '\')');
        $this->addMessage($numJobs . " jobs cleaned up.");
        // let's make sure there is a cleanupJob in the queue
        $this->reenqueue();
        $this->isComplete = true;
    }
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()->get('cleanup_statuses'));
        switch ($this->config()->get('cleanup_method')) {
            // If Age, we need to get jobs that are at least n days old
            case "age":
                $cutOff = date(
                    "Y-m-d H:i:s",
                    strtotime(DBDatetime::now() .
                        " - " .
                        $this->config()->cleanup_value .
                        " days")
                );
                $stale = DB::query(
                    'SELECT "ID"
					FROM "QueuedJobDescriptor"
					WHERE "JobStatus"
					IN (\'' . $statusList . '\')
					AND "LastEdited" < \'' . $cutOff . '\'' . $limit
                );
                $staleJobs = $stale->column("ID");
                break;
            // If Number, we need to save n records, then delete from the rest
            case "number":
                $fresh = DB::query(
                    'SELECT "ID"
					FROM "QueuedJobDescriptor"
					ORDER BY "LastEdited"
					ASC LIMIT ' . $this->config()->cleanup_value
                );
                $freshJobIDs = implode('\', \'', $fresh->column("ID"));

                $stale = DB::query(
                    'SELECT "ID"
					FROM "QueuedJobDescriptor"
					WHERE "ID"
					NOT IN (\'' . $freshJobIDs . '\')
					AND "JobStatus"
					IN (\'' . $statusList . '\')' . $limit
                );
                $staleJobs = $stale->column("ID");
                break;
            default:
                $this->addMessage("Incorrect configuration values set. Cleanup ignored");
                $this->isComplete = true;
                return;
        }
        if (empty($staleJobs)) {
            $this->addMessage("No jobs to clean up.");
            $this->isComplete = true;
            $this->reenqueue();
            return;
        }
        $numJobs = count($staleJobs);
        $staleJobs = implode('\', \'', $staleJobs);
        DB::query('DELETE FROM "QueuedJobDescriptor"
			WHERE "ID"
			IN (\'' . $staleJobs . '\')');
        $this->addMessage($numJobs . " jobs cleaned up.");
        // let's make sure there is a cleanupJob in the queue
        $this->reenqueue();
        $this->isComplete = true;
    }
Clear out stale jobs based on the cleanup values