PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 3.8.1
WooCommerce Square v3.8.1
5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Sync / Stepped_Job.php
woocommerce-square / includes / Sync Last commit date
Records 3 years ago Catalog_Item.php 3 years ago Interval_Polling.php 3 years ago Job.php 3 years ago Manual_Synchronization.php 3 years ago Product_Import.php 3 years ago Records.php 3 years ago Stepped_Job.php 3 years ago
Stepped_Job.php
232 lines
1 <?php
2 /**
3 * WooCommerce Square
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@woocommerce.com so we can send you a copy immediately.
12 *
13 * DISCLAIMER
14 *
15 * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer
16 * versions in the future. If you wish to customize WooCommerce Square for your
17 * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/
18 *
19 * @author WooCommerce
20 * @copyright Copyright: (c) 2019, Automattic, Inc.
21 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
22 */
23
24 namespace WooCommerce\Square\Sync;
25
26 defined( 'ABSPATH' ) || exit;
27
28 /**
29 * Stepped Job abstract.
30 *
31 * Adds multi-step management to the job class.
32 *
33 * @since 2.0.0
34 */
35 abstract class Stepped_Job extends Job {
36
37
38 /**
39 * Executes the next step of this job.
40 *
41 * @since 2.0.0
42 *
43 * @return \stdClass the job object
44 */
45 public function run() {
46
47 parent::run();
48
49 if ( empty( $this->get_attr( 'next_steps' ) ) && empty( $this->get_attr( 'completed_steps' ) ) ) {
50 $this->assign_next_steps();
51 }
52
53 $this->do_next_step();
54
55 return $this->job;
56 }
57
58
59 /**
60 * Assigns the next steps needed for this sync job.
61 *
62 * Adds the next steps to the 'next_steps' attribute.
63 *
64 * @since 2.0.0
65 */
66 abstract protected function assign_next_steps();
67
68
69 /**
70 * Gets the next step in the sync process.
71 *
72 * @since 2.0.0
73 *
74 * @return string|null
75 */
76 protected function get_next_step() {
77
78 $next_steps = $this->get_next_steps();
79
80 return isset( $next_steps[0] ) ? $next_steps[0] : null;
81 }
82
83
84 /**
85 * Gets the next steps for the sync process.
86 *
87 * @since 2.0.0
88 *
89 * @return string[]
90 */
91 protected function get_next_steps() {
92
93 return $this->get_attr( 'next_steps' );
94 }
95
96
97 /**
98 * Performs the next step in the sync process.
99 *
100 * @since 2.0.0
101 */
102 protected function do_next_step() {
103
104 $next_step = $this->get_next_step();
105
106 if ( is_callable( array( $this, $next_step ) ) ) {
107
108 $this->start_step_cycle( $next_step );
109
110 try {
111
112 $this->$next_step();
113 $this->complete_step_cycle( $next_step );
114
115 } catch ( \Exception $exception ) {
116
117 $this->complete_step_cycle( $next_step, false, $exception->getMessage() );
118 $this->fail( $exception->getMessage() );
119 return;
120 }
121 }
122
123 if ( ! $this->get_next_step() ) {
124
125 $this->complete();
126 }
127 }
128
129
130 /**
131 * Records the beginning of a new step cycle, meaning a new loop on the job for a given step.
132 *
133 * @since 2.0.0
134 *
135 * @param string $step_name the step name
136 */
137 protected function start_step_cycle( $step_name ) {
138
139 $current_step_cycle = array(
140 'step_name' => $step_name,
141 'start_time' => microtime( true ),
142 );
143
144 wc_square()->log( "Starting step cycle: $step_name" );
145
146 $this->set_attr( 'current_step_cycle', $current_step_cycle );
147 }
148
149
150 /**
151 * Records the completion of a step cycle.
152 *
153 * @since 2.0.0
154 *
155 * @param string $step_name the step name
156 * @param bool $is_successful (optional) whether the step completion is from a success or not
157 * @param string $error_message (optional) error message to include with failed step log
158 */
159 protected function complete_step_cycle( $step_name, $is_successful = true, $error_message = '' ) {
160
161 $current_step_cycle = $this->get_attr( 'current_step_cycle', array() );
162
163 if ( ! empty( $current_step_cycle ) ) {
164
165 $current_step_cycle['end_time'] = microtime( true );
166 $current_step_cycle['runtime'] = number_format( $current_step_cycle['end_time'] - $current_step_cycle['start_time'], 2 ) . 's';
167 $current_step_cycle['success'] = true === $is_successful;
168
169 if ( true === $is_successful ) {
170
171 wc_square()->log( "Completed step cycle: $step_name (${current_step_cycle['runtime']})" );
172
173 } else {
174
175 wc_square()->log( "Failed step cycle: $step_name (${current_step_cycle['runtime']}) - $error_message" );
176 }
177
178 $completed_cycles = $this->get_attr( 'completed_step_cycles', array() );
179 $completed_cycles[] = $current_step_cycle;
180 $this->set_attr( 'completed_step_cycles', $completed_cycles );
181 }
182 }
183
184
185 /**
186 * Completes the specified step (if it's the next step).
187 *
188 * @since 2.0.0
189 *
190 * @param string $step_name
191 */
192 protected function complete_step( $step_name ) {
193
194 $next_steps = $this->get_next_steps();
195
196 if ( isset( $next_steps[0] ) && $step_name === $next_steps[0] ) {
197
198 $this->add_completed_step( $step_name );
199 array_shift( $next_steps );
200 $this->set_attr( 'next_steps', $next_steps );
201 }
202 }
203
204
205 /**
206 * Adds a step to the completed steps array.
207 *
208 * @since 2.0.0
209 *
210 * @param string $step_name
211 */
212 protected function add_completed_step( $step_name ) {
213
214 if ( empty( $step_name ) ) {
215 return;
216 }
217
218 $completed_steps = $this->get_attr( 'completed_steps', array() );
219
220 $completed_steps[] = array(
221 'name' => $step_name,
222 'completion_time' => current_time( 'mysql' ),
223 );
224
225 $this->set_attr( 'completed_steps', $completed_steps );
226
227 wc_square()->log( 'Completed job step: ' . $step_name );
228 }
229
230
231 }
232