Skip to content

Commit bcd8aa3

Browse files
authored
Merge pull request #690 from Extra-Chill/fix/issue-689-drain-json
Fix cleanup drain JSON run visibility
2 parents 19f9377 + 85288b2 commit bcd8aa3

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

inc/Cli/Commands/WorkspaceCommand.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,28 @@ private function run_cleanup_task( array $assoc_args ): void {
767767

768768
$result = $this->attach_cleanup_run_commands($result, $mode);
769769
if ( ! empty($assoc_args['drain']) ) {
770+
if ( 'json' === (string) ( $assoc_args['format'] ?? 'table' ) ) {
771+
$this->render_cleanup_control_result($result + array( 'drain_state' => 'scheduled' ), $assoc_args);
772+
$this->flush_cli_output();
773+
}
770774
$result = $this->drain_cleanup_run_to_status($result, $assoc_args);
771775
}
772776

773777
$this->render_cleanup_control_result($result, $assoc_args);
774778
}
775779

780+
/**
781+
* Flush stdout so JSON drain callers see the scheduled run before draining blocks.
782+
*
783+
* @return void
784+
*/
785+
private function flush_cli_output(): void {
786+
if ( defined('STDOUT') ) {
787+
fflush(STDOUT);
788+
}
789+
flush();
790+
}
791+
776792
/**
777793
* Attach operator commands to a queued cleanup run response.
778794
*

tests/smoke-worktree-cleanup-cli.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static function runcommand( string $command, array $options = array() ):
4444
{
4545
self::$runcommands[] = $command;
4646
if ('datamachine drain --job-id=123' === $command ) {
47+
$GLOBALS['datamachine_code_cleanup_parent_drain_saw_scheduled_json'] = json_decode(self::$logs[0] ?? '', true);
4748
$GLOBALS['datamachine_code_cleanup_parent_drained'] = true;
4849
}
4950
if ('datamachine drain --job-id=125' === $command ) {
@@ -1397,14 +1398,21 @@ public function execute( array $input ): array
13971398
WP_CLI::$runcommands = array();
13981399
$GLOBALS['datamachine_code_cleanup_parent_drained'] = false;
13991400
$GLOBALS['datamachine_code_cleanup_child_drained'] = false;
1401+
$GLOBALS['datamachine_code_cleanup_parent_drain_saw_scheduled_json'] = array();
14001402
$command->cleanup(array( 'run' ), array( 'mode' => 'artifacts', 'drain' => true, 'format' => 'json' ));
1401-
$drained_json = json_decode(WP_CLI::$logs[0] ?? '', true);
1403+
$scheduled_json = json_decode(WP_CLI::$logs[0] ?? '', true);
1404+
$drained_json = json_decode(end(WP_CLI::$logs) ?: '', true);
14021405
datamachine_code_cleanup_assert(array( 'datamachine drain --job-id=123', 'datamachine drain --job-id=125' ) === WP_CLI::$runcommands, 'cleanup run --drain drains parent then active child jobs');
1406+
datamachine_code_cleanup_assert('cleanup-run-123' === ( $scheduled_json['run_id'] ?? '' ), 'cleanup run --drain JSON emits scheduled run id before final status');
1407+
datamachine_code_cleanup_assert(123 === (int) ( $scheduled_json['job_id'] ?? 0 ), 'cleanup run --drain JSON emits scheduled job id before final status');
1408+
datamachine_code_cleanup_assert('scheduled' === ( $scheduled_json['drain_state'] ?? '' ), 'cleanup run --drain JSON marks the early scheduled event');
1409+
datamachine_code_cleanup_assert('cleanup-run-123' === ( $GLOBALS['datamachine_code_cleanup_parent_drain_saw_scheduled_json']['run_id'] ?? '' ), 'cleanup run --drain exposes run id before invoking parent drain');
14031410
datamachine_code_cleanup_assert(4096 === (int) ( $drained_json['drain']['bytes_reclaimed'] ?? 0 ), 'cleanup run --drain reports verified reclaimed bytes');
14041411
datamachine_code_cleanup_assert('failed' === (string) ( $drained_json['drain']['completion_state'] ?? '' ), 'cleanup run --drain reports final cleanup state');
14051412
datamachine_code_cleanup_assert('studio wp datamachine-code workspace cleanup status cleanup-run-123 --format=json' === (string) ( $drained_json['drain']['verify_command'] ?? '' ), 'cleanup run --drain emits one verification command');
14061413
$GLOBALS['datamachine_code_cleanup_parent_drained'] = false;
14071414
$GLOBALS['datamachine_code_cleanup_child_drained'] = false;
1415+
$GLOBALS['datamachine_code_cleanup_parent_drain_saw_scheduled_json'] = array();
14081416

14091417
WP_CLI::$logs = array();
14101418
WP_CLI::$successes = array();
@@ -1422,9 +1430,13 @@ public function execute( array $input ): array
14221430
WP_CLI::$runcommands = array();
14231431
$GLOBALS['datamachine_code_cleanup_parent_drained'] = false;
14241432
$GLOBALS['datamachine_code_cleanup_child_drained'] = false;
1433+
$GLOBALS['datamachine_code_cleanup_parent_drain_saw_scheduled_json'] = array();
14251434
$command->cleanup(array( 'run' ), array( 'mode' => 'retention', 'drain' => true, 'format' => 'json' ));
1426-
$no_work_drained_json = json_decode(WP_CLI::$logs[0] ?? '', true);
1435+
$no_work_scheduled_json = json_decode(WP_CLI::$logs[0] ?? '', true);
1436+
$no_work_drained_json = json_decode(end(WP_CLI::$logs) ?: '', true);
14271437
datamachine_code_cleanup_assert(array( 'datamachine drain --job-id=123' ) === WP_CLI::$runcommands, 'cleanup run --drain drains no-work parent once and does not invent child drains');
1438+
datamachine_code_cleanup_assert('cleanup-run-123' === ( $no_work_scheduled_json['run_id'] ?? '' ), 'cleanup run --drain no-work JSON emits scheduled run id before draining');
1439+
datamachine_code_cleanup_assert('cleanup-run-123' === ( $GLOBALS['datamachine_code_cleanup_parent_drain_saw_scheduled_json']['run_id'] ?? '' ), 'cleanup run --drain no-work exposes run id before invoking parent drain');
14281440
datamachine_code_cleanup_assert('no_work' === (string) ( $no_work_drained_json['drain']['completion_state'] ?? '' ), 'cleanup run --drain reports no_work instead of successful running state for no-child/no-work parents');
14291441
datamachine_code_cleanup_assert(true === (bool) ( $no_work_drained_json['drain']['success'] ?? false ), 'cleanup run --drain succeeds after no-work convergence');
14301442
$GLOBALS['datamachine_code_cleanup_status_scenario'] = 'default';

0 commit comments

Comments
 (0)