ActionScheduler.php
7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
namespace AIOSEO\Plugin\Common\Utils;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* This class makes sure the action scheduler tables always exist.
*
* @since 4.0.0
*/
class ActionScheduler {
/**
* The action scheduler group.
*
* @since 4.1.5
* @version 4.2.7
*
* @var string
*/
private $actionSchedulerGroup = 'aioseo';
/**
* Class Constructor.
*
* @since 4.0.0
*/
public function __construct() { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
add_action( 'action_scheduler_after_execute', [ $this, 'cleanup' ], 1000, 2 );
// Note: \ActionScheduler is first loaded on `plugins_loaded` action hook.
add_action( 'plugins_loaded', [ $this, 'maybeRecreateTables' ] );
}
/**
* Maybe register the `${table_prefix}_actionscheduler_${suffix}` tables with WordPress and create them if needed.
* Hooked into `plugins_loaded` action hook.
*
* @since 4.2.7
*
* @return void
*/
public function maybeRecreateTables() {
if ( ! is_admin() ) {
return;
}
if ( ! apply_filters( 'action_scheduler_enable_recreate_data_store', true ) ) {
return;
}
if (
! class_exists( 'ActionScheduler' ) ||
! class_exists( 'ActionScheduler_HybridStore' ) ||
! class_exists( 'ActionScheduler_StoreSchema' ) ||
! class_exists( 'ActionScheduler_LoggerSchema' )
) {
return;
}
$store = \ActionScheduler::store();
if ( ! is_a( $store, 'ActionScheduler_HybridStore' ) ) {
$store = new \ActionScheduler_HybridStore();
}
$tableList = [
'actionscheduler_actions',
'actionscheduler_logs',
'actionscheduler_groups',
'actionscheduler_claims',
];
foreach ( $tableList as $tableName ) {
if ( ! aioseo()->core->db->tableExists( $tableName ) ) {
add_action( 'action_scheduler/created_table', [ $store, 'set_autoincrement' ], 10, 2 );
$storeSchema = new \ActionScheduler_StoreSchema();
$loggerSchema = new \ActionScheduler_LoggerSchema();
$storeSchema->register_tables( true );
$loggerSchema->register_tables( true );
remove_action( 'action_scheduler/created_table', [ $store, 'set_autoincrement' ] );
break;
}
}
}
/**
* Cleans up the Action Scheduler tables after one of our actions completes.
* Hooked into `action_scheduler_after_execute` action hook.
*
* @since 4.0.10
*
* @param int $actionId The action ID processed.
* @param \ActionScheduler_Action $action Class instance.
* @return void
*/
public function cleanup( $actionId, $action ) {
if (
// Bail if this isn't one of our actions or if we're in a dev environment.
'aioseo' !== $action->get_group() ||
defined( 'AIOSEO_DEV_VERSION' ) ||
// Bail if the tables don't exist.
! aioseo()->core->db->tableExists( 'actionscheduler_actions' ) ||
! aioseo()->core->db->tableExists( 'actionscheduler_groups' )
) {
return;
}
$prefix = aioseo()->core->db->db->prefix;
// Clean up logs associated with entries in the actions table.
aioseo()->core->db->execute(
"DELETE al FROM {$prefix}actionscheduler_logs as al
JOIN {$prefix}actionscheduler_actions as aa on `aa`.`action_id` = `al`.`action_id`
JOIN {$prefix}actionscheduler_groups as ag on `ag`.`group_id` = `aa`.`group_id`
WHERE `ag`.`slug` = 'aioseo'
AND `aa`.`status` IN ('complete', 'failed', 'canceled');"
);
// Clean up actions.
aioseo()->core->db->execute(
"DELETE aa FROM {$prefix}actionscheduler_actions as aa
JOIN {$prefix}actionscheduler_groups as ag on `ag`.`group_id` = `aa`.`group_id`
WHERE `ag`.`slug` = 'aioseo'
AND `aa`.`status` IN ('complete', 'failed', 'canceled');"
);
// Clean up logs where there was no group.
aioseo()->core->db->execute(
"DELETE al FROM {$prefix}actionscheduler_logs as al
JOIN {$prefix}actionscheduler_actions as aa on `aa`.`action_id` = `al`.`action_id`
WHERE `aa`.`hook` LIKE 'aioseo_%'
AND `aa`.`group_id` = 0
AND `aa`.`status` IN ('complete', 'failed', 'canceled');"
);
// Clean up actions that start with aioseo_ and have no group.
aioseo()->core->db->execute(
"DELETE aa FROM {$prefix}actionscheduler_actions as aa
WHERE `aa`.`hook` LIKE 'aioseo_%'
AND `aa`.`group_id` = 0
AND `aa`.`status` IN ('complete', 'failed', 'canceled');"
);
}
/**
* Schedules a single action at a specific time in the future.
*
* @since 4.0.13
* @version 4.2.7
*
* @param string $actionName The action name.
* @param int $time The time to add to the current time.
* @param array $args Args passed down to the action.
* @param bool $forceSchedule Whether we should schedule a new action regardless of whether one is already set.
* @return boolean Whether the action was scheduled.
*/
public function scheduleSingle( $actionName, $time, $args = [], $forceSchedule = false ) {
try {
if ( $forceSchedule || ! $this->isScheduled( $actionName, $args ) ) {
as_schedule_single_action( time() + $time, $actionName, $args, $this->actionSchedulerGroup );
return true;
}
} catch ( \RuntimeException $e ) {
// Nothing needs to happen.
}
return false;
}
/**
* Checks if a given action is already scheduled.
*
* @since 4.0.13
* @version 4.2.7
*
* @param string $actionName The action name.
* @param array $args Args passed down to the action.
* @return boolean Whether the action is already scheduled.
*/
public function isScheduled( $actionName, $args = [] ) {
// We need to check for pending actions specifically since we otherwise cannot schedule another action while one is running (e.g. image and video sitemap scans).
$pendingArgs = [
'hook' => $actionName,
'status' => \ActionScheduler_Store::STATUS_PENDING,
'per_page' => 1
];
$runningArgs = [
'hook' => $actionName,
'status' => \ActionScheduler_Store::STATUS_RUNNING,
'per_page' => 1
];
if ( ! empty( $args ) ) {
$pendingArgs['args'] = $args;
$runningArgs['args'] = $args;
}
$actions = array_merge(
as_get_scheduled_actions( $pendingArgs ),
as_get_scheduled_actions( $runningArgs )
);
return ! empty( $actions );
}
/**
* Unschedule an action.
*
* @since 4.1.4
* @version 4.2.7
*
* @param string $actionName The action name to unschedule.
* @param array $args Args passed down to the action.
* @return void
*/
public function unschedule( $actionName, $args = [] ) {
try {
if ( as_next_scheduled_action( $actionName, $args ) ) {
as_unschedule_action( $actionName, $args, $this->actionSchedulerGroup );
}
} catch ( \Exception $e ) {
// Do nothing.
}
}
/**
* Schedules a recurring action.
*
* @since 4.1.5
* @version 4.2.7
*
* @param string $actionName The action name.
* @param int $time The seconds to add to the current time.
* @param int $interval The interval in seconds.
* @param array $args Args passed down to the action.
* @return boolean Whether the action was scheduled.
*/
public function scheduleRecurrent( $actionName, $time, $interval = 60, $args = [] ) {
try {
if ( ! $this->isScheduled( $actionName, $args ) ) {
as_schedule_recurring_action( time() + $time, $interval, $actionName, $args, $this->actionSchedulerGroup );
return true;
}
} catch ( \RuntimeException $e ) {
// Nothing needs to happen.
}
return false;
}
/**
* Schedule a single async action.
*
* @since 4.1.6
* @version 4.2.7
*
* @param string $actionName The name of the action.
* @param array $args Any relevant arguments.
* @return void
*/
public function scheduleAsync( $actionName, $args = [] ) {
try {
// Run the task immediately using an async action.
as_enqueue_async_action( $actionName, $args, $this->actionSchedulerGroup );
} catch ( \Exception $e ) {
// Do nothing.
}
}
}