PHP code example of ignitor / queues

1. Go to this page and download the library: Download ignitor/queues 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/ */

    

ignitor / queues example snippets


// In your method
public function testQueue()
{
    toQueue(function () {
        sleep(10);
        // log to file or send an email
    });

    return "Request is being Processed";
}



namespace App\Jobs;

use Igniter\Queues\Queue\DispatchableTrait;
// use Igniter\Queues\Queue\IsEncryptedInterface;
use Igniter\Queues\Queue\ShouldQueueInterface;

class TestJob implements ShouldQueueInterface
{
    use DispatchableTrait;

    /**
     * The queue to run the job on.
     *
     * @var string
     */
    public string $queue = 'default';

    /**
     * Delay the job by a given amount of seconds.
     *
     * @param int $delay
     *
     */
    public int $delay = 0;

    /**
     * Delay the job by a given amount of seconds.
     *
     * @param string $delayType
     *
     */
    public string $delayType = 'minutes';

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        //
        $this->data = $data;
    }

    /**
     * Run the job.
     *
     * @return void
     */
    public function run()
    {
        // log to file or send an email, etc.
    }
}

public function testQueue()
{
    TestJob::dispatch();

    return "Request is being Processed";
}

public function testQueue()
{
    toQueue(function ($data) {
        sleep(10);
        // log to file or send an email
    }, ['data' => 'some data']);

    return "Request is being Processed";
}

TestJob::dispatch('some data');

TestJob::dispatch('some data', 'more data', 'even more data');

public function testQueue()
{
    toQueue(function () {
        sleep(10);
        // log to file or send an email
    }, delay: 10);

    return "Request is being Processed";
}



namespace App\Jobs;

use Igniter\Queues\Queue\DispatchableTrait;
use Igniter\Queues\Queue\IsEncryptedInterface;
use Igniter\Queues\Queue\ShouldQueueInterface;

class TestJob implements ShouldQueueInterface, IsEncryptedInterface
{
    use DispatchableTrait;

    /**
     * The queue to run the job on.
     *
     * @var string
     */
    public string $queue = 'default';

    /**
     * Delay the job by a given amount of seconds.
     *
     * @param int $delay
     *
     */
    public int $delay = 0;

    /**
     * Delay the job by a given amount of seconds.
     *
     * @param string $delayType
     *
     */
    public string $delayType = 'minutes';

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        //
        $this->data = $data;
    }

    /**
     * Run the job.
     *
     * @return void
     */
    public function run()
    {
        // log to file or send an email, etc.
    }
}

public function testQueue()
{
    toQueue(function () {
        sleep(10);
        // log to file or send an email
    }, queue: 'emails');

    return "Request is being Processed";
}



namespace App\Jobs;

use Igniter\Queues\Queue\DispatchableTrait;
use Igniter\Queues\Queue\ShouldQueueInterface;
use Igniter\Queues\Queue\CanFailInterface;

class TestJob implements ShouldQueueInterface, CanFailInterface
{
    use DispatchableTrait;

    /**
     * The queue to run the job on.
     *
     * @var string
     */
    public string $queue = 'default';

    /**
     * Delay the job by a given amount of seconds.
     *
     * @param int $delay
     *
     */
    public int $delay = 0;

    /**
     * Delay the job by a given amount of seconds.
     *
     * @param string $delayType
     *
     */
    public string $delayType = 'minutes';

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        //
        $this->data = $data;
    }

    /**
     * Run the job.
     *
     * @return void
     */
    public function run()
    {
        // log to file or send an email, etc.
    }

    /**
     * Triggered when this job fails for whatever reason and only if this job is queueable
     * 
     * @param stdClass $job
     * @param string $message
     */
    public function onFailure($job, $message)
    {
        if ($job->attempts >= 3) { // It totally failed
            $email = service('email');
            $email->setTo('[email protected]');
            $email->setSubject('Some Subject');
            $email->setMessage('Some Message: job-id(' . $job->id . ') <br /> Data:' . json_encode($this->data) . ' with messege: ' . $message);
            $email->send();
        }
    }
}

public function testQueue()
{
    toQueue(
        callback: function ($data) {
            sleep(10);
            // log to file or send an email
        },
        data: ['data' => 'some data'],
        onFailure: function ($job, $message) {
            if ($job->attempts >= 3) { // It totally failed
                $email = service('email');
                $email->setTo('[email protected]');
                $email->setSubject('Some Subject');
                $email->setMessage('Some Message: job-id(' . $job->id . ') <br /> Data:' . json_encode($data) . ' with message: ' . $message);
                $email->send();
            }
        }
    );

    return "Request is being Processed";
}
bash
composer ueue:table

php spark migrate
bash
php spark queue:work
bash
php spark make:job TestJob
bash
php spark queue:work --workers 2
bash
php spark queue:work

php spark queue:work

php spark queue:work

php spark queue:work

php spark queue:work
bash
php spark queue:work --retry 5
bash
php spark queue:work --restart
bash
php spark queue:table
bash
php spark queue:work --queue emails
bash
*/5 * * * * cd /var/www/html/your-project && php spark queue:work
bash
*/5 * * * * cd /var/www/html/your-project && php spark queue:work --queue emails
bash
*/5 * * * * cd /var/www/html/your-project && php spark queue:work --workers 4