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
|
<?php
class ITSEC_Job {
/** @var ITSEC_Scheduler */ private $scheduler;
/** @var string */ private $id;
/** @var array */ private $data;
/** @var array */ private $opts;
/** * ITSEC_Job constructor. * * @param ITSEC_Scheduler $scheduler * @param string $id * @param array $data * @param array $opts */ public function __construct( ITSEC_Scheduler $scheduler, $id, $data = array(), $opts = array() ) { $this->scheduler = $scheduler; $this->id = $id; $this->data = $data; $this->opts = $opts; }
/** * Reschedule a job in some number of seconds. * * The original event will not fire while a reschedule is pending. * * @param int $seconds * @param array $data Additional data to attach to the rescheduled event. */ public function reschedule_in( $seconds, $data = array() ) { $data = array_merge( $this->data, $data );
if ( isset( $data['retry_count'] ) ) { $data['retry_count'] ++; } else { $data['retry_count'] = 1; }
$this->scheduler->schedule_once( ITSEC_Core::get_current_time_gmt() + $seconds, $this->id, $data ); }
/** * Schedule the next loop item. * * @param array $data Data to provide to the next event. */ public function schedule_next_in_loop( $data = array() ) { if ( ! $config = $this->scheduler->get_loop( $this->get_id() ) ) { return; }
$data = array_merge( $this->get_data(), $data, array( 'loop_item' => $this->data['loop_item'] + 1, ) );
$this->scheduler->schedule_once( ITSEC_Core::get_current_time_gmt() + $config['wait'], $this->get_id(), $data ); }
/** * Schedule the loop to start over again. * * @param array $data */ public function schedule_new_loop( $data = array() ) {
if ( ! $config = $this->scheduler->get_loop( $this->get_id() ) ) { return; }
$start = $this->data['loop_start']; $interval = $this->scheduler->get_schedule_interval( $config['schedule'] ); $now = ITSEC_Core::get_current_time_gmt();
$next = $start + $interval < $now ? $now + $config['wait'] : $start + $interval;
$this->scheduler->schedule_loop( $this->get_id(), $data, array( 'fire_at' => $next, ) ); }
/** * Get the retry count for this job. * * @return int|false */ public function is_retry() { $data = $this->get_data();
if ( empty( $data['retry_count'] ) ) { return false; }
return $data['retry_count']; }
/** * Get the ID of this job. * * @return string */ public function get_id() { return $this->id; }
/** * Get the data attached to the job. * * @return array */ public function get_data() { return $this->data; }
/** * Is this a single event. * * @return bool */ public function is_single() { return ! empty( $this->opts['single'] ); } }
|