Some updates

This commit is contained in:
Netkas 2023-02-09 17:26:16 -05:00
parent 33bb2e5035
commit f82f1ca26a
7 changed files with 44 additions and 5 deletions

View file

@ -92,6 +92,7 @@
public function toArray(): array
{
return [
'type' => 'tamer_job',
'id' => $this->id,
'name' => $this->name,
'data' => ($this->closure ? serialize(new SerializableClosure($this->data)) : $this->data),

View file

@ -78,6 +78,7 @@
public function toArray(): array
{
return [
'type' => 'tamer_job_results',
'id' => $this->id,
'data' => $this->data,
'status' => $this->status

View file

@ -7,7 +7,9 @@
use Closure;
use Exception;
use PhpAmqpLib\Message\AMQPMessage;
use TamerLib\Abstracts\ObjectType;
use TamerLib\Classes\Functions;
use TamerLib\Classes\Validate;
use TamerLib\Exceptions\ConnectionException;
use TamerLib\Interfaces\ClientProtocolInterface;
use TamerLib\Objects\Job;
@ -326,6 +328,7 @@
$this->preformAutoreconf();
$correlationIds = [];
$connection = $this->connections[array_rand($this->connections)];
if($this->automatic_reconnect)
$connection->preformAutoreconf();
@ -348,12 +351,32 @@
// Register callback for each task
$callback = function($msg) use (&$correlationIds, $connection)
{
var_dump(Validate::getObjectType(msgpack_unpack($msg->body)));
if(Validate::getObjectType(msgpack_unpack($msg->body)) !== ObjectType::JobResults)
{
$connection->getChannel()->basic_nack($msg->delivery_info['delivery_tag'], false, true);
return;
}
$job_result = JobResults::fromArray(msgpack_unpack($msg->body));
$task = $this->getTaskById($job_result->getId());
if($task == null)
{
$connection->getChannel()->basic_nack($msg->delivery_info['delivery_tag'], false, true);
return;
}
try
{
$task->runCallback($job_result);
if($task->isClosure())
{
$task->runCallback($job_result->getData());
}
else
{
$task->runCallback($job_result);
}
}
catch(Exception $e)
{

View file

@ -8,6 +8,8 @@
use LogLib\Log;
use PhpAmqpLib\Message\AMQPMessage;
use TamerLib\Abstracts\JobStatus;
use TamerLib\Abstracts\ObjectType;
use TamerLib\Classes\Validate;
use TamerLib\Exceptions\ConnectionException;
use TamerLib\Interfaces\WorkerProtocolInterface;
use TamerLib\Objects\Job;
@ -69,6 +71,7 @@
public function __construct(?string $username = null, ?string $password = null)
{
$this->defined_servers = [];
$this->connections = [];
$this->functions = [];
$this->automatic_reconnect = true;
$this->username = $username;
@ -282,10 +285,17 @@
return;
// Select a random connection
$connection = $this->connections[array_rand($this->connections)];
$connection = $this->connections[array_rand($this->connections)];
$callback = function($message) use ($throw_errors, $connection)
{
var_dump(Validate::getObjectType(msgpack_unpack($message->body)));
if(Validate::getObjectType(msgpack_unpack($message->body)) !== ObjectType::Job)
{
$connection->getChannel()->basic_nack($message->delivery_info['delivery_tag']);
return;
}
$received_job = Job::fromArray(msgpack_unpack($message->body));
if($received_job->isClosure())
@ -364,6 +374,7 @@
{
break;
}
$connection->getChannel()->wait();
}
}