Skip to content

Commit

Permalink
Added AfterIndexImport event #283
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Nov 8, 2023
1 parent c53d55a commit 3795cc8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
All notable changes to this project will be documented in this file.

## 3.3.1 - 2023-11-08

### Added
- Added `AfterIndexImport` event, which fires after the ImportIndex job has finished. ([#283](https://github.com/studioespresso/craft-scout/issues/283))
### Fixed
- Deindexing now also takes the correct siteId into account ([#281](https://github.com/studioespresso/craft-scout/issues/281))

Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,29 @@ The event has a properties:

An example use-case for this would be to check the type of the element that's beind saved and settings `shouldBeSearchable` to `false` when it's a Matrix block.

### AfterIndexImport

This event runs at the end of the `ImportIndex` job, when every item has been processed.

```php

use rias\scout\events\AfterIndexImport;
use rias\scout\jobs\ImportIndex;

Event::on(
ImportIndex::class,
ImportIndex::EVENT_AFTER_INDEX_IMPORT,
function (AfterIndexImport $event) {
// Your code here
});
```

The event has one property:
- $indexName (the name of the index that just finished importing

An example use-case for this would be to keep a log or dateLastImported if you're running imports on a schedule.


---

## Upgrading from 1.x
Expand Down
22 changes: 22 additions & 0 deletions src/events/AfterIndexImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace rias\scout\events;

use craft\base\Element;
use rias\scout\ScoutIndex;
use yii\base\Event;

/**
* This event fires right at the end of je ImportIndexJob, after every element has been imported.
*/
class AfterIndexImport extends Event
{
/**
* The index that has been imported
*
* @var string
*/
public string $indexName;


}
12 changes: 11 additions & 1 deletion src/jobs/ImportIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

namespace rias\scout\jobs;

use craft\base\Event;
use craft\queue\BaseJob;
use rias\scout\engines\Engine;
use rias\scout\events\AfterIndexImport;
use rias\scout\Scout;

class ImportIndex extends BaseJob
{
/** @var string */
public $indexName;

public const EVENT_AFTER_INDEX_IMPORT = "afterIndexImport";

public function execute($queue): void
{
/** @var Engine $engine */
$engine = Scout::$plugin->getSettings()->getEngines()->first(function(Engine $engine) {
$engine = Scout::$plugin->getSettings()->getEngines()->first(function (Engine $engine) {
return $engine->scoutIndex->indexName === $this->indexName;
});

Expand All @@ -33,6 +37,12 @@ public function execute($queue): void
$elementsUpdated += count($elements);
$this->setProgress($queue, $elementsUpdated / $elementsCount);
}

$event = new AfterIndexImport([
'indexName' => $this->indexName
]);

Event::trigger(self::class, self::EVENT_AFTER_INDEX_IMPORT, $event);
}

protected function defaultDescription(): string
Expand Down

0 comments on commit 3795cc8

Please sign in to comment.