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;
}
}