Records
6 years ago
Catalog_Item.php
6 years ago
Interval_Polling.php
6 years ago
Job.php
6 years ago
Manual_Synchronization.php
6 years ago
Product_Import.php
5 years ago
Records.php
6 years ago
Stepped_Job.php
6 years ago
Stepped_Job.php
234 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 |
| 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 |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square\Sync; |
| 25 | |
| 26 | use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework; |
| 27 | |
| 28 | defined( 'ABSPATH' ) || exit; |
| 29 | |
| 30 | /** |
| 31 | * Stepped Job abstract. |
| 32 | * |
| 33 | * Adds multi-step management to the job class. |
| 34 | * |
| 35 | * @since 2.0.0 |
| 36 | */ |
| 37 | abstract class Stepped_Job extends Job { |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Executes the next step of this job. |
| 42 | * |
| 43 | * @since 2.0.0 |
| 44 | * |
| 45 | * @return \stdClass the job object |
| 46 | */ |
| 47 | public function run() { |
| 48 | |
| 49 | parent::run(); |
| 50 | |
| 51 | if ( empty( $this->get_attr( 'next_steps' ) ) && empty( $this->get_attr( 'completed_steps' ) ) ) { |
| 52 | $this->assign_next_steps(); |
| 53 | } |
| 54 | |
| 55 | $this->do_next_step(); |
| 56 | |
| 57 | return $this->job; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * Assigns the next steps needed for this sync job. |
| 63 | * |
| 64 | * Adds the next steps to the 'next_steps' attribute. |
| 65 | * |
| 66 | * @since 2.0.0 |
| 67 | */ |
| 68 | abstract protected function assign_next_steps(); |
| 69 | |
| 70 | |
| 71 | /** |
| 72 | * Gets the next step in the sync process. |
| 73 | * |
| 74 | * @since 2.0.0 |
| 75 | * |
| 76 | * @return string|null |
| 77 | */ |
| 78 | protected function get_next_step() { |
| 79 | |
| 80 | $next_steps = $this->get_next_steps(); |
| 81 | |
| 82 | return isset( $next_steps[0] ) ? $next_steps[0] : null; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * Gets the next steps for the sync process. |
| 88 | * |
| 89 | * @since 2.0.0 |
| 90 | * |
| 91 | * @return string[] |
| 92 | */ |
| 93 | protected function get_next_steps() { |
| 94 | |
| 95 | return $this->get_attr( 'next_steps' ); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * Performs the next step in the sync process. |
| 101 | * |
| 102 | * @since 2.0.0 |
| 103 | */ |
| 104 | protected function do_next_step() { |
| 105 | |
| 106 | $next_step = $this->get_next_step(); |
| 107 | |
| 108 | if ( is_callable( array( $this, $next_step ) ) ) { |
| 109 | |
| 110 | $this->start_step_cycle( $next_step ); |
| 111 | |
| 112 | try { |
| 113 | |
| 114 | $this->$next_step(); |
| 115 | $this->complete_step_cycle( $next_step ); |
| 116 | |
| 117 | } catch ( Framework\SV_WC_Plugin_Exception $exception ) { |
| 118 | |
| 119 | $this->complete_step_cycle( $next_step, false, $exception->getMessage() ); |
| 120 | $this->fail( $exception->getMessage() ); |
| 121 | return; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if ( ! $this->get_next_step() ) { |
| 126 | |
| 127 | $this->complete(); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * Records the beginning of a new step cycle, meaning a new loop on the job for a given step. |
| 134 | * |
| 135 | * @since 2.0.0 |
| 136 | * |
| 137 | * @param string $step_name the step name |
| 138 | */ |
| 139 | protected function start_step_cycle( $step_name ) { |
| 140 | |
| 141 | $current_step_cycle = array( |
| 142 | 'step_name' => $step_name, |
| 143 | 'start_time' => microtime( true ), |
| 144 | ); |
| 145 | |
| 146 | wc_square()->log( "Starting step cycle: $step_name" ); |
| 147 | |
| 148 | $this->set_attr( 'current_step_cycle', $current_step_cycle ); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * Records the completion of a step cycle. |
| 154 | * |
| 155 | * @since 2.0.0 |
| 156 | * |
| 157 | * @param string $step_name the step name |
| 158 | * @param bool $is_successful (optional) whether the step completion is from a success or not |
| 159 | * @param string $error_message (optional) error message to include with failed step log |
| 160 | */ |
| 161 | protected function complete_step_cycle( $step_name, $is_successful = true, $error_message = '' ) { |
| 162 | |
| 163 | $current_step_cycle = $this->get_attr( 'current_step_cycle', array() ); |
| 164 | |
| 165 | if ( ! empty( $current_step_cycle ) ) { |
| 166 | |
| 167 | $current_step_cycle['end_time'] = microtime( true ); |
| 168 | $current_step_cycle['runtime'] = number_format( $current_step_cycle['end_time'] - $current_step_cycle['start_time'], 2 ) . 's'; |
| 169 | $current_step_cycle['success'] = true === $is_successful; |
| 170 | |
| 171 | if ( true === $is_successful ) { |
| 172 | |
| 173 | wc_square()->log( "Completed step cycle: $step_name (${current_step_cycle['runtime']})" ); |
| 174 | |
| 175 | } else { |
| 176 | |
| 177 | wc_square()->log( "Failed step cycle: $step_name (${current_step_cycle['runtime']}) - $error_message" ); |
| 178 | } |
| 179 | |
| 180 | $completed_cycles = $this->get_attr( 'completed_step_cycles', array() ); |
| 181 | $completed_cycles[] = $current_step_cycle; |
| 182 | $this->set_attr( 'completed_step_cycles', $completed_cycles ); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /** |
| 188 | * Completes the specified step (if it's the next step). |
| 189 | * |
| 190 | * @since 2.0.0 |
| 191 | * |
| 192 | * @param string $step_name |
| 193 | */ |
| 194 | protected function complete_step( $step_name ) { |
| 195 | |
| 196 | $next_steps = $this->get_next_steps(); |
| 197 | |
| 198 | if ( isset( $next_steps[0] ) && $step_name === $next_steps[0] ) { |
| 199 | |
| 200 | $this->add_completed_step( $step_name ); |
| 201 | array_shift( $next_steps ); |
| 202 | $this->set_attr( 'next_steps', $next_steps ); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /** |
| 208 | * Adds a step to the completed steps array. |
| 209 | * |
| 210 | * @since 2.0.0 |
| 211 | * |
| 212 | * @param string $step_name |
| 213 | */ |
| 214 | protected function add_completed_step( $step_name ) { |
| 215 | |
| 216 | if ( empty( $step_name ) ) { |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | $completed_steps = $this->get_attr( 'completed_steps', array() ); |
| 221 | |
| 222 | $completed_steps[] = array( |
| 223 | 'name' => $step_name, |
| 224 | 'completion_time' => current_time( 'mysql' ), |
| 225 | ); |
| 226 | |
| 227 | $this->set_attr( 'completed_steps', $completed_steps ); |
| 228 | |
| 229 | wc_square()->log( 'Completed job step: ' . $step_name ); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | } |
| 234 |