PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.4
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.4
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / modules / optimization / components / retry.php

retry.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.4, at modules/optimization/components/retry.php

122 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimization\Modules\Optimization\Components;
4
5 use ImageOptimization\Classes\Async_Operation\{
6 Async_Operation,
7 Async_Operation_Hook,
8 Async_Operation_Queue,
9 Exceptions\Async_Operation_Exception,
10 Queries\Image_Optimization_Operation_Query,
11 };
12 use ImageOptimization\Classes\Image\{
13 Image_Meta,
14 Image_Optimization_Error_Type,
15 Image_Status,
16 };
17
18 use ImageOptimization\Classes\Logger;
19
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit; // Exit if accessed directly.
22 }
23
24 class Retry {
25 const RETRY_LIMIT = 3;
26 const ONE_MINUTE_IN_SECONDS = 60;
27
28 public static function maybe_retry_optimization( int $image_id ) {
29 $query = ( new Image_Optimization_Operation_Query() )
30 ->set_image_id( $image_id )
31 ->set_status( Async_Operation::OPERATION_STATUS_RUNNING )
32 ->set_limit( 1 );
33
34 try {
35 [ $action ] = Async_Operation::get( $query );
36
37 if ( ! $action ) {
38 Logger::info( 'Could not find an action to retry for ' . $image_id );
39
40 ( new Image_Meta( $image_id ) )
41 ->set_status( Image_Status::OPTIMIZATION_FAILED )
42 ->set_error_type( Image_Optimization_Error_Type::GENERIC )
43 ->save();
44
45 return;
46 }
47
48 // Unlikely timeout to separate force retries from the real timeout issues
49 do_action( 'action_scheduler_failed_action', $action->get_id(), 1337 );
50 } catch ( Async_Operation_Exception $aoe ) {
51 Logger::error( "Failed to retry an optimization operation for `$image_id`: " . $aoe->getMessage() );
52 }
53 }
54
55 public function retry_failed_optimizations( $action_id ) {
56 $action = Async_Operation::get_by_id( (int) $action_id );
57
58 // Ensure it's the optimization action we want to reschedule
59 if ( Async_Operation_Queue::OPTIMIZE !== $action->get_queue() ) {
60 return;
61 }
62
63 // Skip bulk operations
64 $hook = $action->get_hook();
65
66 if ( in_array( $hook, [
67 Async_Operation_Hook::OPTIMIZE_BULK,
68 Async_Operation_Hook::REOPTIMIZE_BULK,
69 ], true ) ) {
70 Logger::info( "Skipping retry for bulk operation {$action_id}" );
71 return;
72 }
73
74 Logger::info( "Optimization {$action_id} failed" );
75
76 $attachment_id = isset( $action->get_args()['attachment_id'] ) ? (int) $action->get_args()['attachment_id'] : null;
77
78 if ( ! $attachment_id ) {
79 Logger::info( "Missing image ID in failed action {$action_id}" );
80
81 return;
82 }
83
84 $image_meta = new Image_Meta( $attachment_id );
85 $retry_count = $image_meta->get_retry_count() ?? 0;
86
87 if ( $retry_count < self::RETRY_LIMIT ) {
88 $image_meta
89 ->set_retry_count( $retry_count + 1 )
90 ->save();
91
92 try {
93 Async_Operation::create(
94 $action->get_hook(),
95 $action->get_args(),
96 $action->get_queue(),
97 time() + ( $retry_count * self::ONE_MINUTE_IN_SECONDS ),
98 );
99
100 Logger::debug(
101 "Rescheduled image optimization for image ID {$attachment_id}. Retry attempt: " . ( $retry_count + 1 )
102 );
103 } catch ( Async_Operation_Exception $aoe ) {
104 Logger::debug(
105 "Failed to reschedule optimization for image ID {$attachment_id}: " . $aoe->getMessage()
106 );
107 }
108 } else {
109 $image_meta
110 ->set_status( Image_Status::OPTIMIZATION_FAILED )
111 ->set_retry_count( null )
112 ->save();
113
114 Logger::debug( "Image optimization for image ID {$attachment_id} exceeded retry limit." );
115 }
116 }
117
118 public function __construct() {
119 add_action( 'action_scheduler_failed_action', [ $this, 'retry_failed_optimizations' ], 10, 2 );
120 }
121 }
122