1. Go to this page and download the library: Download tomzx/job 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/ */
tomzx / job example snippets
use tomzx\Job\Job;
use tomzx\Job\Throttler;
class SymfonyProcessJob extends Job
{
/**
* @var \Symfony\Component\Process\Process
*/
private $process;
public function __construct(Process $process)
{
$this->process = $process;
}
public function handle()
{
$this->process->start();
}
public function getProcess()
{
return $this->process;
}
public function isResolved()
{
return $this->process->isTerminated();
}
}
// Create a throttler with a maximum of 5 jobs in parallel
$throttler = new Throttler(5);
// Bind event handlers
$throttler->onJobWaiting(function (SymfonyProcessJob $job) {
echo 'New job added!' . PHP_EOL;
});
$throttler->onJobRunning(function (SymfonyProcessJob $job) {
echo 'Job running!' . PHP_EOL;
});
$throttler->onJobCompleted(function (SymfonyProcessJob $job) {
echo $job->getProcess()->getOutput() . PHP_EOL;
});
// Create a couple of jobs
for ($i = 0; $i < 10; ++$i) {
// Push the jobs, they will not be started until ->wait is called()
$throttler->push(new SymfonyProcessJob(new Process('sleep 1 && time')));
// Push the jobs and start them right away
//$throttler->pushAndStart(new SymfonyProcessJob(new Process('sleep 1 && time')));
}
// Block until the jobs are completed
$throttler->wait();
class TestJob extends Job
{
private $handled = false;
public function handle()
{
static $i = 0;
echo ++$i . PHP_EOL;
sleep(1);
$this->handled = true;
}
public function isResolved()
{
return $this->handled;
}
}
$jobQueue = new JobQueue();
// Create a couple of jobs
for ($i = 0; $i < 10; ++$i) {
$job = new TestJob();
$job->handle();
$jobQueue->push($job);
}
$awaiter = new Awaiter();
// Await completion of all jobs
// jobs = $awaiter->all($jobQueue);
// Await completion of any job
// $job = $awaiter->any($jobQueue);
// Await completion of a given amount of jobs
// $jobs = $awaiter->some($jobQueue, 2);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.