Skip to content

Commit

Permalink
fix(PruneChangesBeforeJob): Remove unused / old code that retrieved a…
Browse files Browse the repository at this point in the history
… flat list of IDs as this can cause the job to not work if there are a lot of DataChangeRecord items. (#18) (#19)

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.
(cherry picked from commit b0a0c97)
  • Loading branch information
silbinarywolf authored May 9, 2018
1 parent ef5881c commit df9d16e
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions code/jobs/PruneChangesBeforeJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ 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()
Expand All @@ -32,24 +40,20 @@ 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() {
$items = DataChangeRecord::get()->filter('Created:LessThan', $this->pruneBefore);
$max = $items->max('ID');

$query = new SQLDelete('DataChangeRecord', '"ID" < \'' . $max . '\'');
$query->execute();
if ($this->totalSteps <= 0) {
$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);
Expand Down

0 comments on commit df9d16e

Please sign in to comment.