Return the modified item for further processing * in the next pass through. Or, return false to remove the * item from the queue. * * @param mixed $task Queue item to iterate over. * * @return mixed */ abstract protected function task( $task ); private function is_active_instance( $instance_id ) { return $instance_id === $this->get_active_instance_id(); } /** * Save the unique ID of the process we are presuming to be dead, so we can prevent it from coming back. * * @param $instance_id * * @return void */ private function set_active_instance_id( $instance_id ) { update_site_option( $this->get_active_instance_option_id(), $instance_id ); } private function get_active_instance_id() { return get_site_option( $this->get_active_instance_option_id(), '' ); } private function get_active_instance_option_id() { return $this->identifier . '_active_instance'; } private function set_process_id( $instance_id ) { update_site_option( $this->get_process_id_option_key(), $instance_id ); } public function get_process_id() { return get_site_option( $this->get_process_id_option_key() ); } private function delete_process_id() { delete_site_option( $this->get_process_id_option_key() ); } private function get_process_id_option_key() { return $this->identifier . '_process_id'; } public function set_logger( $logger ) { $this->logger_container->set_logger( $logger ); } /** * @return Background_Logger_Container */ private function logger() { return $this->logger_container; } public function get_status() { return $this->status; } /** * @param $tasks array * * @return void */ public function start( $tasks ) { $this->do_action( 'before_start' ); $total_items = count( $tasks ); $this->status->start( $total_items ); $this->update_queue( $tasks ); // Generate ID for the whole process. $this->set_process_id( $this->generate_unique_id() ); $this->logger()->info( "Starting new process with $total_items tasks" ); // Trigger the started event before dispatching the request to ensure it is called before the completed event. $this->do_action( 'started' ); $this->spawn(); } private function mutex( $operation ) { $mutex = new Mutex( $this->get_handler_mutex_id() ); $mutex->set_break_on_timeout( true ) // Let the previous handler do its thing ->set_timeout( $this->get_lock_duration() ) ->execute( $operation ); } private function get_handler_mutex_id() { return $this->identifier . '_handler_lock'; } private function get_time_limit() { return apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds } private function get_lock_duration() { $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute return apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); } protected function get_instance_expiry_duration_seconds() { return MINUTE_IN_SECONDS * 2; } private function get_last_run_transient_key() { return $this->identifier . '_last_run'; } private function clear_last_run_timestamp() { delete_site_transient( $this->get_last_run_transient_key() ); } private function cleanup() { // Delete options and transients $this->delete_queue(); delete_site_option( $this->get_active_instance_option_id() ); $this->delete_process_id(); $this->delete_revival_count(); $this->clear_last_run_timestamp(); // Cancel all events $this->clear_scheduled_event(); } private function task_limit_reached( $processed_tasks_count ) { if ( $this->get_tasks_per_request() === self::TASKS_PER_REQUEST_UNLIMITED ) { return false; } return $processed_tasks_count >= $this->get_tasks_per_request(); } public function get_tasks_per_request() { return $this->tasks_per_request; } /** * @param int $tasks_per_request */ public function set_tasks_per_request( $tasks_per_request ) { $this->tasks_per_request = $tasks_per_request; } private function do_action( $action ) { do_action( "{$this->identifier}_$action", $this->identifier, $this ); } private function get_cron_interval_seconds() { $minutes = property_exists( $this, 'cron_interval' ) ? $this->cron_interval : 5; $interval = apply_filters( $this->identifier . '_cron_interval', $minutes ); return $interval * MINUTE_IN_SECONDS; } public function get_identifier() { return $this->identifier; } protected function should_update_queue_after_task() { return false; } private function increment_revival_count() { $revival_count = $this->get_revival_count(); $this->set_revival_count( $revival_count + 1 ); } private function set_revival_count( $instance_id ) { update_site_option( $this->get_revival_count_option_key(), $instance_id ); } public function get_revival_count() { return (int) get_site_option( $this->get_revival_count_option_key(), 0 ); } private function delete_revival_count() { delete_site_option( $this->get_revival_count_option_key() ); } private function get_revival_count_option_key() { return $this->identifier . '_revival_count'; } protected function get_revival_limit() { return apply_filters( $this->identifier . '_revival_limit', 5 ); } protected function is_revival_limit_reached() { return $this->get_revival_count() >= $this->get_revival_limit(); } }