Skip to content

Commit 99e6db4

Browse files
committed
refactor for ES 8.0 compatibility
mostly due to "Exception: Serialization of 'CurlMultiHandle' is not allowed"
1 parent 0dec425 commit 99e6db4

File tree

10 files changed

+76
-63
lines changed

10 files changed

+76
-63
lines changed

src/Client/ElasticaClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ public function getIndex(string $name): Index
4141
return $this->client->getIndex($name);
4242
}
4343

44-
public function enableEventListener(): void
44+
public static function enableEventListener(): void
4545
{
4646
self::$listenToEvents = true;
4747
}
4848

49-
public function disableEventListener(): void
49+
public static function disableEventListener(): void
5050
{
5151
self::$listenToEvents = false;
5252
}
5353

54-
public function listensToEvents(): bool
54+
public static function listensToEvents(): bool
5555
{
5656
return self::$listenToEvents ?? config('elastica-bridge.events.listen', true);
5757
}

src/Commands/IndexCommand.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,24 @@ public function handle(): int
4747
continue;
4848
}
4949

50+
$indexConfigKey = $indexConfig::class;
51+
5052
Bus::batch([
5153
[
52-
new SetupIndex($indexConfig, $this->option('delete')),
53-
new PopulateIndex($indexConfig),
54+
new SetupIndex($indexConfigKey, $this->option('delete')),
55+
new PopulateIndex($indexConfigKey),
5456
],
5557
])
5658
->onConnection(config('elastica-bridge.connection'))
57-
->then(function () use ($indexConfig): void {
58-
ActivateIndex::dispatch($indexConfig)
59+
->then(function () use ($indexConfigKey): void {
60+
ActivateIndex::dispatch($indexConfigKey)
5961
->onConnection(config('elastica-bridge.connection'));
6062
})
61-
->catch(function () use ($indexConfig): void {
62-
$indexConfig->indexingLock()->forceRelease();
63+
->catch(function () use ($indexConfigKey): void {
64+
app(IndexRepository::class)->get($indexConfigKey)->indexingLock()->forceRelease();
6365
})
64-
->finally(function () use ($indexConfig): void {
65-
$indexConfig->indexingLock()->forceRelease();
66+
->finally(function () use ($indexConfigKey): void {
67+
app(IndexRepository::class)->get($indexConfigKey)->indexingLock()->forceRelease();
6668
})
6769
->name('ES index: '.$indexConfig->getName())
6870
->dispatch();

src/Events/EventHandler.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
class EventHandler
1111
{
1212
public function __construct(
13-
private readonly ElasticaClient $elasticaClient,
1413
private readonly ModelEventListener $modelEventListener
1514
) {}
1615

@@ -21,13 +20,13 @@ public function subscribe(Dispatcher $events): void
2120
->map(fn (string $name): string => sprintf('eloquent.%s:*', $name))
2221
->toArray(),
2322
function ($event, $models): void {
24-
if (! $this->elasticaClient->listensToEvents()) {
23+
if (! ElasticaClient::listensToEvents()) {
2524
return;
2625
}
2726

2827
dispatch(function () use ($event, $models): void {
2928
$name = (str($event)->before(':')->after('.'));
30-
if (! $this->elasticaClient->listensToEvents()) {
29+
if (! ElasticaClient::listensToEvents()) {
3130
return;
3231
}
3332

src/Jobs/AbstractIndexJob.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ abstract class AbstractIndexJob implements ShouldBeUnique, ShouldQueue
2020
use Queueable;
2121
use SerializesModels;
2222

23-
protected IndexInterface $indexConfig;
23+
/**
24+
* @var class-string<IndexInterface>
25+
*/
26+
protected string $indexConfigKey;
2427

2528
public function uniqueId(): string
2629
{
27-
return $this->indexConfig::class;
30+
return $this->indexConfigKey;
2831
}
2932

3033
/**

src/Jobs/ActivateIndex.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@
55
namespace Limenet\LaravelElasticaBridge\Jobs;
66

77
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
8+
use Limenet\LaravelElasticaBridge\Repository\IndexRepository;
89

910
class ActivateIndex extends AbstractIndexJob
1011
{
12+
/**
13+
* @param class-string<IndexInterface> $indexConfigKey
14+
*/
1115
public function __construct(
12-
protected IndexInterface $indexConfig
16+
protected string $indexConfigKey
1317
) {}
1418

15-
public function handle(): void
19+
public function handle(IndexRepository $indexRepository): void
1620
{
17-
$newIndex = $this->indexConfig->getBlueGreenInactiveElasticaIndex();
21+
$indexConfig = $indexRepository->get($this->indexConfigKey);
22+
23+
$newIndex = $indexConfig->getBlueGreenInactiveElasticaIndex();
1824
$newIndex->flush();
19-
$newIndex->addAlias($this->indexConfig->getName(), true);
25+
$newIndex->addAlias($indexConfig->getName(), true);
2026
}
2127
}

src/Jobs/PopulateBatchIndex.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Limenet\LaravelElasticaBridge\Jobs;
66

7-
use Elastica\Index;
87
use Illuminate\Bus\Batchable;
98
use Illuminate\Bus\Queueable;
109
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -13,6 +12,7 @@
1312
use Illuminate\Queue\SerializesModels;
1413
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
1514
use Limenet\LaravelElasticaBridge\Model\ElasticsearchableInterface;
15+
use Limenet\LaravelElasticaBridge\Repository\IndexRepository;
1616
use Throwable;
1717

1818
class PopulateBatchIndex implements ShouldQueue
@@ -23,16 +23,19 @@ class PopulateBatchIndex implements ShouldQueue
2323
use Queueable;
2424
use SerializesModels;
2525

26+
/**
27+
* @param class-string<IndexInterface> $indexConfigKey
28+
*/
2629
public function __construct(
27-
private readonly Index $index,
28-
private readonly IndexInterface $indexConfig,
30+
protected string $indexConfigKey,
2931
private readonly string $indexDocument,
3032
private readonly int $limit,
3133
private readonly int $offset
3234
) {}
3335

34-
public function handle(): void
36+
public function handle(IndexRepository $indexRepository): void
3537
{
38+
$indexConfig = $indexRepository->get($this->indexConfigKey);
3639
if ($this->batch()?->cancelled() === true) {
3740
return;
3841
}
@@ -42,21 +45,21 @@ public function handle(): void
4245
/** @var ElasticsearchableInterface[] $records */
4346
$records = $this->indexDocument::offset($this->offset)->limit($this->limit)->get();
4447
foreach ($records as $record) {
45-
if (! $record->shouldIndex($this->indexConfig)) {
48+
if (! $record->shouldIndex($indexConfig)) {
4649
continue;
4750
}
4851

49-
$esDocuments[] = $record->toElasticaDocument($this->indexConfig);
52+
$esDocuments[] = $record->toElasticaDocument($indexConfig);
5053
}
5154

5255
if ($esDocuments === []) {
5356
return;
5457
}
5558

5659
try {
57-
$this->index->addDocuments($esDocuments);
60+
$indexConfig->getBlueGreenInactiveElasticaIndex()->addDocuments($esDocuments);
5861
} catch (Throwable $throwable) {
59-
if (! $this->indexConfig->ingoreIndexingErrors()) {
62+
if (! $indexConfig->ingoreIndexingErrors()) {
6063
throw $throwable;
6164
}
6265
}

src/Jobs/PopulateIndex.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,43 @@
66

77
use Illuminate\Bus\Batchable;
88
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
9+
use Limenet\LaravelElasticaBridge\Repository\IndexRepository;
910

1011
class PopulateIndex extends AbstractIndexJob
1112
{
1213
use Batchable;
1314

15+
/**
16+
* @param class-string<IndexInterface> $indexConfigKey
17+
*/
1418
public function __construct(
15-
protected IndexInterface $indexConfig
19+
protected string $indexConfigKey
1620
) {}
1721

18-
public function handle(): void
22+
public function handle(IndexRepository $indexRepository): void
1923
{
24+
$indexConfig = $indexRepository->get($this->indexConfigKey);
25+
2026
if ($this->batch()?->cancelled() === true) {
2127
return;
2228
}
2329

24-
$index = $this->indexConfig->getBlueGreenInactiveElasticaIndex();
30+
$index = $indexConfig->getBlueGreenInactiveElasticaIndex();
2531

2632
$index->delete();
27-
$index->create($this->indexConfig->getCreateArguments());
33+
$index->create($indexConfig->getCreateArguments());
2834

2935
$jobs = [];
3036

31-
foreach ($this->indexConfig->getAllowedDocuments() as $indexDocument) {
37+
foreach ($indexConfig->getAllowedDocuments() as $indexDocument) {
3238
$modelCount = $indexDocument::count();
3339

34-
for ($batchNumber = 0; $batchNumber < ceil($modelCount / $this->indexConfig->getBatchSize()); $batchNumber++) {
40+
for ($batchNumber = 0; $batchNumber < ceil($modelCount / $indexConfig->getBatchSize()); $batchNumber++) {
3541
$jobs[] = new PopulateBatchIndex(
36-
$index,
37-
$this->indexConfig,
42+
$indexConfig::class,
3843
$indexDocument,
39-
$this->indexConfig->getBatchSize(),
40-
$batchNumber * $this->indexConfig->getBatchSize()
44+
$indexConfig->getBatchSize(),
45+
$batchNumber * $indexConfig->getBatchSize()
4146
);
4247
}
4348
}

src/Jobs/SetupIndex.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,35 @@
44

55
namespace Limenet\LaravelElasticaBridge\Jobs;
66

7-
use Elastica\Exception\ExceptionInterface as ElasticaException;
87
use Illuminate\Bus\Batchable;
98
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
109
use Limenet\LaravelElasticaBridge\Enum\IndexBlueGreenSuffix;
1110
use Limenet\LaravelElasticaBridge\Exception\Index\BlueGreenIndicesIncorrectlySetupException;
1211
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
12+
use Limenet\LaravelElasticaBridge\Repository\IndexRepository;
1313
use Limenet\LaravelElasticaBridge\Util\ElasticsearchResponse;
1414

1515
class SetupIndex extends AbstractIndexJob
1616
{
17+
private IndexInterface $indexConfig;
18+
1719
use Batchable;
1820

21+
/**
22+
* @param class-string<IndexInterface> $indexConfigKey
23+
*/
1924
public function __construct(
20-
protected IndexInterface $indexConfig,
25+
protected string $indexConfigKey,
2126
private readonly bool $deleteExisting
2227
) {}
2328

24-
public function handle(ElasticaClient $elastica): void
29+
public function handle(ElasticaClient $elastica, IndexRepository $indexRepository): void
2530
{
2631
if ($this->batch()?->cancelled() === true) {
2732
return;
2833
}
2934

35+
$this->indexConfig = $indexRepository->get($this->indexConfigKey);
3036
$this->migrate($elastica);
3137
$this->cleanup($elastica);
3238
$this->setup($elastica);
@@ -38,20 +44,13 @@ private function migrate(ElasticaClient $elastica): void
3844
return;
3945
}
4046

41-
$index = $elastica->getClient()->getIndex($this->indexConfig->getName());
42-
43-
try {
44-
$response = ElasticsearchResponse::getResponse($elastica->getClient()->indices()->existsAlias(['name' => $this->indexConfig->getName()]))->asBool();
45-
} catch (ElasticaException) {
46-
if ($index->exists() && $index->getAliases() === []) {
47-
$index->delete();
48-
}
49-
50-
return;
51-
}
47+
$nonAliasIndex = $elastica->getClient()->getIndex($this->indexConfig->getName());
5248

53-
if ($index->exists()) {
54-
$index->delete();
49+
if (
50+
$nonAliasIndex->exists()
51+
&& ! ElasticsearchResponse::getResponse($elastica->getClient()->indices()->existsAlias(['name' => $this->indexConfig->getName()]))->asBool()
52+
) {
53+
$nonAliasIndex->delete();
5554
}
5655
}
5756

src/Services/ModelEventListener.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Limenet\LaravelElasticaBridge\Services;
66

7-
use Elastica\Exception\NotFoundException;
7+
use Elastic\Elasticsearch\Exception\ClientResponseException;
88
use Illuminate\Database\Eloquent\Model;
99
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
1010
use Limenet\LaravelElasticaBridge\Model\ElasticsearchableInterface;
@@ -30,10 +30,6 @@ class ModelEventListener
3030
self::EVENT_DELETED,
3131
];
3232

33-
public function __construct(
34-
private readonly IndexRepository $indexRepository
35-
) {}
36-
3733
public function handle(string $event, Model $model): void
3834
{
3935
if (! $model instanceof ElasticsearchableInterface) {
@@ -68,7 +64,7 @@ private function ensureModelMissingFromIndex(IndexInterface $index, Model $model
6864
{
6965
try {
7066
$index->getElasticaIndex()->deleteById($model->getElasticsearchId());
71-
} catch (NotFoundException) {
67+
} catch (ClientResponseException) {
7268
}
7369
}
7470

@@ -78,7 +74,7 @@ private function ensureModelMissingFromIndex(IndexInterface $index, Model $model
7874
public function matchingIndicesForElement(Model $model): array
7975
{
8076
return array_filter(
81-
$this->indexRepository->all(),
77+
app(IndexRepository::class)->all(),
8278
fn (IndexInterface $index): bool => in_array($model::class, $index->getAllowedDocuments(), true)
8379
);
8480
}

tests/Feature/TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Limenet\LaravelElasticaBridge\Tests\Feature;
66

7-
use Elastica\Exception\ResponseException;
7+
use Elastica\Exception\ExceptionInterface;
88
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
99
use Limenet\LaravelElasticaBridge\Exception\BaseException;
1010
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
@@ -67,7 +67,7 @@ protected function cleanupIndices(): void
6767
if ($inactive->exists()) {
6868
$inactive->delete();
6969
}
70-
} catch (BaseException|ResponseException) {
70+
} catch (BaseException|ExceptionInterface) {
7171
}
7272
}
7373
}

0 commit comments

Comments
 (0)