1. Go to this page and download the library: Download activecollab/jobsqueue 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/ */
activecollab / jobsqueue example snippets
use ActiveCollab\JobsQueue\Jobs\Job;
class Inc extends Job
{
/**
* Increment a number
*/
public function execute(): mixed
{
return $this->getData()['number'] + 1;
}
}
use ActiveCollab\JobsQueue\JobsDispatcher;
use ActiveCollab\JobsQueue\Queue\MySqlQueue;
use mysqli;
use RuntimeException;
$database_link = new MySQLi('localhost', 'root', '', 'activecollab_jobs_queue_test');
if ($database_link->connect_error) {
throw new RuntimeException('Failed to connect to database. MySQL said: ' . $database_link->connect_error);
}
$queue = new MySqlQueue($database_link);
// Not
public function execute(): mixed
{
print_r($this->getData()); // Print all job properties
print $this->getData('number') . "\n"; // Print only number
}
$batch = $dispatcher->batch('Testing batch', function(BatchInterface &$batch) {
for ($i = 1; $i <= 1000; $i++) {
$batch->dispatch(new Inc(['number' => $i]));
}
});
sleep(1);
print $batch->countJobs() . " jobs in a batch\n";
print $batch->countPendingJobs() . " batch jobs still pending for execution\n";
print $batch->countFailedJobs() . " batch jobs have failed to complete\n";
print $batch->countCompletedJobs() . " batch jobs were completed successfully\n";
$dispatcher->exceptionOnUnregisteredChannel(false);
// This job will end up in the 'main' channel, but exception will not be thrown
$dispatcher->execute(new Inc(['number' => 123]), 'unknown channel');
class ListAndForget extends Job
{
/**
* Report that we launched a background process
*/
public function execute(): mixed
{
$output = [];
exec("nohup ls -la > /dev/null 2>&1 & echo $!", $output);
$pid = (integer) $output[1];
if ($pid > 0) {
$this->reportBackgroundProcess($pid);
}
}
}