-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ensi-platform/task-102492
#102492 команда проверки что все нужные топики созданы
- Loading branch information
Showing
6 changed files
with
273 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Ensi\LaravelPhpRdKafka\Commands; | ||
|
||
use Ensi\LaravelPhpRdKafka\KafkaFacade; | ||
use Illuminate\Console\Command; | ||
|
||
class CheckTopicsExistsCommand extends Command | ||
{ | ||
protected $signature = 'kafka:find-not-created-topics | ||
{--validate : вернуть ошибку если есть не созданные топики}'; | ||
protected $description = 'Проверить что все топики из kafka.topics существуют'; | ||
|
||
public function handle(): int | ||
{ | ||
$totalDesiredTopics = 0; | ||
$notFoundTopics = []; | ||
|
||
$connectionNames = KafkaFacade::availableConnections(); | ||
|
||
foreach ($connectionNames as $connectionName) { | ||
$existingTopics = $this->getExistingTopics($connectionName); | ||
|
||
$desiredTopics = KafkaFacade::allTopics($connectionName); | ||
$totalDesiredTopics += count($desiredTopics); | ||
|
||
foreach ($desiredTopics as $topicName) { | ||
if (!in_array($topicName, $existingTopics)) { | ||
$notFoundTopics[] = $topicName; | ||
} | ||
} | ||
} | ||
|
||
$notFoundTopics = count($notFoundTopics); | ||
if ($notFoundTopics) { | ||
$this->output->writeln(join("\n", $notFoundTopics)); | ||
} | ||
|
||
if ($this->option('validate')) { | ||
if ($notFoundTopics) { | ||
$this->output->writeln("\nThere are {$notFoundTopics} not created topics"); | ||
|
||
return self::FAILURE; | ||
} else { | ||
$this->output->writeln("All {$totalDesiredTopics} desired topics exists"); | ||
|
||
return self::SUCCESS; | ||
} | ||
} | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
private function getExistingTopics(string $connectionName): array | ||
{ | ||
$rdKafka = KafkaFacade::rdKafka($connectionName); | ||
$metadata = $rdKafka->getMetadata(true, null, 2000); | ||
|
||
$existingTopics = []; | ||
foreach ($metadata->getTopics() as $topicMeta) { | ||
$existingTopics[] = $topicMeta->getTopic(); | ||
} | ||
|
||
return $existingTopics; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.