Skip to content
Permalink
Browse files

FEATURE: Remove old indices after successful index switch

  • Loading branch information...
daniellienert committed Jul 3, 2018
1 parent 6af64dc commit 1558998f54ae3b0f4ab103661317c07623c30acd
Showing with 37 additions and 0 deletions.
  1. +35 −0 Classes/UpdateAliasJob.php
  2. +2 −0 Configuration/Settings.yaml
@@ -12,6 +12,7 @@
*/
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
use Flowpack\ElasticSearch\Transfer\Exception\ApiException;
use Flowpack\JobQueue\Common\Job\JobInterface;
use Flowpack\JobQueue\Common\Queue\Message;
use Flowpack\JobQueue\Common\Queue\QueueInterface;
@@ -44,6 +45,13 @@ class UpdateAliasJob implements JobInterface
*/
protected $acceptedFailedJobs = -1;
/**
* @Flow\InjectConfiguration(path="cleanupIndicesAfterSuccessfulSwitch")
* @var bool
*/
protected $cleanupIndicesAfterSuccessfulSwitch = true;
/**
* @param string $indexPostfix
*/
@@ -66,6 +74,11 @@ public function execute(QueueInterface $queue, Message $message): bool
if ($this->shouldIndexBeSwitched($queue)) {
$this->nodeIndexer->setIndexNamePostfix($this->indexPostfix);
$this->nodeIndexer->updateIndexAlias();
if ($this->cleanupIndicesAfterSuccessfulSwitch === true) {
$this->cleanupOldIndices();
}
$this->log(sprintf('action=indexing step=index-switched alias=%s message="Index was switched successfully"', $this->indexPostfix), LOG_NOTICE);
} else {
$this->log(sprintf('action=indexing step=index-switched alias=%s message="Index was not switched due to %s failed batches in the current queue"', $this->indexPostfix, $queue->countFailed()), LOG_ERR);
@@ -94,6 +107,28 @@ public function getLabel(): string
return sprintf('ElasticSearch Indexing Job (%s)', $this->getIdentifier());
}
/**
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
*/
protected function cleanupOldIndices()
{
try {
$indicesToBeRemoved = $this->nodeIndexer->removeOldIndices();
if (count($indicesToBeRemoved) > 0) {
foreach ($indicesToBeRemoved as $indexToBeRemoved) {
$this->log(sprintf('action=indexing step=index-switched alias=%s message="Old index was successfully removed"', $indexToBeRemoved), LOG_INFO);
}
}
} catch (ApiException $exception) {
$response = json_decode($exception->getResponse());
if ($response->error instanceof \stdClass) {
$this->log(sprintf('action=indexing step=index-switched alias=%s message="Old indices could not be removed. ElasticSearch responded with status %s, saying "%s: %s"', $this->indexPostfix, $response->status, $response->error->type, $response->error->reason), LOG_ERR);
} else {
$this->log(sprintf('action=indexing step=index-switched alias=%s message="Old indices could not be removed. ElasticSearch responded with status %s, saying "%s"', $this->indexPostfix, $response->status, $response->error), LOG_ERR);
}
}
}
/**
* @param QueueInterface $queue
* @return bool
@@ -9,6 +9,8 @@ Flowpack:
# -1 means, accept any number of failures
acceptedFailedJobs: 0

# Run a nodeindex:cleanup after the node switching job switched the index
cleanupIndicesAfterSuccessfulSwitch: true
JobQueue:
Common:
presets:

0 comments on commit 1558998

Please sign in to comment.
You can’t perform that action at this time.