| 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 |
}; |
| 11 |
|
| 12 |
use ImageOptimization\Classes\Logger; |
| 13 |
|
| 14 |
use Exception; |
| 15 |
use Throwable; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; // Exit if accessed directly. |
| 19 |
} |
| 20 |
|
| 21 |
class Actions_Cleanup { |
| 22 |
const FIVE_MINUTES_IN_SECONDS = 300; |
| 23 |
|
| 24 |
/** |
| 25 |
* @async |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function cleanup_stuck_operations() { |
| 29 |
global $wpdb; |
| 30 |
|
| 31 |
$table_name = $wpdb->prefix . 'actionscheduler_actions'; |
| 32 |
$now = time(); |
| 33 |
$threshold = $now - self::FIVE_MINUTES_IN_SECONDS; |
| 34 |
|
| 35 |
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 36 |
$results = $wpdb->get_col( |
| 37 |
$wpdb->prepare( |
| 38 |
" |
| 39 |
SELECT action_id |
| 40 |
FROM {$table_name} |
| 41 |
WHERE last_attempt_gmt IS NOT NULL |
| 42 |
AND UNIX_TIMESTAMP(last_attempt_gmt) < %d |
| 43 |
", |
| 44 |
$threshold |
| 45 |
) |
| 46 |
); |
| 47 |
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 48 |
|
| 49 |
if ( empty( $results ) ) { |
| 50 |
Logger::log( Logger::LEVEL_INFO, 'No stuck optimization operations found for cleanup.' ); |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
foreach ( $results as $action_id ) { |
| 55 |
$action = Async_Operation::get_by_id( (int) $action_id ); |
| 56 |
|
| 57 |
if ( |
| 58 |
! $action || |
| 59 |
Async_Operation_Queue::OPTIMIZE !== $action->get_queue() || |
| 60 |
Async_Operation::OPERATION_STATUS_RUNNING !== $action->get_status() |
| 61 |
) { |
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
try { |
| 66 |
do_action( |
| 67 |
'action_scheduler_failed_action', |
| 68 |
$action_id, |
| 69 |
self::FIVE_MINUTES_IN_SECONDS |
| 70 |
); |
| 71 |
|
| 72 |
Logger::log( |
| 73 |
Logger::LEVEL_INFO, |
| 74 |
"Triggered retry for stuck action ID {$action_id}." |
| 75 |
); |
| 76 |
} catch ( Throwable $t ) { |
| 77 |
Logger::log( |
| 78 |
Logger::LEVEL_ERROR, |
| 79 |
"Failed to handle stuck operation for action ID {$action_id}: " . $t->getMessage() |
| 80 |
); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
public function schedule_cleanup() { |
| 86 |
try { |
| 87 |
Async_Operation::create_recurring( |
| 88 |
time(), |
| 89 |
self::FIVE_MINUTES_IN_SECONDS, |
| 90 |
Async_Operation_Hook::STUCK_OPERATION_CLEANUP, |
| 91 |
[], |
| 92 |
Async_Operation_Queue::CLEANUP, |
| 93 |
10, |
| 94 |
true |
| 95 |
); |
| 96 |
} catch ( Async_Operation_Exception $aoe ) { |
| 97 |
Logger::log( |
| 98 |
Logger::LEVEL_ERROR, |
| 99 |
'Failed to schedule recurring stuck operation cleanup: ' . $aoe->getMessage() |
| 100 |
); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public function __construct() { |
| 105 |
add_action( 'action_scheduler_init', [ $this, 'schedule_cleanup' ] ); |
| 106 |
add_action( Async_Operation_Hook::STUCK_OPERATION_CLEANUP, [ $this, 'cleanup_stuck_operations' ] ); |
| 107 |
} |
| 108 |
} |
| 109 |
|