Skip to content

Commit 1f86be5

Browse files
authored
Merge pull request #35 from daniellienert/feature/remove-old-indices
FEATURE: Remove old indices after successful index switch
2 parents 6af64dc + 1558998 commit 1f86be5

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Classes/UpdateAliasJob.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
15+
use Flowpack\ElasticSearch\Transfer\Exception\ApiException;
1516
use Flowpack\JobQueue\Common\Job\JobInterface;
1617
use Flowpack\JobQueue\Common\Queue\Message;
1718
use Flowpack\JobQueue\Common\Queue\QueueInterface;
@@ -44,6 +45,13 @@ class UpdateAliasJob implements JobInterface
4445
*/
4546
protected $acceptedFailedJobs = -1;
4647

48+
/**
49+
* @Flow\InjectConfiguration(path="cleanupIndicesAfterSuccessfulSwitch")
50+
* @var bool
51+
*/
52+
protected $cleanupIndicesAfterSuccessfulSwitch = true;
53+
54+
4755
/**
4856
* @param string $indexPostfix
4957
*/
@@ -66,6 +74,11 @@ public function execute(QueueInterface $queue, Message $message): bool
6674
if ($this->shouldIndexBeSwitched($queue)) {
6775
$this->nodeIndexer->setIndexNamePostfix($this->indexPostfix);
6876
$this->nodeIndexer->updateIndexAlias();
77+
78+
if ($this->cleanupIndicesAfterSuccessfulSwitch === true) {
79+
$this->cleanupOldIndices();
80+
}
81+
6982
$this->log(sprintf('action=indexing step=index-switched alias=%s message="Index was switched successfully"', $this->indexPostfix), LOG_NOTICE);
7083
} else {
7184
$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
94107
return sprintf('ElasticSearch Indexing Job (%s)', $this->getIdentifier());
95108
}
96109

110+
/**
111+
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
112+
*/
113+
protected function cleanupOldIndices()
114+
{
115+
try {
116+
$indicesToBeRemoved = $this->nodeIndexer->removeOldIndices();
117+
if (count($indicesToBeRemoved) > 0) {
118+
foreach ($indicesToBeRemoved as $indexToBeRemoved) {
119+
$this->log(sprintf('action=indexing step=index-switched alias=%s message="Old index was successfully removed"', $indexToBeRemoved), LOG_INFO);
120+
}
121+
}
122+
} catch (ApiException $exception) {
123+
$response = json_decode($exception->getResponse());
124+
if ($response->error instanceof \stdClass) {
125+
$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);
126+
} else {
127+
$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);
128+
}
129+
}
130+
}
131+
97132
/**
98133
* @param QueueInterface $queue
99134
* @return bool

Configuration/Settings.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Flowpack:
99
# -1 means, accept any number of failures
1010
acceptedFailedJobs: 0
1111

12+
# Run a nodeindex:cleanup after the node switching job switched the index
13+
cleanupIndicesAfterSuccessfulSwitch: true
1214
JobQueue:
1315
Common:
1416
presets:

0 commit comments

Comments
 (0)