Skip to content

Commit

Permalink
Allow to configure ElasticSearch index in esb.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
mmenozzi committed Oct 29, 2024
1 parent 8933545 commit 4dac01b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions esb.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ services:
flows:
sample_flow: # The flow "code" and will be the Beanstalkd tube name
description: Sample Flow # The flow description
es_index: # Optional: the create/update ElasticSearch index API body (see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html). This is useful if you want to control index mapping, settings and aliases.
settings: # For example you can set the total_fields limit to an higher (or lower) value:
index:
total_fields:
limit: 2000
producer:
service: My\Esb\Producer # A producer service ID defined above
batch_size: 1200 # Jobs are produced in batches of 1200 jobs. Optional: default is 1000
Expand Down
1 change: 1 addition & 0 deletions src/FlowConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->arrayPrototype()
->children()
->scalarNode('description')->isRequired()->cannotBeEmpty()->end()
->variableNode('es_index')->defaultNull()->end()
->arrayNode('producer')
->children()
->scalarNode('service')->isRequired()->end()
Expand Down
8 changes: 8 additions & 0 deletions src/Model/FlowConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,12 @@ public function getProducerBatchSize(): int
{
return $this->config['producer']['batch_size'];
}

/**
* @return array<array-key, mixed>|null
*/
public function getElasticSearchIndexCreateOrUpdateBody(): ?array
{
return $this->config['es_index'] ?? null;
}
}
8 changes: 8 additions & 0 deletions src/Service/ElasticSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public function getClient(): Client
return $this->client;
}

/**
* @param array<array-key, mixed>|null $createOrUpdateBody
*/
public function setElasticSearchIndex(string $indexName, array $createOrUpdateBody = null): Amp\Promise
{
return $this->client->createOrUpdateIndex($indexName, $createOrUpdateBody);
}

/**
* @param JobInterface $job
* @param string $indexName
Expand Down
13 changes: 13 additions & 0 deletions src/Service/QueueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ public function __construct(
public function boot(): Promise
{
return call(function () {
if ($this->flowConfig->getElasticSearchIndexCreateOrUpdateBody() !== null) {
yield $this->elasticSearch->setElasticSearchIndex(
$this->flowConfig->getTube(),
$this->flowConfig->getElasticSearchIndexCreateOrUpdateBody()
);
$this->logger->debug(
'Successfully set ElasticSearch index',
[
'index' => $this->flowConfig->getTube(),
'body' => $this->flowConfig->getElasticSearchIndexCreateOrUpdateBody()
]
);
}
//Producer
yield $this->beanstalkClient->use($this->flowConfig->getTube());

Expand Down

0 comments on commit 4dac01b

Please sign in to comment.