From 2f70499f154edc68c3fccc01da5e05d59ed0e172 Mon Sep 17 00:00:00 2001 From: Rias Date: Tue, 12 Dec 2017 09:51:13 +0100 Subject: [PATCH] Move indexing to jobs --- CHANGELOG.md | 4 ++ README.md | 9 +-- composer.json | 2 +- src/Scout.php | 17 ++++-- src/jobs/DeIndexElement.php | 55 +++++++++++++++++++ .../ScoutTask.php => jobs/IndexElement.php} | 42 ++++---------- src/models/IndexModel.php | 32 +---------- 7 files changed, 89 insertions(+), 72 deletions(-) create mode 100644 src/jobs/DeIndexElement.php rename src/{tasks/ScoutTask.php => jobs/IndexElement.php} (57%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57f8891..089f04f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 0.1.1 - 2017-12-12 +### Added +- Move indexing to jobs + ## 0.1.0 - 2017-12-11 ### Added - Initial release diff --git a/README.md b/README.md index 85d010a..0c48534 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Scout plugin for Craft CMS 3.x +# Scout plugin for Craft CMS 3 Craft Scout provides a simple solution for adding full-text search to your entries. Scout will automatically keep your search indexes in sync with your entries. @@ -22,9 +22,9 @@ To install the plugin, follow these instructions. ## Setup -To define your Indices, create a new `scout.php` file within your `config` folder. This file should return an array with 3 keys, an `application_id`, your `admin_api_key` (which are both found in your [Algolia](https://www.algolia.com/api-keys) account) and a `mappings` key, which defines your site's mappings. +To define your indices, create a new `scout.php` file within your `config` folder. This file should return an array with 3 keys, an `application_id`, your `admin_api_key` (which are both found in your [Algolia](https://www.algolia.com/api-keys) account) and a `mappings` key, which defines your site's mappings. -Within the mappings array, each index is represented by an array, and values are the configuration. +Within the mappings array, each index is represented by a configuration array. ```php toArray(); } ], + ... ], ]; ``` @@ -53,7 +54,7 @@ return [ The index name in Algolia, if you don't already have an index created, Scout will create one for you. #### `elementType` -The element type that this index contains, most of the time this will be `\craft\elements\Entry::class` +The element type that this index contains, most of the time this will be `craft\elements\Entry::class` Craft's default element type classes are: diff --git a/composer.json b/composer.json index ea20516..51c01b0 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "rias/scout", "description": "Craft Scout provides a simple solution for adding full-text search to your entries. Scout will automatically keep your search indexes in sync with your entries.", "type": "craft-plugin", - "version": "0.1.0", + "version": "0.1.1", "keywords": [ "craft", "cms", diff --git a/src/Scout.php b/src/Scout.php index d72834b..b642f46 100644 --- a/src/Scout.php +++ b/src/Scout.php @@ -14,6 +14,8 @@ use craft\base\Element; use craft\events\ModelEvent; use craft\records\Entry; +use rias\scout\jobs\DeIndexElement; +use rias\scout\jobs\IndexElement; use rias\scout\services\ScoutService as ScoutServiceService; use rias\scout\models\Settings; @@ -64,8 +66,9 @@ public function init() Element::class, Element::EVENT_AFTER_SAVE, function (ModelEvent $event) { - /* @var Element $event->sender */ - $this->scoutService->indexElement($event->sender); + Craft::$app->queue->push(new IndexElement([ + 'element' => $event->sender, + ])); } ); @@ -76,8 +79,9 @@ function (ModelEvent $event) { Element::class, Element::EVENT_AFTER_MOVE_IN_STRUCTURE, function (ModelEvent $event) { - /* @var Element $event->sender */ - $this->scoutService->indexElement($event->sender); + Craft::$app->queue->push(new IndexElement([ + 'element' => $event->sender, + ])); } ); @@ -88,8 +92,9 @@ function (ModelEvent $event) { Element::class, Element::EVENT_BEFORE_DELETE, function (ModelEvent $event) { - /* @var Element $event->sender */ - $this->scoutService->deindexElement($event->sender); + Craft::$app->queue->push(new DeIndexElement([ + 'element' => $event->sender, + ])); } ); } diff --git a/src/jobs/DeIndexElement.php b/src/jobs/DeIndexElement.php new file mode 100644 index 0000000..4df0b89 --- /dev/null +++ b/src/jobs/DeIndexElement.php @@ -0,0 +1,55 @@ +sender */ + Scout::$plugin->scoutService->deindexElement($this->element); + } + + // Protected Methods + // ========================================================================= + + /** + * @inheritdoc + */ + protected function defaultDescription(): string + { + return Craft::t('scout', 'De-indexing Element'); + } +} diff --git a/src/tasks/ScoutTask.php b/src/jobs/IndexElement.php similarity index 57% rename from src/tasks/ScoutTask.php rename to src/jobs/IndexElement.php index 0214faa..83f812a 100644 --- a/src/tasks/ScoutTask.php +++ b/src/jobs/IndexElement.php @@ -8,27 +8,27 @@ * @copyright Copyright (c) 2017 Rias */ -namespace rias\scout\tasks; +namespace rias\scout\jobs; +use craft\base\Element; +use craft\queue\BaseJob; +use craft\queue\QueueInterface; use rias\scout\Scout; use Craft; -use craft\base\Task; /** * @author Rias * @package Scout * @since 0.1.0 */ -class ScoutTask extends Task +class IndexElement extends BaseJob { - // Public Properties + // Properties // ========================================================================= - /** - * @var string - */ - public $someAttribute = 'Some Default'; + /* @var Element */ + public $element; // Public Methods // ========================================================================= @@ -36,28 +36,10 @@ class ScoutTask extends Task /** * @inheritdoc */ - public function rules() - { - return [ - ['someAttribute', 'string'], - ['someAttribute', 'default', 'value' => 'Some Default'], - ]; - } - - /** - * @inheritdoc - */ - public function getTotalSteps(): int - { - return 1; - } - - /** - * @inheritdoc - */ - public function runStep(int $step) + public function execute($queue) { - return true; + /* @var Element $event->sender */ + Scout::$plugin->scoutService->indexElement($this->element); } // Protected Methods @@ -68,6 +50,6 @@ public function runStep(int $step) */ protected function defaultDescription(): string { - return Craft::t('scout', 'ScoutTask'); + return Craft::t('scout', 'Indexing Element'); } } diff --git a/src/models/IndexModel.php b/src/models/IndexModel.php index fd1b7d6..049388b 100644 --- a/src/models/IndexModel.php +++ b/src/models/IndexModel.php @@ -143,37 +143,7 @@ public function deindexElement(Element $element) } /** - * Adds or removes the supplied elements from the index. - * - * @param $elements array - * - * @return mixed - * @throws \AlgoliaSearch\AlgoliaException - */ - public function indexElements($elements) - { - $toIndex = []; - $toDelete = []; - array_map(function ($element) use (&$toIndex, &$toDelete) { - if ($this->canIndexElement($element)) { - if ($element->enabled) { - $toIndex[] = $this->transformElement($element); - } else { - $toDelete[] = $this->transformElement($element); - } - } - }, $elements); - if (count($toIndex)) { - $this->getIndex()->addObjects($toIndex); - } - if (count($toDelete)) { - $this->getIndex()->deleteObjects($toDelete); - } - return true; - } - - /** - * Returns the transformer based on the given endpoint + * Returns the transformer * * @return callable|TransformerAbstract|object * @throws \yii\base\InvalidConfigException