$row Request row. * @param int $now Current Unix time. */ public static function due( array $row, int $now ): bool { if ( Status::QUEUED !== ( $row['status'] ?? '' ) ) { return false; } return $now - LeasePolicy::timestamp( $row['created_at'] ?? null, $now ) >= self::GRACE; } /** * Whether running in web requests is allowed on this site. */ public static function enabled(): bool { /** * Filters whether try-ons may run inside web requests when no background worker picks * them up. Return false on sites with a reliable server cron. * * @since 1.0.0 * * @param bool $enabled Default true. */ return false !== apply_filters( 'fitzaro/run_in_request', true ); } /** * Runs the request after the response is sent, where the server supports finishing early. * * @param string $id Request UUID. */ public static function after_response( string $id ): void { if ( ! self::enabled() || ( ! function_exists( 'fastcgi_finish_request' ) && ! function_exists( 'litespeed_finish_request' ) ) ) { return; } add_action( 'shutdown', static function () use ( $id ): void { if ( function_exists( 'fastcgi_finish_request' ) ) { fastcgi_finish_request(); } elseif ( function_exists( 'litespeed_finish_request' ) ) { litespeed_finish_request(); } ignore_user_abort( true ); Jobs::run( $id ); }, PHP_INT_MAX ); } /** * Runs the request now when it is still waiting for a worker. Blocks until the provider answers. * * @param string $id Request UUID (already checked to belong to the caller). */ public static function if_stalled( string $id ): void { if ( ! self::enabled() ) { return; } $row = Jobs::requests()->find( $id ); if ( null !== $row && self::due( $row, time() ) ) { Jobs::run( $id ); } } }