1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WP_Queue; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
abstract class Job { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var int |
12
|
|
|
*/ |
13
|
|
|
private $id; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
private $attempts; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Carbon |
22
|
|
|
*/ |
23
|
|
|
private $reserved_at; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Carbon |
27
|
|
|
*/ |
28
|
|
|
private $available_at; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Carbon |
32
|
|
|
*/ |
33
|
|
|
private $created_at; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
private $released = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
private $failed = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Handle job logic. |
47
|
|
|
*/ |
48
|
|
|
abstract public function handle(); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get job ID. |
52
|
|
|
* |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
6 |
|
public function id() { |
56
|
6 |
|
return $this->id; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set job ID. |
61
|
|
|
* |
62
|
|
|
* @param int $id |
63
|
|
|
*/ |
64
|
1 |
|
public function set_id( $id ) { |
65
|
1 |
|
$this->id = $id; |
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get job attempts; |
70
|
|
|
* |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
3 |
|
public function attempts() { |
74
|
3 |
|
return $this->attempts; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set job attempts. |
79
|
|
|
* |
80
|
|
|
* @param int $attempts |
81
|
|
|
*/ |
82
|
1 |
|
public function set_attempts( $attempts ) { |
83
|
1 |
|
$this->attempts = $attempts; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get reserved at date. |
88
|
|
|
* |
89
|
|
|
* @return Carbon |
90
|
|
|
*/ |
91
|
1 |
|
public function reserved_at() { |
92
|
1 |
|
return $this->reserved_at; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set reserved at date. |
97
|
|
|
* |
98
|
|
|
* @param null|Carbon $reserved_at |
99
|
|
|
*/ |
100
|
1 |
|
public function set_reserved_at( $reserved_at ) { |
101
|
1 |
|
$this->reserved_at = $reserved_at; |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get available at date. |
106
|
|
|
* |
107
|
|
|
* @return Carbon |
108
|
|
|
*/ |
109
|
1 |
|
public function available_at() { |
110
|
1 |
|
return $this->available_at; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set available at date. |
115
|
|
|
* |
116
|
|
|
* @param Carbon $available_at |
117
|
|
|
*/ |
118
|
1 |
|
public function set_available_at( Carbon $available_at ) { |
119
|
1 |
|
$this->available_at = $available_at; |
120
|
1 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get created at date. |
124
|
|
|
* |
125
|
|
|
* @return Carbon |
126
|
|
|
*/ |
127
|
1 |
|
public function created_at() { |
128
|
1 |
|
return $this->created_at; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set created at date. |
133
|
|
|
* |
134
|
|
|
* @param Carbon $created_at |
135
|
|
|
*/ |
136
|
1 |
|
public function set_created_at( Carbon $created_at ) { |
137
|
1 |
|
$this->created_at = $created_at; |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Flag job as released. |
142
|
|
|
*/ |
143
|
1 |
|
public function release() { |
144
|
1 |
|
$this->released = true; |
145
|
1 |
|
$this->attempts += 1; |
146
|
1 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Should the job be released back onto the queue? |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
1 |
|
public function released() { |
154
|
1 |
|
return $this->released; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Flag job as failed. |
159
|
|
|
*/ |
160
|
1 |
|
public function fail() { |
161
|
1 |
|
$this->failed = true; |
162
|
1 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Has the job failed? |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
1 |
|
public function failed() { |
170
|
1 |
|
return $this->failed; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Determine which properties should be serialized. |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
7 |
|
public function __sleep() { |
179
|
7 |
|
$object_props = get_object_vars( $this ); |
180
|
|
|
$excluded_props = array( |
181
|
7 |
|
'id', |
182
|
|
|
'attempts', |
183
|
|
|
'reserved_at', |
184
|
|
|
'available_at', |
185
|
|
|
'created_at', |
186
|
|
|
'released', |
187
|
|
|
'failed', |
188
|
|
|
); |
189
|
|
|
|
190
|
7 |
|
foreach ( $excluded_props as $prop ) { |
191
|
7 |
|
unset( $object_props[ $prop ] ); |
192
|
|
|
} |
193
|
|
|
|
194
|
7 |
|
return array_keys( $object_props ); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
} |