ActionScheduler.php
3.39 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
<?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 extends \ActionScheduler_ListTable {
/**
* Class Constructor.
*
* @since 4.0.0
*
* @param $store
* @param $logger
* @param $runner
*/
public function __construct( $store, $logger, $runner ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if (
(
is_a( $store, 'ActionScheduler_HybridStore' ) ||
is_a( $store, 'ActionScheduler_DBStore' )
) &&
apply_filters( 'action_scheduler_enable_recreate_data_store', true ) &&
method_exists( get_parent_class( $this ), 'recreate_tables' )
) {
$tableList = [
'actionscheduler_actions',
'actionscheduler_logs',
'actionscheduler_groups',
'actionscheduler_claims',
];
foreach ( $tableList as $tableName ) {
if ( ! aioseo()->db->tableExists( $tableName ) ) {
$this->recreate_tables();
return;
}
}
}
add_action( 'action_scheduler_after_execute', [ $this, 'cleanup' ], 1000, 2 );
}
/**
* Cleans up the Action Scheduler tables after one of our actions completes.
*
* @since 4.0.10
*
* @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()->db->tableExists( 'actionscheduler_actions' ) ||
! aioseo()->db->tableExists( 'actionscheduler_groups' )
) {
return;
}
$prefix = aioseo()->db->db->prefix;
// Clean up logs associated with entries in the actions table.
aioseo()->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()->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()->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()->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');"
);
// Clean up orphaned log files. @TODO: Look at adding this back in, however it was causing errors with the number of locks exceeding the lock table size.
// aioseo()->db->execute(
// "DELETE al FROM {$prefix}actionscheduler_logs as al
// LEFT JOIN {$prefix}actionscheduler_actions as aa on `aa`.`action_id` = `al`.`action_id`
// WHERE `aa`.`action_id` IS NULL
// LIMIT 100000;"
// );
}
}