CycleStateService.php
10 months ago
JobManager.php
10 months ago
NotificationsProcessor.php
4 weeks ago
JobManager.php
155 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\StockNotifications\AsyncTasks; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\StockNotifications\Enums\NotificationStatus; |
| 8 | use Automattic\WooCommerce\Internal\StockNotifications\Enums\NotificationCancellationSource; |
| 9 | use Automattic\WooCommerce\Internal\StockNotifications\Factory; |
| 10 | use Automattic\WooCommerce\Internal\StockNotifications\Config; |
| 11 | use Automattic\WooCommerce\Internal\StockNotifications\Notification; |
| 12 | use Automattic\WooCommerce\Internal\StockNotifications\NotificationQuery; |
| 13 | use Automattic\WooCommerce\Internal\StockNotifications\Emails\EmailManager; |
| 14 | use Automattic\WooCommerce\Internal\StockNotifications\Utilities\EligibilityService; |
| 15 | use WC_Product; |
| 16 | use Exception; |
| 17 | |
| 18 | /** |
| 19 | * The manager for async tasks. |
| 20 | */ |
| 21 | class JobManager { |
| 22 | |
| 23 | /** |
| 24 | * The job hook for sending stock notifications. |
| 25 | */ |
| 26 | public const AS_JOB_SEND_STOCK_NOTIFICATIONS = 'wc_send_stock_notifications_batch'; |
| 27 | |
| 28 | /** |
| 29 | * The job group for stock notifications. |
| 30 | */ |
| 31 | public const AS_JOB_GROUP = 'wc-stock-notifications'; |
| 32 | |
| 33 | /** |
| 34 | * The logger instance. |
| 35 | * |
| 36 | * @var \WC_Logger_Interface |
| 37 | */ |
| 38 | private $logger; |
| 39 | |
| 40 | /** |
| 41 | * The queue instance. |
| 42 | * |
| 43 | * @var \WC_Queue_Interface |
| 44 | */ |
| 45 | private $queue; |
| 46 | |
| 47 | /** |
| 48 | * Constructor. |
| 49 | * |
| 50 | * @return void |
| 51 | */ |
| 52 | public function __construct() { |
| 53 | $this->logger = \wc_get_logger(); |
| 54 | $this->queue = \WC()->queue(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Schedule a job. |
| 59 | * |
| 60 | * @param int $product_id The product ID. |
| 61 | * @return bool True if the job was scheduled, false otherwise. |
| 62 | */ |
| 63 | public function schedule_initial_job_for_product( int $product_id ): bool { |
| 64 | $args = array( 'product_id' => $product_id ); |
| 65 | |
| 66 | try { |
| 67 | |
| 68 | if ( $this->queue->get_next( self::AS_JOB_SEND_STOCK_NOTIFICATIONS, $args, self::AS_JOB_GROUP ) ) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Filter: woocommerce_customer_stock_notifications_first_batch_delay |
| 74 | * |
| 75 | * @since 10.2.0 |
| 76 | * |
| 77 | * Schedule the first batch with a delay to prevent overwhelming the system. |
| 78 | * |
| 79 | * @param int $delay Delay time in seconds before first batch. |
| 80 | * @param int $product_id Product ID being scheduled. |
| 81 | */ |
| 82 | $delay = (int) apply_filters( 'woocommerce_customer_stock_notifications_first_batch_delay', MINUTE_IN_SECONDS, $product_id ); |
| 83 | $delay = max( 0, $delay ); |
| 84 | |
| 85 | $action_id = $this->queue->schedule_single( |
| 86 | time() + $delay, |
| 87 | self::AS_JOB_SEND_STOCK_NOTIFICATIONS, |
| 88 | $args, |
| 89 | self::AS_JOB_GROUP |
| 90 | ); |
| 91 | |
| 92 | if ( ! $action_id ) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | $this->logger->info( |
| 97 | sprintf( 'Scheduled stock notification for product %d', $product_id ), |
| 98 | array( 'source' => 'wc-customer-stock-notifications' ) |
| 99 | ); |
| 100 | |
| 101 | return true; |
| 102 | } catch ( Exception $e ) { |
| 103 | $this->logger->error( |
| 104 | sprintf( 'Failed to schedule stock notification for product %d: %s', $product_id, $e->getMessage() ), |
| 105 | array( 'source' => 'wc-customer-stock-notifications' ) |
| 106 | ); |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Schedule the next batch for a product. |
| 114 | * |
| 115 | * @param int $product_id The product ID. |
| 116 | * @return bool |
| 117 | */ |
| 118 | public function schedule_next_batch_for_product( int $product_id ): bool { |
| 119 | |
| 120 | $args = array( 'product_id' => $product_id ); |
| 121 | |
| 122 | if ( $this->queue->get_next( self::AS_JOB_SEND_STOCK_NOTIFICATIONS, $args, self::AS_JOB_GROUP ) ) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Filter: woocommerce_customer_stock_notifications_next_batch_delay |
| 128 | * |
| 129 | * @since 10.2.0 |
| 130 | * |
| 131 | * @param int $delay Delay time in seconds before next batch. |
| 132 | * @param int $product_id Product ID being scheduled. |
| 133 | */ |
| 134 | $delay = (int) apply_filters( 'woocommerce_customer_stock_notifications_next_batch_delay', 0, $product_id ); |
| 135 | $delay = max( 0, $delay ); |
| 136 | |
| 137 | if ( 0 === $delay ) { |
| 138 | $action_id = $this->queue->add( |
| 139 | self::AS_JOB_SEND_STOCK_NOTIFICATIONS, |
| 140 | $args, |
| 141 | self::AS_JOB_GROUP |
| 142 | ); |
| 143 | } else { |
| 144 | $action_id = $this->queue->schedule_single( |
| 145 | time() + $delay, |
| 146 | self::AS_JOB_SEND_STOCK_NOTIFICATIONS, |
| 147 | $args, |
| 148 | self::AS_JOB_GROUP |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | return ! empty( $action_id ); |
| 153 | } |
| 154 | } |
| 155 |