Skip to content

Commit dc95d7b

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 dc95d7b

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-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

+38
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
use craft\helpers\UrlHelper;
2323
use lhs\elasticsearch\Elasticsearch as ElasticsearchPlugin;
2424
use lhs\elasticsearch\exceptions\IndexElementException;
25+
use lhs\elasticsearch\jobs\IndexElementJob;
2526
use lhs\elasticsearch\records\ElasticsearchRecord;
27+
use yii\db\Query;
2628

2729
/**
2830
*/
@@ -94,6 +96,7 @@ public function indexElement(Element $element): ?string
9496

9597
/**
9698
* Removes an entry from the Elasticsearch index
99+
*
97100
* @param Element $element The entry to delete
98101
* @return int The number of rows deleted
99102
* @throws \yii\elasticsearch\Exception If the entry to be deleted cannot be found
@@ -102,11 +105,46 @@ public function deleteElement(Element $element): int
102105
{
103106
Craft::info("Deleting entry #{$element->id}: {$element->url}", __METHOD__);
104107

108+
$this->deleteElementFromQueue($element);
109+
105110
ElasticsearchRecord::$siteId = $element->siteId;
106111

107112
return ElasticsearchRecord::deleteAll(['_id' => $element->id]);
108113
}
109114

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

0 commit comments

Comments
 (0)