PluginProbe
Image Optimization – Compress Images and Convert to WebP or AVIF / 1.6.8
Image Optimization – Compress Images and Convert to WebP or AVIF v1.6.8
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 1.6.6 All 32 releases
image-optimization / modules / optimization / components / retry.php

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

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