Edit File by line
/home/zeestwma/ajeebong.../wp-conte.../plugins/image-op.../modules/optimiza.../componen...
File: retry.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace ImageOptimization\Modules\Optimization\Components;
[2] Fix | Delete
[3] Fix | Delete
use ImageOptimization\Classes\Async_Operation\{
[4] Fix | Delete
Async_Operation,
[5] Fix | Delete
Async_Operation_Queue,
[6] Fix | Delete
Exceptions\Async_Operation_Exception,
[7] Fix | Delete
Queries\Image_Optimization_Operation_Query,
[8] Fix | Delete
};
[9] Fix | Delete
use ImageOptimization\Classes\Image\{
[10] Fix | Delete
Image_Meta,
[11] Fix | Delete
Image_Optimization_Error_Type,
[12] Fix | Delete
Image_Status,
[13] Fix | Delete
};
[14] Fix | Delete
[15] Fix | Delete
use ImageOptimization\Classes\Logger;
[16] Fix | Delete
[17] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[18] Fix | Delete
exit; // Exit if accessed directly.
[19] Fix | Delete
}
[20] Fix | Delete
[21] Fix | Delete
class Retry {
[22] Fix | Delete
const RETRY_LIMIT = 3;
[23] Fix | Delete
const ONE_MINUTE_IN_SECONDS = 60;
[24] Fix | Delete
[25] Fix | Delete
public static function maybe_retry_optimization( int $image_id ) {
[26] Fix | Delete
$query = ( new Image_Optimization_Operation_Query() )
[27] Fix | Delete
->set_image_id( $image_id )
[28] Fix | Delete
->set_status( Async_Operation::OPERATION_STATUS_RUNNING )
[29] Fix | Delete
->set_limit( 1 );
[30] Fix | Delete
[31] Fix | Delete
try {
[32] Fix | Delete
[ $action ] = Async_Operation::get( $query );
[33] Fix | Delete
[34] Fix | Delete
if ( ! $action ) {
[35] Fix | Delete
Logger::info( 'Could not find an action to retry for ' . $image_id );
[36] Fix | Delete
[37] Fix | Delete
( new Image_Meta( $image_id ) )
[38] Fix | Delete
->set_status( Image_Status::OPTIMIZATION_FAILED )
[39] Fix | Delete
->set_error_type( Image_Optimization_Error_Type::GENERIC )
[40] Fix | Delete
->save();
[41] Fix | Delete
[42] Fix | Delete
return;
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
// Unlikely timeout to separate force retries from the real timeout issues
[46] Fix | Delete
do_action( 'action_scheduler_failed_action', $action->get_id(), 1337 );
[47] Fix | Delete
} catch ( Async_Operation_Exception $aoe ) {
[48] Fix | Delete
Logger::error( "Failed to retry an optimization operation for `$image_id`: " . $aoe->getMessage() );
[49] Fix | Delete
}
[50] Fix | Delete
}
[51] Fix | Delete
[52] Fix | Delete
public function retry_failed_optimizations( $action_id ) {
[53] Fix | Delete
$action = Async_Operation::get_by_id( (int) $action_id );
[54] Fix | Delete
[55] Fix | Delete
// Ensure it's the optimization action we want to reschedule
[56] Fix | Delete
if ( Async_Operation_Queue::OPTIMIZE !== $action->get_queue() ) {
[57] Fix | Delete
return;
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
Logger::info( "Optimization {$action_id} failed" );
[61] Fix | Delete
[62] Fix | Delete
$attachment_id = isset( $action->get_args()['attachment_id'] ) ? (int) $action->get_args()['attachment_id'] : null;
[63] Fix | Delete
[64] Fix | Delete
if ( ! $attachment_id ) {
[65] Fix | Delete
Logger::info( "Missing image ID in failed action {$action_id}" );
[66] Fix | Delete
[67] Fix | Delete
return;
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
$image_meta = new Image_Meta( $attachment_id );
[71] Fix | Delete
$retry_count = $image_meta->get_retry_count() ?? 0;
[72] Fix | Delete
[73] Fix | Delete
if ( $retry_count < self::RETRY_LIMIT ) {
[74] Fix | Delete
$image_meta
[75] Fix | Delete
->set_retry_count( $retry_count + 1 )
[76] Fix | Delete
->save();
[77] Fix | Delete
[78] Fix | Delete
try {
[79] Fix | Delete
Async_Operation::create(
[80] Fix | Delete
$action->get_hook(),
[81] Fix | Delete
$action->get_args(),
[82] Fix | Delete
$action->get_queue(),
[83] Fix | Delete
time() + ( $retry_count * self::ONE_MINUTE_IN_SECONDS ),
[84] Fix | Delete
);
[85] Fix | Delete
[86] Fix | Delete
Logger::debug(
[87] Fix | Delete
"Rescheduled image optimization for image ID {$attachment_id}. Retry attempt: " . ( $retry_count + 1 )
[88] Fix | Delete
);
[89] Fix | Delete
} catch ( Async_Operation_Exception $aoe ) {
[90] Fix | Delete
Logger::debug(
[91] Fix | Delete
"Failed to reschedule optimization for image ID {$attachment_id}: " . $aoe->getMessage()
[92] Fix | Delete
);
[93] Fix | Delete
}
[94] Fix | Delete
} else {
[95] Fix | Delete
$image_meta
[96] Fix | Delete
->set_status( Image_Status::OPTIMIZATION_FAILED )
[97] Fix | Delete
->set_retry_count( null )
[98] Fix | Delete
->save();
[99] Fix | Delete
[100] Fix | Delete
Logger::debug( "Image optimization for image ID {$attachment_id} exceeded retry limit." );
[101] Fix | Delete
}
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
public function __construct() {
[105] Fix | Delete
add_action( 'action_scheduler_failed_action', [ $this, 'retry_failed_optimizations' ], 10, 2 );
[106] Fix | Delete
}
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function