PluginProbe
Image Optimization – Compress Images and Convert to WebP or AVIF / 1.6.6
Image Optimization – Compress Images and Convert to WebP or AVIF v1.6.6
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 / actions-cleanup.php

actions-cleanup.php in Image Optimization – Compress Images and Convert to WebP or AVIF 1.6.6, at modules/optimization/components/actions-cleanup.php

109 lines 2.4 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 };
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