PHP code example of ride / lib-queue

1. Go to this page and download the library: Download ride/lib-queue library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ride / lib-queue example snippets




use ride\library\queue\dispatcher\QueueDispatcher;
use ride\library\queue\job\AbstractQueueJob;
use ride\library\queue\worker\GenericQueueWorker;
use ride\library\queue\QueueManager;

// push some jobs to the queue
function pushJobs(QueueDispatcher $queueDispatcher, array $files) {
    foreach ($files as $index => $file) {
        // create a job with the needed references
        $job = new MyQueueJob($file);
         
        // you can optionally set a priority
        // jobs with a smaller priority value are more urgent
        $job->setPriority(100);
         
        // the queue dispatcher will select a queue and push your job to it
        $queueJobStatus = $queueDispatcher->queue($job);
    
        // you get the job id and more properties through the resulting queue job status     
        $files[$index] = $queueJobStatus->getId();
    }
    
    return $files;
}

// work horse
function work(QueueManager $queueManager, $queue = 'default') {
    // use a generic worker
    $queueWorker = new GenericQueueWorker();
    
    // this worker will poll the queue every 3 seconds for a job forever
    // you can provide a 0 second sleep time, the worker will then stop when it has no jobs
    $queueWorker->work($queueManager, $queue, 3);
}

// example job which executes pngcrush on the provided path
class MyQueueJob extends AbstractQueueJob {

    public function __construct($path) {
        $this->path = $path;
    }

    public function run(QueueManager $queueManager) {
        // you can update the description of your job while running
        $queueManager->updateStatus($this->getJobId(), 'Checking file ' . $this->path);
        
        if (file_exists($this->path) && !is_directory($this->path)) {
            return;
        }
        
        $command = 'pngcrush -nofilecheck -rem alla -bail -blacken -ow ' . $this->path; 
        
        $queueManager->updateStatus($this->getJobId(), 'Invoking ' . $command);
        
        exec($command, $output, $code);
        
        $queueManager->updateStatus($this->getJobId(), $command . ' returned ' . $code);
    }
    
}