@@ -666,22 +666,59 @@ private function control_cleanup_run_job( string $operation, int $job_id, array
666666 return ;
667667 }
668668
669- $ input = array ( 'job_id ' => $ job_id );
670- if ( 'resume ' === $ operation ) {
671- $ input ['force ' ] = ! empty ( $ assoc_args ['force ' ] );
672- } else {
673- $ input ['reason ' ] = 'cleanup_cancelled ' ;
669+ $ target_job_ids = $ this ->cleanup_run_control_job_ids ( $ operation , $ job_id );
670+ $ results = array ();
671+ foreach ( $ target_job_ids as $ target_job_id ) {
672+ $ input = array ( 'job_id ' => $ target_job_id );
673+ if ( 'resume ' === $ operation ) {
674+ $ input ['force ' ] = ! empty ( $ assoc_args ['force ' ] );
675+ } else {
676+ $ input ['reason ' ] = 'cleanup_cancelled ' ;
677+ }
678+
679+ $ result = $ ability ->execute ( $ input );
680+ if ( ! ( $ result ['success ' ] ?? false ) ) {
681+ WP_CLI ::error ( (string ) ( $ result ['error ' ] ?? 'Cleanup run control failed. ' ) );
682+ return ;
683+ }
684+ $ results [] = $ result ;
674685 }
675686
676- $ result = $ ability ->execute ( $ input );
677- if ( ! ( $ result ['success ' ] ?? false ) ) {
678- WP_CLI ::error ( (string ) ( $ result ['error ' ] ?? 'Cleanup run control failed. ' ) );
679- return ;
687+ $ output = $ results [0 ] ?? array (
688+ 'success ' => true ,
689+ 'job_id ' => $ job_id ,
690+ );
691+ $ output ['run_id ' ] = $ this ->cleanup_run_id ( $ job_id );
692+ $ output ['state ' ] = 'resume ' === $ operation ? 'running ' : 'cancelled ' ;
693+ $ output ['controlled_job_ids ' ] = $ target_job_ids ;
694+ $ output ['results ' ] = $ results ;
695+ $ this ->render_cleanup_control_result ( $ output , $ assoc_args );
696+ }
697+
698+ /**
699+ * Resolve which Data Machine jobs should be controlled for a job-backed cleanup run.
700+ *
701+ * @param string $operation Cleanup control operation.
702+ * @param int $job_id Cleanup parent job ID.
703+ * @return array<int,int>
704+ */
705+ private function cleanup_run_control_job_ids ( string $ operation , int $ job_id ): array {
706+ $ output = $ this ->cleanup_run_evidence_store ()->read ( $ this ->cleanup_run_id ( $ job_id ), true , true );
707+ if ( $ output instanceof \WP_Error ) {
708+ return array ( $ job_id );
680709 }
681710
682- $ result ['run_id ' ] = $ this ->cleanup_run_id ( $ job_id );
683- $ result ['state ' ] = 'resume ' === $ operation ? 'running ' : 'cancelled ' ;
684- $ this ->render_cleanup_control_result ( $ result , $ assoc_args );
711+ $ children = (array ) ( $ output ['evidence ' ]['children ' ] ?? array () );
712+ $ processing_ids = array_map ( 'intval ' , (array ) ( $ children ['processing_job_ids ' ] ?? array () ) );
713+ $ failed_ids = array_map ( 'intval ' , (array ) ( $ children ['failed_job_ids ' ] ?? array () ) );
714+ $ pending_ids = array_map ( 'intval ' , (array ) ( $ children ['pending_job_ids ' ] ?? array () ) );
715+
716+ if ( 'resume ' === $ operation ) {
717+ $ child_targets = array_values ( array_unique ( array_filter ( array_merge ( $ processing_ids , $ failed_ids ) ) ) );
718+ return array () !== $ child_targets ? $ child_targets : array ( $ job_id );
719+ }
720+
721+ return array_values ( array_unique ( array_filter ( array_merge ( array ( $ job_id ), $ pending_ids , $ processing_ids ) ) ) );
685722 }
686723
687724 private function render_cleanup_control_result ( array $ result , array $ assoc_args ): void {
0 commit comments