class-background-process-status.php
4.43 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
<?php
namespace Smush\Core\Modules\Background;
class Background_Process_Status {
const PROCESSING = 'in_processing';
const CANCELLED = 'is_cancelled';
const COMPLETED = 'is_completed';
const DEAD = 'is_dead';
const TOTAL_ITEMS = 'total_items';
const PROCESSED_ITEMS = 'processed_items';
const FAILED_ITEMS = 'failed_items';
private $identifier;
/**
* @var Background_Utils
*/
private $utils;
public function __construct( $identifier ) {
$this->identifier = $identifier;
$this->utils = new Background_Utils();
}
public function get_data() {
$option_value = $this->utils->get_site_option(
$this->get_option_id(),
array()
);
return wp_parse_args(
$option_value,
array(
self::PROCESSING => false,
self::CANCELLED => false,
self::COMPLETED => false,
self::TOTAL_ITEMS => 0,
self::PROCESSED_ITEMS => 0,
self::FAILED_ITEMS => 0,
)
);
}
public function to_array() {
return $this->get_data();
}
private function set_data( $updated ) {
$data = $this->get_data();
update_site_option( $this->get_option_id(), array_merge( $data, $updated ) );
}
private function get_value( $key ) {
$data = $this->get_data();
return isset( $data[ $key ] )
? $data[ $key ]
: false;
}
private function set_value( $key, $value ) {
$this->mutex( function () use ( $key, $value ) {
$updated_data = array_merge(
$this->get_data(),
array( $key => $value )
);
update_site_option( $this->get_option_id(), $updated_data );
} );
}
private function get_option_id() {
return $this->identifier . '_status';
}
public function is_in_processing() {
return $this->get_value( self::PROCESSING );
}
public function set_in_processing( $in_processing ) {
$this->set_value( self::PROCESSING, $in_processing );
}
public function get_total_items() {
return $this->get_value( self::TOTAL_ITEMS );
}
public function set_total_items( $total_items ) {
$this->set_value( self::TOTAL_ITEMS, $total_items );
}
public function get_processed_items() {
return $this->get_value( self::PROCESSED_ITEMS );
}
public function set_processed_items( $processed_items ) {
$this->set_value( self::PROCESSED_ITEMS, $processed_items );
}
public function get_failed_items() {
return $this->get_value( self::FAILED_ITEMS );
}
public function set_failed_items( $failed_items ) {
$this->set_value( self::PROCESSED_ITEMS, $failed_items );
}
public function is_cancelled() {
return $this->get_value( self::CANCELLED );
}
public function set_is_cancelled( $is_cancelled ) {
$this->set_value( self::CANCELLED, $is_cancelled );
}
public function is_dead() {
return $this->get_value( self::DEAD );
}
public function is_completed() {
return $this->get_value( self::COMPLETED );
}
public function set_is_completed( $is_completed ) {
$this->set_value( self::COMPLETED, $is_completed );
}
private function mutex( $operation ) {
$mutex = new Mutex( $this->get_option_id() );
$mutex->execute( $operation );
}
public function start( $total_items ) {
$this->mutex( function () use ( $total_items ) {
$this->set_data( array(
self::PROCESSING => true,
self::CANCELLED => false,
self::DEAD => false,
self::COMPLETED => false,
self::TOTAL_ITEMS => $total_items,
self::PROCESSED_ITEMS => 0,
self::FAILED_ITEMS => 0,
) );
} );
}
public function complete() {
$this->mutex( function () {
$this->set_data( array(
self::PROCESSING => false,
self::CANCELLED => false,
self::DEAD => false,
self::COMPLETED => true,
) );
} );
}
public function cancel() {
$this->mutex( function () {
$this->set_data( array(
self::PROCESSING => false,
self::CANCELLED => true,
self::DEAD => false,
self::COMPLETED => false,
) );
} );
}
public function mark_as_dead() {
$this->mutex( function () {
$this->set_data( array(
self::PROCESSING => false,
self::CANCELLED => false,
self::DEAD => true,
self::COMPLETED => false,
) );
} );
}
public function task_successful() {
$this->mutex( function () {
$this->set_data( array(
self::PROCESSED_ITEMS => $this->get_processed_items() + 1,
) );
} );
}
public function task_failed() {
$this->mutex( function () {
$this->set_data( array(
self::PROCESSED_ITEMS => $this->get_processed_items() + 1,
self::FAILED_ITEMS => $this->get_failed_items() + 1,
) );
} );
}
}