refactored jobs, cleaned up

This commit is contained in:
Daniel Klabbers 2021-11-02 13:55:41 +01:00
parent 254c58101b
commit 47fc5c2f1b
8 changed files with 49 additions and 64 deletions

View File

@ -35,6 +35,13 @@ All mutations to discussions are automatically added and removed from the elasti
*I have another question.* *I have another question.*
Reach out to us via https://helpdesk.blomstra.net. We will get back to you as soon as we can. If you have a running subscription please mention when you started your plan and/or which plan you are on. Always add sufficient information when reporting errors. We prefer errors being reported here, but understand that sometimes you can't. Reach out to us via https://helpdesk.blomstra.net. We will get back to you as soon as we can. If you have a running subscription please mention when you started your plan and/or which plan you are on. Always add sufficient information when reporting errors. We prefer errors being reported here, but understand that sometimes you can't.
*Can I dispatch the sync jobs to another queue?*
Yes:
```php
\Blomstra\Search\Observe\Job::$onQueue = 'sync';
```
--- ---
- Blomstra provides managed Flarum hosting. - Blomstra provides managed Flarum hosting.

View File

@ -2,7 +2,7 @@
namespace Blomstra\Search\Commands; namespace Blomstra\Search\Commands;
use Blomstra\Search\Observe\SavingJob; use Blomstra\Search\Jobs\SavingJob;
use Blomstra\Search\Seeders\Seeder; use Blomstra\Search\Seeders\Seeder;
use Elasticsearch\Client; use Elasticsearch\Client;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
@ -59,7 +59,6 @@ class RebuildDocumentsCommand extends Command
'index' => $index, 'index' => $index,
'body' => [ 'body' => [
'settings' => [ 'settings' => [
'analysis' => [ 'analysis' => [
'analyzer' => [ 'analyzer' => [
'flarum_analyzer' => [ 'flarum_analyzer' => [

33
src/Jobs/DeletingJob.php Normal file
View File

@ -0,0 +1,33 @@
<?php
namespace Blomstra\Search\Jobs;
use Elasticsearch\Client;
use Illuminate\Contracts\Container\Container;
use Illuminate\Database\Eloquent\Model;
class DeletingJob extends Job
{
public function handle(Container $container)
{
if ($this->models->isEmpty()) return;
/** @var Client $client */
$client = $container->make('blomstra.search.elastic');
// Preparing body for storing.
$body = $this->models->map(function (Model $model) {
$document = $this->seeder->toDocument($model);
return [
['delete' => ['_index' => $this->index, '_id' => $document->id]]
];
})->flatten(1);
$response = $client->bulk([
'index' => $this->index,
'body' => $body->toArray(),
'refresh' => true
]);
}
}

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Blomstra\Search\Observe; namespace Blomstra\Search\Jobs;
use Blomstra\Search\Seeders\Seeder; use Blomstra\Search\Seeders\Seeder;
use Flarum\Queue\AbstractJob; use Flarum\Queue\AbstractJob;
@ -10,8 +10,12 @@ abstract class Job extends AbstractJob
{ {
protected string $index; protected string $index;
public static ?string $onQueue = null;
public function __construct(protected Collection $models, protected Seeder $seeder) public function __construct(protected Collection $models, protected Seeder $seeder)
{ {
$this->index = resolve('blomstra.search.elastic_index'); $this->index = resolve('blomstra.search.elastic_index');
if (static::$onQueue) $this->onQueue(static::$onQueue);
} }
} }

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Blomstra\Search\Observe; namespace Blomstra\Search\Jobs;
use Elasticsearch\Client; use Elasticsearch\Client;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;

View File

@ -1,30 +0,0 @@
<?php
namespace Blomstra\Search;
use Blomstra\Search\Documents\Document;
use Illuminate\Contracts\Container\Container;
class Manager
{
public function __construct(protected Container $container)
{}
public function document(string $type): ?string
{
return $this->fromTagged('blomstra.search.documents', $type);
}
protected function fromTagged(string $binding, $search): ?string
{
$entities = $this->container->tagged($binding);
foreach ($entities as $entity) {
$instance = $this->container->make($entity);
if ($instance->type() === $search) return $entity;
}
return null;
}
}

View File

@ -1,28 +0,0 @@
<?php
namespace Blomstra\Search\Observe;
use Illuminate\Database\Eloquent\Model;
use MeiliSearch\Client;
class DeletingJob extends Job
{
public function handle(Client $meili)
{
if ($this->models->isEmpty()) return;
$document = $this->getDocument();
if (! $document) return;
$keys = $this->models->map(function (Model $model) use ($document) {
return (new $document)($model)->id();
})->toArray();
$meili
->index(resolve('blomstra.search.elastic_index'))
->deleteDocuments(
$keys
);
}
}

View File

@ -2,8 +2,8 @@
namespace Blomstra\Search; namespace Blomstra\Search;
use Blomstra\Search\Observe\DeletingJob; use Blomstra\Search\Jobs\DeletingJob;
use Blomstra\Search\Observe\SavingJob; use Blomstra\Search\Jobs\SavingJob;
use Blomstra\Search\Seeders; use Blomstra\Search\Seeders;
use Elasticsearch\ClientBuilder; use Elasticsearch\ClientBuilder;
use Flarum\Foundation\AbstractServiceProvider; use Flarum\Foundation\AbstractServiceProvider;