Skip to content

Commit

Permalink
feat: add indexer preparing state
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Apr 10, 2024
1 parent 23a8796 commit 57de5c5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/Console/Command/Io/IndexerProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class IndexerProgressBar implements IndexerProgressHandler

private IndexerProgressHandler $currentProgressHandler;

private int $prepareLines = 0;

/**
* @var array<Throwable>
*/
Expand All @@ -37,8 +39,18 @@ public function init(
$this->progressBar = null;
}

public function prepare(string $message): void
{
$this->currentProgressHandler->prepare($message);
$this->output->writeln($message);
$this->prepareLines++;
}

public function start(int $total): void
{
if ($this->prepareLines > 0) {
$this->output->write("\x1b[" . $this->prepareLines . "A");
}
$this->currentProgressHandler->start($total);
$this->progressBar = new ProgressBar($this->output, $total);
$this->formatProgressBar('green');
Expand Down
13 changes: 12 additions & 1 deletion src/Dto/Indexer/IndexerStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function __construct(
public int $skipped,
public DateTime $lastUpdate,
public int $updated,
public int $errors
public int $errors,
public string $prepareMessage = ''
) {
}

Expand All @@ -53,6 +54,7 @@ public static function empty(): IndexerStatus

public function getStatusLine(): string
{

$endTime = $this->endTime;
if ($endTime === null || $endTime->getTimestamp() === 0) {
$endTime = new DateTime();
Expand All @@ -63,6 +65,15 @@ public function getStatusLine(): string
if ($lastUpdate->getTimestamp() === 0) {
$lastUpdate = $endTime;
}

if ($this->state === IndexerStatusState::PREPARING) {
return
'[' . $this->state->name . '] ' .
'start: ' . $this->startTime->format('d.m.Y H:i') . ', ' .
'time: ' . $duration->format('%Hh %Im %Ss') . ', ' .
'message: ' . $this->prepareMessage;
}

return
'[' . $this->state->name . '] ' .
'start: ' . $this->startTime->format('d.m.Y H:i') . ', ' .
Expand Down
1 change: 1 addition & 0 deletions src/Dto/Indexer/IndexerStatusState.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
enum IndexerStatusState: string
{
case UNKNOWN = 'UNKNOWN';
case PREPARING = 'PREPARING';
case RUNNING = 'RUNNING';
case FINISHED = 'FINISHED';
case ABORTED = 'ABORTED';
Expand Down
1 change: 1 addition & 0 deletions src/Service/Indexer/IndexerProgressHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

interface IndexerProgressHandler
{
public function prepare(string $message): void;
public function start(int $total): void;
public function startUpdate(int $total): void;
public function advance(int $step): void;
Expand Down
37 changes: 36 additions & 1 deletion src/Service/Indexer/IndexerProgressState.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,38 @@ public function __construct(
) {
}

public function prepare(string $message): void
{
$this->status = new IndexerStatus(
IndexerStatusState::PREPARING,
new DateTime(),
null,
0,
0,
0,
new DateTime(),
0,
0,
$message
);
$this->statusStore->store(
$this->getStatusStoreKey(),
$this->status
);
}

public function start(int $total): void
{
$storedStatus = $this->statusStore->load($this->getStatusStoreKey());

$startTime = new DateTime();
if ($storedStatus->state === IndexerStatusState::PREPARING) {
$startTime = $storedStatus->startTime;
}

$this->status = new IndexerStatus(
IndexerStatusState::RUNNING,
new DateTime(),
$startTime,
null,
$total,
0,
Expand All @@ -38,6 +65,10 @@ public function start(int $total): void
0,
0
);
$this->statusStore->store(
$this->getStatusStoreKey(),
$this->status
);
}

/**
Expand All @@ -58,6 +89,10 @@ public function startUpdate(int $total): void
$storedStatus->updated,
$storedStatus->errors,
);
$this->statusStore->store(
$this->getStatusStoreKey(),
$this->status
);
}

public function advance(int $step): void
Expand Down
3 changes: 3 additions & 0 deletions src/Service/Indexer/InternalResourceIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public function index(): IndexerStatus
'chunkSize' => $param->chunkSize,
'cleanupThreshold' => $param->cleanupThreshold,
]);

$this->progressHandler->prepare('Collect resource locations');

try {
$paths = $this->finder->findAll();
$this->deleteErrorProtocol($this->getBaseIndex());
Expand Down

0 comments on commit 57de5c5

Please sign in to comment.