AbandonedCartsJob.php
6 months ago
AbstractBatchedJob.php
6 months ago
AbstractJob.php
3 months ago
ActionScheduler.php
5 months ago
CleanupCartsJob.php
6 months ago
ImportJob.php
5 months ago
JobInterface.php
7 months ago
RecurringJobInterface.php
7 months ago
AbstractBatchedJob.php
78 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Jobs; |
| 4 | |
| 5 | |
| 6 | defined( 'ABSPATH' ) || exit; |
| 7 | |
| 8 | abstract class AbstractBatchedJob extends AbstractJob { |
| 9 | |
| 10 | public const BATCH_LIMIT_PER_JOB = 15; |
| 11 | |
| 12 | public function init(): void { |
| 13 | add_action( $this->get_create_batch_hook(), array( $this, 'handle_create_batch_action' ), 10, 2 ); |
| 14 | parent::init(); |
| 15 | } |
| 16 | |
| 17 | protected function get_create_batch_hook(): string { |
| 18 | return "{$this->get_hook_base_name()}/create_batch"; |
| 19 | } |
| 20 | |
| 21 | public function schedule( array $args = array() ): void { |
| 22 | $this->schedule_create_batch_action( 1, $args ); |
| 23 | } |
| 24 | |
| 25 | public function handle_create_batch_action( int $batch_number, array $args ): void { |
| 26 | $items = $this->get_batch( $batch_number, $args ); |
| 27 | |
| 28 | if ( empty( $items ) || $batch_number > self::BATCH_LIMIT_PER_JOB ) { |
| 29 | $this->handle_complete( $batch_number, $args ); |
| 30 | } else { |
| 31 | $this->set_items_as_processing( $items ); |
| 32 | $this->schedule_process_action( $items, $args ); |
| 33 | $this->schedule_create_batch_action( $batch_number + 1, $args ); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | protected function get_batch_size(): int { |
| 38 | return apply_filters( 'hostinger_reach_batch_item_limit', 1 ); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | protected function schedule_create_batch_action( int $batch_number, array $args ): void { |
| 43 | if ( $this->can_schedule( array( $batch_number ) ) ) { |
| 44 | $this->action_scheduler->schedule_immediate( |
| 45 | $this->get_create_batch_hook(), |
| 46 | array( |
| 47 | $batch_number, |
| 48 | $args, |
| 49 | ) |
| 50 | ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | protected function schedule_process_action( array $items = array(), array $args = array() ): void { |
| 55 | $job_data = array( |
| 56 | 'items' => $items, |
| 57 | 'args' => $args, |
| 58 | ); |
| 59 | if ( ! $this->is_processing( $job_data ) ) { |
| 60 | $this->action_scheduler->schedule_immediate( $this->get_process_item_hook(), array( $job_data ) ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | protected function is_processing( array $args = array() ): bool { |
| 65 | return $this->action_scheduler->has_scheduled_action( $this->get_process_item_hook(), array( $args ) ); |
| 66 | } |
| 67 | |
| 68 | protected function handle_complete( int $final_batch_number, array $args ): void { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | protected function set_items_as_processing( array $items ): void { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | abstract protected function get_batch( int $batch_number, array $args ): array; |
| 77 | } |
| 78 |