Implemented RabbitMQ and Refactored Client & Worker (WIP)

This commit is contained in:
Netkas 2023-02-02 21:09:45 -05:00
parent d15da30813
commit 6d8d4a75a4
13 changed files with 966 additions and 170 deletions

View file

@ -2,23 +2,23 @@
require 'ncc';
use Tamer\Objects\JobResults;
use Tamer\Objects\Task;
use Tamer\Objects\JobResults;
use Tamer\Objects\Task;
import('net.nosial.tamerlib', 'latest');
import('net.nosial.tamerlib', 'latest');
$client = new \Tamer\Protocols\GearmanClient();
$client = new \Tamer\Protocols\Gearman\Client();
$client->addServer();
$client->doBackground(new Task('sleep', '5'));
$client->do(new Task('sleep', '5'));
$client->addTask(new Task('sleep', '5', function(JobResults $job) {
$client->queue(new Task('sleep', '5', function(JobResults $job) {
echo "Task {$job->getId()} completed with data: {$job->getData()} \n";
}));
$client->addTask(new Task('sleep', '5', function(JobResults $job) {
$client->queue(new Task('sleep', '5', function(JobResults $job) {
echo "Task {$job->getId()} completed with data: {$job->getData()} \n";
}));

View file

@ -2,14 +2,14 @@
require 'ncc';
use Tamer\Objects\JobResults;
use Tamer\Objects\Task;
import('net.nosial.tamerlib', 'latest');
import('net.nosial.tamerlib', 'latest');
$client = new \Tamer\Protocols\GearmanClient();
$client = new \Tamer\Protocols\Gearman\Client();
$client->addServer();
$client->closure(function () {
echo "This function was sent from a client, it should be executed on the worker";
$client->doClosure(function () {
require 'ncc';
import('net.nosial.loglib', 'latest');
\LogLib\Log::info('gearman_closure.php', 'closure');
});

View file

@ -2,10 +2,10 @@
require 'ncc';
use Tamer\Objects\Job;
use Tamer\Objects\Job;
import('net.nosial.tamerlib', 'latest');
$worker = new \Tamer\Protocols\GearmanWorker();
import('net.nosial.tamerlib', 'latest');
$worker = new \Tamer\Protocols\Gearman\Worker();
$worker->addServer();
$worker->addFunction('sleep', function($job) {

18
tests/rabbitmq_client.php Normal file
View file

@ -0,0 +1,18 @@
<?php
use Tamer\Objects\Task;
require 'ncc';
import('net.nosial.tamerlib', 'latest');
$client = new \Tamer\Protocols\RabbitMq\Client('guest', 'guest');
$client->addServer('127.0.0.1', 5672);
// Loop through 10 tasks
for($i = 0; $i < 500; $i++)
{
$client->do(new Task('sleep', '5'));
}

27
tests/rabbitmq_worker.php Normal file
View file

@ -0,0 +1,27 @@
<?php
require 'ncc';
use Tamer\Objects\Job;
import('net.nosial.tamerlib', 'latest');
$worker = new \Tamer\Protocols\RabbitMq\Worker('guest', 'guest');
$worker->addServer('127.0.0.1', 5672);
$worker->addFunction('sleep', function($job) {
/** @var Job $job */
var_dump(get_class($job));
echo "Task {$job->getId()} started with data: {$job->getData()} \n";
sleep($job->getData());
echo "Task {$job->getId()} completed with data: {$job->getData()} \n";
return $job->getData();
});
while(true)
{
echo "Waiting for job... \n";
$worker->work();
}