Skip to content

Commit 2849495

Browse files
committed
Update ElementIndexerService.php
add method to delete jobs in queue Update Elasticsearch.php Add deleting also for assets
1 parent afe13cb commit 2849495

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/Elasticsearch.php

+15
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ function (Event $event) {
100100
}
101101
);
102102

103+
// Remove asset from the index upon deletion
104+
Event::on(
105+
Asset::class,
106+
Asset::EVENT_AFTER_DELETE,
107+
function (Event $event) {
108+
/** @var Asset $asset */
109+
$asset = $event->sender;
110+
try {
111+
$this->elementIndexerService->deleteElement($asset);
112+
} catch (Exception $e) {
113+
// Noop, the element must have already been deleted
114+
}
115+
}
116+
);
117+
103118
// Index entry, asset & products upon save (creation or update)
104119
Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, [$this, 'onElementSaved']);
105120
Event::on(Asset::class, Asset::EVENT_AFTER_SAVE, [$this, 'onElementSaved']);

src/services/ElementIndexerService.php

+37
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use lhs\elasticsearch\Elasticsearch as ElasticsearchPlugin;
2424
use lhs\elasticsearch\exceptions\IndexElementException;
2525
use lhs\elasticsearch\records\ElasticsearchRecord;
26+
use yii\db\Query;
2627

2728
/**
2829
*/
@@ -94,6 +95,7 @@ public function indexElement(Element $element): ?string
9495

9596
/**
9697
* Removes an entry from the Elasticsearch index
98+
*
9799
* @param Element $element The entry to delete
98100
* @return int The number of rows deleted
99101
* @throws \yii\elasticsearch\Exception If the entry to be deleted cannot be found
@@ -102,11 +104,46 @@ public function deleteElement(Element $element): int
102104
{
103105
Craft::info("Deleting entry #{$element->id}: {$element->url}", __METHOD__);
104106

107+
$this->deleteElementFromQueue($element);
108+
105109
ElasticsearchRecord::$siteId = $element->siteId;
106110

107111
return ElasticsearchRecord::deleteAll(['_id' => $element->id]);
108112
}
109113

114+
/**
115+
* Removes all entries for an element from queue
116+
* @param Element $element
117+
* @return void
118+
* @throws \yii\base\InvalidConfigException
119+
*/
120+
protected function deleteElementFromQueue(Element $element): void
121+
{
122+
$job = new IndexElementJob(
123+
[
124+
'siteId' => $element->getSite()->id,
125+
'elementId' => $element->id,
126+
'type' => get_class($element),
127+
]
128+
);
129+
130+
$queueService = Craft::$app->getQueue();
131+
$queueClass = get_class($queueService);
132+
133+
$entries = (new Query())
134+
->from($queueClass::TABLE)
135+
->where([
136+
'job' => Craft::$app->getQueue()->serializer->serialize($job)
137+
])->all();
138+
139+
foreach ($entries as $entry) {
140+
if (isset($entry['id'])) {
141+
$methodName = $queueService instanceof \yii\queue\db\Queue ? 'remove' : 'release';
142+
$queueService->$methodName($entry['id']);
143+
}
144+
}
145+
}
146+
110147
/**
111148
* Get the reason why an entry should NOT be reindex.
112149
* @param Element $element The element to consider for reindexing

0 commit comments

Comments
 (0)