PHP code example of kozhemin / yii2-background-task-queue

1. Go to this page and download the library: Download kozhemin/yii2-background-task-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/ */

    

kozhemin / yii2-background-task-queue example snippets


use kozhemin\backgroundTaskQueue\TaskPublisher;

$task = new MySimpleTask();
$taskPublisher = new TaskPublisher($task);
$queue = $taskPublisher
    ->setDescription('My simple task')
    ->setDelay(60)
    ->publish();

    if ($queue->hasErrors()) {
        echo "\nTask not created. Errors: " . implode(', ', $queue->getFirstErrors()) . "\n";
        return ExitCode::UNSPECIFIED_ERROR;
    }

    echo "\nTask created with id: " . $queue->uuid . "\n";


use kozhemin\backgroundTaskQueue\contracts\HandlerInterface;
use kozhemin\backgroundTaskQueue\contracts\TaskResponseInterface;
use kozhemin\backgroundTaskQueue\TaskResponse;

class MySimpleTask implements HandlerInterface
{
    public function run(): TaskResponseInterface
    {
        // some code here
        echo "Start task\n";
        sleep(3);
        echo "End task\n";

        $response = new TaskResponse();
        $response->setMessage(['firstResponseMsg' => 'Hello from task1!']);
        $response->appendMessage(['secondResponseMsg' => 'Hello from task2!']);

        return $response;
    }
}

bash
php yii migrate --migrationPath=@vendor/kozhemin/yii2-background-task-queue/migrations

bash
# run task every minute
* * * * * php yii task-queue/run

# run task every 30 seconds
* * * * * php yii task-queue/run
* * * * * sleep 30 && php yii task-queue/run

# run every 10 seconds
* * * * * php yii task-queue/run
* * * * * sleep 10 && php yii task-queue/run
* * * * * sleep 20 && php yii task-queue/run
* * * * * sleep 30 && php yii task-queue/run
* * * * * sleep 40 && php yii task-queue/run
* * * * * sleep 50 && php yii task-queue/run

bash
# run listen task real time
watch -n 1 php yii task-queue/ls

bash
# task list
php yii task-queue/ls --status=0,1,2,3
bash
# task info
php yii task-queue/info 67895e8b58fbb