ActionScheduler_QueueRunner.php
4.11 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
<?php
/**
* Class ActionScheduler_QueueRunner
*/
class ActionScheduler_QueueRunner {
const WP_CRON_HOOK = 'action_scheduler_run_queue';
const WP_CRON_SCHEDULE = 'every_minute';
/** @var ActionScheduler_QueueRunner */
private static $runner = NULL;
/** @var ActionScheduler_Store */
private $store = NULL;
/** @var ActionScheduler_FatalErrorMonitor */
private $monitor = NULL;
/**
* @return ActionScheduler_QueueRunner
* @codeCoverageIgnore
*/
public static function instance() {
if ( empty(self::$runner) ) {
$class = apply_filters('action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner');
self::$runner = new $class();
}
return self::$runner;
}
public function __construct( ActionScheduler_Store $store = NULL ) {
$this->store = $store ? $store : ActionScheduler_Store::instance();
}
/**
* @codeCoverageIgnore
*/
public function init() {
add_filter( 'cron_schedules', array( self::instance(), 'add_wp_cron_schedule' ) );
if ( !wp_next_scheduled(self::WP_CRON_HOOK) ) {
$schedule = apply_filters( 'action_scheduler_run_schedule', self::WP_CRON_SCHEDULE );
wp_schedule_event( time(), $schedule, self::WP_CRON_HOOK );
}
add_action( self::WP_CRON_HOOK, array( self::instance(), 'run' ) );
}
public function run() {
@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
@set_time_limit( apply_filters( 'action_scheduler_queue_runner_time_limit', 600 ) );
do_action( 'action_scheduler_before_process_queue' );
$this->run_cleanup();
$count = 0;
if ( $this->store->get_claim_count() < apply_filters( 'action_scheduler_queue_runner_concurrent_batches', 5 ) ) {
$batch_size = apply_filters( 'action_scheduler_queue_runner_batch_size', 25 );
$this->monitor = new ActionScheduler_FatalErrorMonitor( $this->store );
$actions_run = $this->do_batch( $batch_size );
unset( $this->monitor );
}
do_action( 'action_scheduler_after_process_queue' );
return $count;
}
protected function run_cleanup() {
$cleaner = new ActionScheduler_QueueCleaner( $this->store );
$cleaner->delete_old_actions();
$cleaner->reset_timeouts();
$cleaner->mark_failures();
}
protected function do_batch( $size = 100 ) {
$claim = $this->store->stake_claim($size);
$this->monitor->attach($claim);
$processed_actions = 0;
foreach ( $claim->get_actions() as $action_id ) {
// bail if we lost the claim
if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $claim->get_id() ) ) ) {
break;
}
$this->process_action( $action_id );
$processed_actions++;
}
$this->store->release_claim($claim);
$this->monitor->detach();
$this->clear_caches();
return $processed_actions;
}
public function process_action( $action_id ) {
try {
do_action( 'action_scheduler_before_execute', $action_id );
$action = $this->store->fetch_action( $action_id );
$this->store->log_execution( $action_id );
$action->execute();
do_action( 'action_scheduler_after_execute', $action_id );
$this->store->mark_complete( $action_id );
} catch ( Exception $e ) {
$this->store->mark_failure( $action_id );
do_action( 'action_scheduler_failed_execution', $action_id, $e );
}
$this->schedule_next_instance( $action );
}
protected function schedule_next_instance( ActionScheduler_Action $action ) {
$next = $action->get_schedule()->next( new DateTime() );
if ( $next ) {
$this->store->save_action( $action, $next );
}
}
/**
* Running large batches can eat up memory, as WP adds data to its object cache.
*
* If using a persistent object store, this has the side effect of flushing that
* as well, so this is disabled by default. To enable:
*
* add_filter( 'action_scheduler_queue_runner_flush_cache', '__return_true' );
*
* @return void
*/
protected function clear_caches() {
if ( ! wp_using_ext_object_cache() || apply_filters( 'action_scheduler_queue_runner_flush_cache', false ) ) {
wp_cache_flush();
}
}
public function add_wp_cron_schedule( $schedules ) {
$schedules['every_minute'] = array(
'interval' => 60, // in seconds
'display' => __( 'Every minute' ),
);
return $schedules;
}
}