Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Review: Cleanup service for old version records. #335

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions src/VersionsCleanup/ChangeSetCleanupTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace App\Tasks\Tools;

use App\Helpers\QueueLockHelper;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\Queries\SQLDelete;
use SilverStripe\Versioned\ChangeSet;
use SilverStripe\Versioned\ChangeSetItem;

class ChangeSetCleanupTask extends BuildTask
{
/**
* ChangeSet lifetime in days
* Any records older than this duration are considered obsolete and ready for deletion
*/
private const DELETION_LIFETIME = 100;

/**
* Number of ChangeSet records to be deleted in one go
* Note that this does directly impact number of related records so the actual number of deleted records may vary
*/
private const DELETION_LIMIT = 500;

/**
* @var string
*/
private static $segment = 'change-set-cleanup-task';

/**
* @var string
*/
protected $title = 'Remove old ChangeSet data';

/**
* @var string
*/
protected $description = 'Regular cleanup of ChangeSet related data (Campaign admin module related)';

/**
* @param HTTPRequest $request
*/
public function run($request): void // phpcs:ignore SlevomatCodingStandard.TypeHints
{
if (QueueLockHelper::isMaintenanceLockActive()) {
return;
}

$mainTableRaw = ChangeSet::config()->get('table_name');
$itemTableRaw = ChangeSetItem::config()->get('table_name');
$relationTableRaw = $itemTableRaw . '_ReferencedBy';
$mainTable = sprintf('"%s"', $mainTableRaw);
$itemTable = sprintf('"%s"', $itemTableRaw);
$relationTable = sprintf('"%s"', $relationTableRaw);
$deletionDate = DBDatetime::now()
->modify(sprintf('- %d days', self::DELETION_LIFETIME))
->Rfc2822();

$ids = ChangeSet::get()
->filter(['LastEdited:LessThan' => $deletionDate])
->sort('ID', 'ASC')
->limit(self::DELETION_LIMIT)
->columnUnique('ID');

if (count($ids) === 0) {
echo 'Nothing to delete.' . PHP_EOL;

return;
}

$query = SQLDelete::create(
[
$mainTable,
],
[
sprintf($mainTable . '."ID" IN (%s)', DB::placeholders($ids)) => $ids,
],
[
$mainTable,
$itemTable,
$relationTable,
],
);

$query
->addLeftJoin($itemTableRaw, sprintf('%s."ID" = %s."ChangeSetID"', $mainTable, $itemTable))
->addLeftJoin($relationTableRaw, sprintf('%s."ID" = %s."ChangeSetItemID"', $itemTable, $relationTable));

$result = $query->execute();

if ($result === null) {
echo 'Failed to execute deletion.' . PHP_EOL;

return;
}

echo sprintf('Deleted %d records.', DB::affected_rows()) . PHP_EOL;
}
}
66 changes: 66 additions & 0 deletions src/VersionsCleanup/CleanupJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\VersionsCleanup;

use Exception;
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJob;

/**
* @property array $versions
* @property array $remainingVersions
*/
class CleanupJob extends AbstractQueuedJob
{
/**
* @param array $versions
*/
public function hydrate(array $versions): void
{
$this->versions = $versions;
}

public function getTitle(): string
{
return 'Delete old version records';
}

public function getJobType(): int
{
return QueuedJob::QUEUED;
}

public function setup(): void
{
$this->remainingVersions = $this->versions;
$this->totalSteps = count($this->versions);
}

/**
* @throws Exception
*/
public function process(): void
{
$remaining = $this->remainingVersions;

if (count($remaining) > 0) {
$item = array_shift($remaining);
$id = $item['id'];
$class = $item['class'];
$versions = $item['versions'];

$count = CleanupService::singleton()->deleteVersions($class, $id, $versions);
$this->addMessage(sprintf('Deleted %d version records', $count));

// Mark item as processed
$this->currentStep += 1;
$this->remainingVersions = $remaining;
}

if (count($this->remainingVersions) > 0) {
return;
}

$this->isComplete = true;
}
}
Loading