@@ -210,7 +210,7 @@ public function execute( array $input ): array {
210210 $ job_id = (int ) $ job ->job_id ;
211211 $ job_flow_id = (int ) $ job ->flow_id ;
212212
213- if ( $ this ->hasActiveStepAction ( $ job_id ) ) {
213+ if ( $ this ->hasActiveStepAction ( $ job_id, $ timeout_hours ) ) {
214214 ++$ skipped ;
215215 $ jobs [] = array (
216216 'job_id ' => $ job_id ,
@@ -444,9 +444,10 @@ private function getTerminalBackedInProgressActions( ?int $flow_id ): array {
444444 * original job row is old.
445445 *
446446 * @param int $job_id Job ID.
447- * @return bool True when a pending/in-progress step action exists.
447+ * @param int $timeout_hours Hours before in-progress actions are considered stale.
448+ * @return bool True when a pending or fresh in-progress step action exists.
448449 */
449- private function hasActiveStepAction ( int $ job_id ): bool {
450+ private function hasActiveStepAction ( int $ job_id, int $ timeout_hours ): bool {
450451 global $ wpdb ;
451452
452453 if ( $ job_id <= 0 ) {
@@ -459,7 +460,7 @@ private function hasActiveStepAction( int $job_id ): bool {
459460 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is generated from the WP prefix.
460461 $ actions = $ wpdb ->get_results (
461462 $ wpdb ->prepare (
462- "SELECT action_id, args
463+ "SELECT action_id, args, status, scheduled_date_gmt, last_attempt_gmt
463464 FROM {$ actions_table }
464465 WHERE hook = %s
465466 AND status IN ( %s, %s )
@@ -472,9 +473,27 @@ private function hasActiveStepAction( int $job_id ): bool {
472473 );
473474 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
474475
476+ $ timeout_seconds = max ( 1 , $ timeout_hours ) * HOUR_IN_SECONDS ;
477+ $ now_gmt = strtotime ( current_time ( 'mysql ' , true ) );
478+
475479 foreach ( $ actions as $ action ) {
476480 if ( $ job_id === $ this ->extractActionJobId ( (string ) ( $ action ->args ?? '' ) ) ) {
477- return true ;
481+ if ( 'pending ' === (string ) $ action ->status ) {
482+ return true ;
483+ }
484+
485+ $ last_attempt = (string ) ( $ action ->last_attempt_gmt ?? '' );
486+ $ scheduled = (string ) ( $ action ->scheduled_date_gmt ?? '' );
487+ $ reference = $ last_attempt && '0000-00-00 00:00:00 ' !== $ last_attempt ? $ last_attempt : $ scheduled ;
488+ $ started_at = $ reference ? strtotime ( $ reference ) : false ;
489+
490+ if ( false === $ started_at || false === $ now_gmt ) {
491+ return true ;
492+ }
493+
494+ if ( ( $ now_gmt - $ started_at ) < $ timeout_seconds ) {
495+ return true ;
496+ }
478497 }
479498 }
480499
0 commit comments