Skip to content

Commit

Permalink
fix(PruneChangesBeforeJob): Various fixes (brought from 4.0 branch) (#23
Browse files Browse the repository at this point in the history
)

- fix(PruneChangesBeforeJob): Remove unused / old code that retrieved a flat list of IDs as this can cause the job to not work if there are a lot of DataChangeRecord items.
- fix(PruneChangesBeforeJob): Fix logic of priorTo so that it defaults to the last 3 months properly
- fix(PruneChangesBeforeJob): Change steps to 1 as it's technically doing this in 1 step now, this is to avoid an issue where totalSteps=0 can occur and the job won't requeue itself.
  • Loading branch information
silbinarywolf committed May 10, 2018
1 parent 3c7fdd6 commit 98b6a8f
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions src/Job/PruneChangesBeforeJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Symbiote\DataChange\Job;

use Symbiote\DataChange\DataChangeRecord;

use SilverStripe\ORM\Queries\SQLDelete;
use SilverStripe\Core\Injector\Injector;
use Symbiote\QueuedJobs\Services\QueuedJobService;
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
use Symbiote\DataChange\Model\DataChangeRecord;

if (class_exists('AbstractQueuedJob')) {
if (class_exists(AbstractQueuedJob::class)) {

/**
* A scheduled regular prune of _old_ data change records
Expand All @@ -18,49 +20,50 @@ class PruneChangesBeforeJob extends AbstractQueuedJob

public function __construct($priorTo = null)
{
$ts = 0;
if ($priorTo) {
$ts = strtotime($priorTo);
if ($ts <= 0) {
$ts = time() - 90*86400;
}
$this->priorTo = $priorTo;
$this->pruneBefore = date('Y-m-d 00:00:00', $ts);
$this->totalSteps = DataChangeRecord::get()->filter('Created:LessThan', $this->pruneBefore)->count();
}
if ($ts <= 0) {
$ts = time() - 90*86400;
}
$this->priorTo = $priorTo;
$this->pruneBefore = date('Y-m-d 00:00:00', $ts);
// NOTE(Jake): 2018-05-08
//
// Change steps to 1 as it's technically doing
// this in 1 step now, this is to avoid an issue where
// totalSteps=0 can occur and the job won't requeue itself.
// (When using ->count() off the DataList)
//
$this->totalSteps = 1;
}

public function getSignature()
{
return md5($this->pruneBefore);
}

public function getTitle()
{
return "Prune data change track entries before " . $this->pruneBefore;
}

public function setup()
{
$this->pruneIds = DataChangeRecord::get()->filter('Created:LessThan', $this->pruneBefore)->column();
$this->totalSteps = count($this->pruneIds);
}

public function process()
{

public function process() {
$items = DataChangeRecord::get()->filter('Created:LessThan', $this->pruneBefore);
$max = $items->max('ID');

$query = new SQLDelete('DataChangeRecord', '"ID" < \'' . $max . '\'');
$query->execute();

$job = new PruneChangesBeforeJob($this->priorTo);

$next = date('Y-m-d 03:00:00', strtotime('tomorrow'));

$this->currentStep = $this->totalSteps;
$this->currentStep = 1;
$this->isComplete = true;

singleton('QueuedJobService')->queueJob($job, $next);
Injector::inst()->get(QueuedJobService::class)->queueJob($job, $next);
}
}

Expand Down

0 comments on commit 98b6a8f

Please sign in to comment.