Repository navigation
Report terminal program status (OSC 7501) for queue:consume #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
samdark
wants to merge
11
commits into
master
Choose a base branch
from
osc-7501-514
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9bf1576
Report terminal program status (OSC 7501) for queue:consume
samdark 301f214
Address review findings for terminal status reports
samdark 0ba848f
Add CHANGELOG entry for terminal status reports
samdark e739641
Apply PHP CS Fixer and Rector changes (CI)
github-actions[bot] 9e21092
Fix numeric queue names in providers
samdark a1a23cb
Add isSynchronous() to the consumer interface
samdark 280fc8b
Terminal status reporter: dual env opt-out and UTF-8 hardening
samdark bad0e3d
Rework queue:consume terminal status reporting
samdark f15cc8e
Add regression tests for terminal status behavior
samdark 359ddd9
Fix never-returning test callbacks for PHP 8.1
samdark 693273f
Reconcile queue terminal reporting and preserve numeric discovery
samdark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,139 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Queue\Cli; | ||
|
|
||
| use InvalidArgumentException; | ||
|
|
||
| use function base64_encode; | ||
| use function implode; | ||
| use function in_array; | ||
| use function preg_match; | ||
| use function preg_replace; | ||
| use function sprintf; | ||
| use function strlen; | ||
| use function substr; | ||
| use function trim; | ||
|
|
||
| /** @internal Encodes OSC 7501 without depending on a console framework or output stream. */ | ||
| final class Osc7501Encoder | ||
| { | ||
| private const MAX_MESSAGE_BYTES = 2048; | ||
| private const MAX_ID_BYTES = 128; | ||
| private const ID_PATTERN = '/^[A-Za-z0-9_.+-]{1,32}(?:\/[A-Za-z0-9_.+-]{1,32}){0,7}$/D'; | ||
| private const APP_PATTERN = '/^[A-Za-z0-9_.+-]{1,32}$/D'; | ||
| private const STATES = ['idle', 'working', 'done', 'error', 'blocked']; | ||
| private const KINDS = ['permission', 'question', 'auth']; | ||
|
|
||
| public static function encode( | ||
| string $state, | ||
| string $id, | ||
| string $app, | ||
| ?string $message = null, | ||
| ?int $progress = null, | ||
| ?string $kind = null, | ||
| ): string { | ||
| self::validateId($id); | ||
| if (!preg_match(self::APP_PATTERN, $app)) { | ||
| throw new InvalidArgumentException('The terminal status app must be 1 to 32 ASCII identifier characters.'); | ||
| } | ||
| if ($state === 'clear') { | ||
| return "\x1b]7501;state=clear:id={$id}\x1b\\"; | ||
| } | ||
|
|
||
| if (!in_array($state, self::STATES, true)) { | ||
| throw new InvalidArgumentException( | ||
| sprintf( | ||
| 'The terminal status state "%s" must be one of: %s.', | ||
| $state, | ||
| implode(', ', self::STATES), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| if ($kind !== null) { | ||
| if ($state !== 'blocked') { | ||
| throw new InvalidArgumentException( | ||
| sprintf( | ||
| 'A terminal status kind is only supported for the "blocked" state, "%s" given.', | ||
| $state, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| if (!in_array($kind, self::KINDS, true)) { | ||
| throw new InvalidArgumentException( | ||
| sprintf( | ||
| 'The terminal status kind "%s" must be one of: %s.', | ||
| $kind, | ||
| implode(', ', self::KINDS), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if ($progress !== null) { | ||
| if ($state !== 'working' && $state !== 'blocked') { | ||
| throw new InvalidArgumentException( | ||
| sprintf( | ||
| 'A terminal status progress is only supported for the "working" and "blocked" states, "%s" given.', | ||
| $state, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| if ($progress < 0 || $progress > 100) { | ||
|
Check warning on line 86 in src/Cli/Osc7501Encoder.php
|
||
| throw new InvalidArgumentException( | ||
| sprintf('The terminal status progress must be between 0 and 100, %d given.', $progress), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| $pairs = ['state=' . $state, 'id=' . $id]; | ||
| if ($kind !== null) { | ||
| $pairs[] = 'kind=' . $kind; | ||
| } | ||
| if ($progress !== null) { | ||
| $pairs[] = 'progress=' . $progress; | ||
| } | ||
| $pairs[] = 'app=' . $app; | ||
|
|
||
| $message = self::normalizeText($message); | ||
| if ($message !== null) { | ||
| $pairs[] = 'msg=' . base64_encode($message); | ||
| } | ||
|
|
||
| return "\x1b]7501;" . implode(':', $pairs) . "\x1b\\"; | ||
| } | ||
|
|
||
| private static function validateId(string $id): void | ||
| { | ||
| if (strlen($id) > self::MAX_ID_BYTES || !preg_match(self::ID_PATTERN, $id)) { | ||
| throw new InvalidArgumentException( | ||
| sprintf( | ||
| 'The terminal status id "%s" must be up to 8 "/"-separated segments of 1 to 32 characters ' | ||
| . 'among letters, digits, "_", ".", "+" and "-", and at most %d bytes.', | ||
| $id, | ||
| self::MAX_ID_BYTES, | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private static function normalizeText(?string $text): ?string | ||
| { | ||
| if ($text === null || !preg_match('//u', $text)) { | ||
| return null; | ||
| } | ||
|
|
||
| // Bound work before sanitizing. Remove at most three bytes from a split UTF-8 character. | ||
| $text = substr($text, 0, self::MAX_MESSAGE_BYTES); | ||
| while ($text !== '' && !preg_match('//u', $text)) { | ||
| $text = substr($text, 0, -1); | ||
| } | ||
| $text = trim((string) preg_replace('/[\x00-\x1F\x7F\x{80}-\x{9F}\x{2028}\x{2029}]+/u', ' ', $text)); | ||
|
|
||
| return $text !== '' ? $text : null; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,49 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Queue\Cli; | ||
|
|
||
| use Exception; | ||
| use Symfony\Component\Console\Output\ConsoleOutputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Output\StreamOutput; | ||
|
|
||
| use function bin2hex; | ||
| use function getenv; | ||
| use function in_array; | ||
| use function is_resource; | ||
| use function random_bytes; | ||
| use function stream_isatty; | ||
| use function strtolower; | ||
| use function trim; | ||
|
|
||
| /** @internal Selects a positively verified terminal output for queue status reports. */ | ||
| final class TerminalStatusReporter | ||
| { | ||
| public static function forOutput(OutputInterface $output): TerminalStatusWriter | ||
| { | ||
| if ($output instanceof ConsoleOutputInterface) { | ||
| $output = $output->getErrorOutput(); | ||
| } | ||
|
|
||
| $stream = $output instanceof StreamOutput ? $output->getStream() : null; | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Resolved
|
||
| $setting = getenv('YII_TERMINAL_STATUS'); | ||
| $enabled = !in_array(strtolower(trim($setting === false ? '' : $setting)), ['off', '0', 'no', 'false'], true) | ||
| && $output->isDecorated() | ||
| && !$output->isQuiet() | ||
| && is_resource($stream) | ||
| && stream_isatty($stream); | ||
|
|
||
| $id = 'queue/disabled'; | ||
| if ($enabled) { | ||
| try { | ||
| $id = 'queue/' . bin2hex(random_bytes(8)); | ||
| } catch (Exception) { | ||
| $enabled = false; | ||
| } | ||
| } | ||
|
|
||
| return new TerminalStatusWriter($stream, $enabled, $id); | ||
| } | ||
| } | ||
This file contains hidden or 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,76 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Queue\Cli; | ||
|
|
||
| use Throwable; | ||
|
|
||
| use function fflush; | ||
| use function fwrite; | ||
| use function is_resource; | ||
| use function strlen; | ||
|
|
||
| /** @internal Writes reports within one invocation's record subtree. */ | ||
| final class TerminalStatusWriter | ||
| { | ||
| /** @param resource|null $stream An explicitly selected output stream; it is never closed by this writer. */ | ||
| public function __construct( | ||
| private $stream, | ||
| private bool $enabled, | ||
| private readonly string $id, | ||
| private readonly string $app = 'queue', | ||
| ) { | ||
| Osc7501Encoder::encode('clear', $id, $app); | ||
| $this->enabled = $enabled && is_resource($stream); | ||
| } | ||
|
|
||
| public function emit( | ||
| string $state, | ||
| ?string $message = null, | ||
| ?string $id = null, | ||
| ?int $progress = null, | ||
| ?string $kind = null, | ||
| ): void { | ||
| if (!$this->isEnabled()) { | ||
| return; | ||
| } | ||
|
|
||
| $this->write(Osc7501Encoder::encode( | ||
| $state, | ||
| $id === null ? $this->id : $this->id . '/' . $id, | ||
| $this->app, | ||
| $message, | ||
| $progress, | ||
| $kind, | ||
| )); | ||
| } | ||
|
|
||
| /** Clears only this invocation's subtree, or a child beneath it. Never clears the terminal root. */ | ||
| public function clear(?string $id = null): void | ||
| { | ||
| $this->emit('clear', id: $id); | ||
| } | ||
|
|
||
| public function isEnabled(): bool | ||
| { | ||
| return $this->enabled && is_resource($this->stream); | ||
| } | ||
|
|
||
| private function write(string $sequence): void | ||
| { | ||
| $stream = $this->stream; | ||
| if (!is_resource($stream)) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| // Terminal status is optional. A closed or failed sink must not change the work's outcome. | ||
| if (@fwrite($stream, $sequence) !== strlen($sequence) || !@fflush($stream)) { | ||
| $this->enabled = false; | ||
| } | ||
| } catch (Throwable) { | ||
| $this->enabled = false; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.