PHP code example of ozaretskii / php-snake-mqp

1. Go to this page and download the library: Download ozaretskii/php-snake-mqp 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/ */

    

ozaretskii / php-snake-mqp example snippets



// Require the Composer autoloader.
lQueueAdapter;
use Ozaretskii\PhpSnakeMqp\SnakeClient;

// Initiate queue adapter
$adapter = new MysqlQueueAdapter([
    'host' => 'localhost',
    'user' => 'root',
    'password' => 'root',
    'database' => 'test_database',
    'port' => '',
    'socket' => '',
]);
// create table if not existing yet (can be called just once per lifetime).
$adapter->prepare();
// Placing tasks into the queue
$hello = function ($arg1, $arg2) {
    var_dump($arg1);
    return $arg2;
};
$client = new SnakeClient($adapter);
// queueing closure
$client->addQueue($hello, ['function output', 'function result']);
/**
 * Worker:
 * - output: string(15) "function output"
 * - result: "function result"
 */
// queueing regular function
$client->addQueue('var_dump', ['text1', ['data' => 'array']]);
/** Worker:
 * - output: 
 * string(5) "text1"
 * array(1) {
 *   ["data"]=> string(5) "array"
 * } 
 * - result: null
 */


// Initiate queue adapter
$adapter = new MysqlQueueAdapter([
    'host' => 'localhost',
    'user' => 'root',
    'password' => 'root',
    'database' => 'test_database',
    'port' => '',
    'socket' => '',
]);
// process 10 jobs from the queue and print results
foreach ($client->processJobsInQueue(10) as $job) {
    if ($job->isSuccessful()) {
        var_dump("Job $job->id has finished.");
        var_dump("Results: ", $job->result);
        var_dump("Output: ", $job->printedOutput);
    } else {
        var_dump("Job $job->id has failed with an error.");
        var_dump("Error: ", $job->result);
        var_dump("Output: ", $job->printedOutput);
    }
}