Skip to content

Commit

Permalink
chore: cosmetic (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi authored Jul 17, 2024
1 parent 43a3148 commit 38a59db
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function handleSignal(int $signal, int|false $previousExitCode = 0): int|
$result = (bool) $this->batchProcess?->stop();

if ($result) {
$this->io?->warning('Interrupt received, stopping batch processing');
$this->io?->warning('Interrupt received, will stop after the current page');
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ private function getProgressString(BeforePageEvent|AfterPageEvent $event): strin
$event->getEncodedPageIdentifier(),
);
}

return sprintf(
'Page <info>%s</info>/<info>%s</info>, identifier <info>%s</info>',
$event->getPage()->getPageNumber() ?? '?',
$this->totalPages,
$event->getEncodedPageIdentifier(),
);

}

public function beforePage(BeforePageEvent $event): void
Expand Down Expand Up @@ -187,17 +187,20 @@ public function onInterrupt(InterruptEvent $event): void
$nextPageIdentifier = $event->getNextPageIdentifier();

if ($this->progressFile !== null) {
$this->io->warning(sprintf(
'Batch process interrupted. To resume, use the argument "-f %s"',
$this->progressFile
));
$this->io->warning([
'Batch process interrupted. To resume, use the argument:',
'-f ' . $this->progressFile,
]);
} elseif ($nextPageIdentifier !== null) {
$this->io->warning(sprintf(
'Batch process interrupted. To resume, use the argument "-r %s"',
$nextPageIdentifier
));
$this->io->warning([
'Batch process interrupted. To resume, use the argument:',
'-r ' . $nextPageIdentifier,
]);
} else {
$this->io->error('Batch process interrupted, but there does not seem to be a next page identifier for you to resume');
$this->io->warning([
'Batch process interrupted, but there does not seem',
'to be a next page identifier for you to resume',
]);
}

$this->showStats($event);
Expand All @@ -215,13 +218,21 @@ public function onTimeLimit(TimeLimitEvent $event): void

$nextPageIdentifier = $event->getNextPageIdentifier();

if ($nextPageIdentifier !== null) {
$this->io->warning(sprintf(
'Time limit reached. To resume, use the argument "-r %s"',
$nextPageIdentifier
));
if ($this->progressFile !== null) {
$this->io->warning([
'Time limit reached. To resume, use the argument:',
'-f ' . $this->progressFile,
]);
} elseif ($nextPageIdentifier !== null) {
$this->io->warning([
'Time limit reached. To resume, use the argument:',
'-r ' . $nextPageIdentifier,
]);
} else {
$this->io->error('Time limit reached, but there does not seem to be a next page identifier for you to resume');
$this->io->warning([
'Time limit reached, but there does not seem',
'to be a next page identifier for you to resume',
]);
}

$this->showStats($event);
Expand Down Expand Up @@ -254,8 +265,6 @@ private function showStats(BeforeProcessEvent|AfterPageEvent|AfterProcessEvent|I
['Start page' => $this->startPageIdentifier ?? '(first page)'],
['Progress file' => $this->progressFile ?? '(not used)'],
['Items per page' => $this->itemsPerPage],
['Total pages' => $this->totalPages ?? '(unknown)'],
['Total items' => $this->totalItems ?? '(unknown)'],
];

$stats[] = ['Start time' => $this->formatTime($this->getStartTime())];
Expand All @@ -282,25 +291,58 @@ private function showStats(BeforeProcessEvent|AfterPageEvent|AfterProcessEvent|I
}

if ($processDuration !== null) {
$stats[] = ['Time elapsed' => Helper::formatTime($processDuration)];

if ($eta !== null && $event instanceof AfterPageEvent) {
$stats[] = ['Estimated time remaining' => Helper::formatTime($eta)];
if ($eta === null) {
$stats[] = ['Time elapsed' => Helper::formatTime($processDuration)];
} else {
$stats[] = ['Time elapsed - remaining' => sprintf(
'%s - %s',
Helper::formatTime($processDuration),
Helper::formatTime($eta)
)];
}
}

$stats = [
...$stats,
['Page processed' => $this->pageNumber],
['Item processed' => $this->itemNumber],
['Memory usage' => Helper::formatMemory(memory_get_usage(true))],
];
if ($event instanceof BeforeProcessEvent) {
$pagesInfo = $this->totalPages ?? '(unknown)';
$itemsInfo = $this->totalItems ?? '(unknown)';
} elseif ($event instanceof AfterProcessEvent) {
$pagesInfo = $this->pageNumber;
$itemsInfo = $this->itemNumber;
} elseif ($this->totalPages === null) {
$pagesInfo = $this->pageNumber;
$itemsInfo = $this->itemNumber;
} else {
$pagesInfo = sprintf(
'%s/%s',
$this->pageNumber,
$this->totalPages
);

if ($processDuration !== null) {
$stats[] = ['Pages/minute' => round($pagesPerSecond * 60, 2)];
$stats[] = ['Items/minute' => round($itemsPerSecond * 60, 2)];
$itemsInfo = sprintf(
'%s/%s',
$this->itemNumber,
$this->totalItems ?? '?'
);
}

if ($pagesPerSecond > 0) {
$pagesInfo .= sprintf(' (%s/minute)', round($pagesPerSecond * 60, 2));
}

if ($itemsPerSecond > 0) {
$itemsInfo .= sprintf(' (%s/minute)', round($itemsPerSecond * 60, 2));
}

$stats[] = ['Pages' => $pagesInfo];
$stats[] = ['Items' => $itemsInfo];


$stats[] = ['Memory (current/peak)' => sprintf(
'%s / %s',
Helper::formatMemory(memory_get_usage(true)),
Helper::formatMemory(memory_get_peak_usage(true))
)];

$this->io->definitionList(...$stats);
}
}

0 comments on commit 38a59db

Please sign in to comment.