PHP code example of zotlo / phalcon-queue

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

    

zotlo / phalcon-queue example snippets


$di->register(new Phalcon\Queue\ServiceProvider());

$di->setShared('config', function () {
    return new \Phalcon\Config\Config([
        'queues'   => [
            'adapter'     => 'mysql',    # sqlite, redis, aws. very soon
            'dbIndex'     => 1,          # Redis Database Index (only redis) 
            'supervisors' => [
                [
                    'queue'           => 'default', # Queue Name
                    'balance'         => 'auto',    # Balance Strategy
                    'processes'       => 5,         # Maximum Process
                    'tries'           => 0,         # Job Maximum Tries
                    'timeout'         => 90,        # Job Timeout
                    'balanceMaxShift' => 5,         # Execute or Destory Process Count
                    'balanceCooldown' => 3,         # Check Process TTL (seconds)
                    'debug'           => false      # Debugging Worker
                ],
                [
                    'queue'           => 'another-queue',
                    'balance'         => 'simple',
                    'processes'       => 5,
                    'tries'           => 0,
                    'timeout'         => 90,
                    'balanceMaxShift' => 5,
                    'balanceCooldown' => 3,
                    'debug'           => false
                ]
            ]
        ]
    ]);
});



namespace App\Jobs;

use Phalcon\Queue\Jobs\Job;

class MyJob implements Job {

    public function handle(): void 
    {
        // your code
    }
    
}

dispatch(new MyJob())

dispatch(new MyJob())
    ->queue('default')

dispatch(new MyJob())
    ->queue('default')
    ->delay(10) # Delay TTL (seconds)

$jobArray = [
    new MyJob(),
    new MyJob(),
    ...
]

dispatchBatch($jobArray);

# Also, you can set queue with batch.
dispatchBatch($jobArray)
    ->queue('default');

async(function (){
    ...
});

# You can 'use' statement.
$uniqId = uniqid();

async(function () use ($uniqId){
    $taskId = $uniqId;
    ...
});

$job = async(function (){
    ...
});

// Your app codes

// Waits until the job succeeds or fails.
$status = await($job); // return ['failed','completed']

// Waits until the job succeeds or fails. If you want to manage all processes, send the 'manageable' value as 'true'.
$status = await($job, true); // return ['pending','processing','failed','completed']

# You can 'use' statement.
$variable = "initial";

async(function () use (&$variable){
    $variable = "changed";
});

echo $variable; # print "initial"

[program:phalcon-queue]
process_name = phalcon-queue
command = /usr/bin/php PHALCON_CLI_PATH/cli.php Queue run default
autostart = true
autorestart = true
user = root
numprocs = 1
stopsignal = SIGTERM
stopwaitsecs = 30
startretries = 3

[program:phalcon-queue]
process_name = another-queue
command = /usr/bin/php PHALCON_CLI_PATH/cli.php Queue run another-queue
autostart = true
autorestart = true
user = root
numprocs = 1
stopsignal = SIGTERM
stopwaitsecs = 30
startretries = 3