PHP code example of symbiote / silverstripe-queuedjobs
1. Go to this page and download the library: Download symbiote/silverstripe-queuedjobs 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/ */
symbiote / silverstripe-queuedjobs example snippets
use SilverStripe\ORM\FieldType\DBDatetime;
use Symbiote\QueuedJobs\Services\QueuedJobService;
$publish = new PublishItemsJob(21);
QueuedJobService::singleton()
->queueJob($publish, DBDatetime::create()->setValue(DBDatetime::now()->getTimestamp() + 86400)->Rfc2822());
namespace App\Jobs;
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
/**
* Class MyJob
*
* @property array $items
* @property array $remaining
*/
class MyJob extends AbstractQueuedJob
{
public function hydrate(array $items): void
{
$this->items = $items;
}
/**
* @return string
*/
public function getTitle(): string
{
return 'My awesome job';
}
public function setup(): void
{
$this->remaining = $this->items;
// Set the total steps to the number of items we want to process
$this->totalSteps = count($this->items);
}
public function process(): void
{
$remaining = $this->remaining;
// check for trivial case
if (count($remaining) === 0) {
$this->isComplete = true;
return;
}
$item = array_shift($remaining);
// code that will process your item goes here
$this->doSomethingWithTheItem($item);
$this->remaining = $remaining;
// Updating current step tells the Queue runner that the job is progressing
$this->currentStep += 1;
// check for job completion
if (count($remaining) > 0) {
// Note that we do not process more than one item at a time
// this makes the Queue runner save the job progress into DB
// in case something goes wrong the job will be resumed from the last checkpoint
return;
}
// Queue runner will mark this job as finished
$this->isComplete = true;
}
}