$url ) { $args = array( $jobId, $row, $offset, $productId, $slot, (string) $url, 1 ); if ( self::queued( $args ) ) { continue; } if ( self::add( $args ) ) { ++$n; } else { // Not queued: leave a warning so that "retry images" can queue it later. JobRowsRepo::record( $jobId, $row, $offset, 'warning', 'image_failed', 'images:' . $slot . ' not queued', $productId, 'images:' . $slot ); } } if ( $n > 0 ) { JobRowsRepo::bump( $jobId, 'images_queued', $n ); } return $n; } /** * End the downloads still waiting when the plugin is deactivated: nothing would run them (Action Scheduler fails an * action that has no callback, without telling the job), so each one gets its warning and stops counting as open; * "retry images" queues them again. * * @return int Downloads ended. */ public static function abandonPending(): int { if ( ! function_exists( 'as_get_scheduled_actions' ) || ! class_exists( 'ActionScheduler' ) ) { return 0; } $n = 0; $seen = array(); do { $page = as_get_scheduled_actions( array( 'hook' => self::HOOK, 'group' => self::GROUP, 'status' => 'pending', // ActionScheduler_Store::STATUS_PENDING. 'per_page' => 100, 'orderby' => 'action_id', // Queue order; the default (date) ties within a second. 'order' => 'ASC', ), 'ids' ); $ids = array_diff( array_map( 'intval', (array) $page ), $seen ); foreach ( $ids as $id ) { $seen[] = $id; $args = array_values( (array) \ActionScheduler::store()->fetch_action( $id )->get_args() ); \ActionScheduler::store()->cancel_action( $id ); if ( count( $args ) >= 5 ) { ImageAction::giveUp( (int) $args[0], (int) $args[1], (int) $args[2], (int) $args[3], (int) $args[4], 'image_failed', 'not run' ); ++$n; } } } while ( $ids ); return $n; } /** * Schedule a later attempt. * * @param array $args Action args of the failed attempt. * @param int $delay_s Delay in seconds. * @return bool Whether the next attempt is scheduled. */ public static function retryLater( array $args, int $delay_s ): bool { $args[6] = (int) $args[6] + 1; return self::queued( $args ) || self::add( $args, time() + $delay_s ); } /** * Whether this attempt is already pending or running. * * @param array $args Action args. * @return bool */ public static function queued( array $args ): bool { return function_exists( 'as_has_scheduled_action' ) && as_has_scheduled_action( self::HOOK, $args, self::GROUP ); } /** * Add a download action (callers check queued() first). Action Scheduler's $unique is not used: before 4.0 it * ignores the args, so any other pending or running image action of the site would drop this one. * * @param array $args Action args. * @param int $at Unix time, 0 = as soon as possible. * @return bool Whether an action was added. */ public static function add( array $args, int $at = 0 ): bool { try { if ( $at > 0 ) { $id = function_exists( 'as_schedule_single_action' ) ? as_schedule_single_action( $at, self::HOOK, $args, self::GROUP, false ) : 0; } else { $id = function_exists( 'as_enqueue_async_action' ) ? as_enqueue_async_action( self::HOOK, $args, self::GROUP, false ) : 0; } } catch ( \Throwable $e ) { $id = 0; // Action Scheduler throws when it cannot store a non-unique action. } return (int) $id > 0; } /** * Open downloads of a job. * * @param int $jobId Job. * @return int */ public static function pending( int $jobId ): int { return max( 0, JobRowsRepo::counter( $jobId, 'images_queued' ) - JobRowsRepo::counter( $jobId, 'images_done' ) ); } }