HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.30
System: Linux iZj6c1151k3ad370bosnmsZ 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64
User: root (0)
PHP: 7.4.30
Disabled: NONE
Upload Files
File: /var/www/html/www.winghung.com/wp-content/plugins/conversational-forms/cf2/Jobs/Scheduler.php
<?php


namespace qcformbuilderwp\qcformbuilderforms\cf2\Jobs;


use WP_Queue\Queue;

class Scheduler
{
	/**
	 * Queue manager
	 *
	 * @since 1.8.0
	 *
	 * @var Queue
	 */
	protected $queue;

	/**
	 * Scheduler constructor.
	 *
	 * @since  1.8.0
	 *
	 * @param Queue $queue
	 */
	public function __construct(Queue $queue)
	{
		$this->queue = $queue;
	}

	/**
	 * Get the queue worker
	 *
	 * @since  1.8.0
	 *
	 * @return \WP_Queue\Worker
	 */
	public function getWorker(){
		return $this->queue->worker(2);
	}

	/**
	 * Schedule a job to run
	 *
	 * @since 1.8.0
	 *
	 * @param Job $job Job to schedule.
	 * @param int $delay Optional. Delay in seconds until job runs. Default is 0.
	 */
	public function schedule(Job $job, $delay = 0){
		$this->queue->push($job, $delay );
	}

	/**
	 * Run the queue with WP_Cron
	 *
	 * @since 1.8.0
	 *
	 * @return bool
	 */
	public function runWithCron()
	{
		return $this->queue->cron(2)->init();
	}

	/**
	 * Run jobs independently of WP_Cron
	 *
	 * @since 1.8.0
	 *
	 * @param int $numberOfJobs
	 *
	 * @return int
	 */
	public function runJobs($numberOfJobs = 5){
		$worker = $this->getWorker();
		$jobsRan = 0;
		for( $totalJobs = 0; $totalJobs < $numberOfJobs; $totalJobs++ ){
			if (!$worker->process()) {
				return $jobsRan;
			}
			$jobsRan++;

		}

		return $jobsRan;

	}

}