311 lines
No EOL
7.2 KiB
PHP
311 lines
No EOL
7.2 KiB
PHP
<?php
|
|
|
|
/** @noinspection PhpMissingFieldTypeInspection */
|
|
|
|
namespace TamerLib\Objects;
|
|
|
|
use Exception;
|
|
use TamerLib\Enums\JobStatus;
|
|
use TamerLib\Enums\JobType;
|
|
|
|
class JobPacket
|
|
{
|
|
/**
|
|
* @var int
|
|
*/
|
|
private $id;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private $channel;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private $job_type;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $status;
|
|
|
|
/**
|
|
* @var string|null
|
|
*/
|
|
private $worker_id;
|
|
|
|
/**
|
|
* @var mixed|null
|
|
*/
|
|
private $parameters;
|
|
|
|
/**
|
|
* @var mixed|null
|
|
*/
|
|
private $payload;
|
|
|
|
/**
|
|
* @var mixed|null
|
|
*/
|
|
private $return_value;
|
|
|
|
/**
|
|
* @var string|null
|
|
*/
|
|
private $exception;
|
|
|
|
/**
|
|
* @var string|null
|
|
*/
|
|
private $return_channel;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private $created_at;
|
|
|
|
/**
|
|
* @param array $data
|
|
* @throws Exception
|
|
*/
|
|
public function __construct(array $data=[])
|
|
{
|
|
$this->id = $data['id'] ?? random_int(1000000000, 9999999999);
|
|
$this->channel = $data['channel'] ?? 0;
|
|
$this->job_type = $data['job_type'] ?? JobType::CLOSURE;
|
|
$this->status = $data['status'] ?? JobStatus::WAITING;
|
|
$this->worker_id = $data['worker_id'] ?? null;
|
|
$this->parameters = $data['parameters'] ?? null;
|
|
$this->payload = $data['payload'] ?? null;
|
|
$this->return_value = $data['return_value'] ?? null;
|
|
$this->exception = $data['exception'] ?? null;
|
|
$this->return_channel = $data['return_channel'] ?? null;
|
|
$this->created_at = $data['created_at'] ?? time();
|
|
}
|
|
|
|
/**
|
|
* Returns the JobPacket's ID
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getId(): int
|
|
{
|
|
return (int)$this->id;
|
|
}
|
|
|
|
/**
|
|
* Sets the ID of the JobPacket, if null is passed, a random ID will be generated
|
|
*
|
|
* @param int|null $id
|
|
* @throws Exception
|
|
*/
|
|
public function setId(?int $id=null): void
|
|
{
|
|
if($id !== null)
|
|
{
|
|
$this->id = random_int(1000000000, 9999999999);
|
|
return;
|
|
}
|
|
|
|
$this->id = (int)$id;
|
|
}
|
|
|
|
/**
|
|
* Returns the channel the JobPacket is assigned to
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getChannel(): int
|
|
{
|
|
return (int)$this->channel;
|
|
}
|
|
|
|
/**
|
|
* Sets the channel the JobPacket is assigned to, if null is passed, the channel will be set to 0
|
|
* 0 is the default channel and is used for all jobs that are not assigned to a specific channel
|
|
*
|
|
* @param int|null $channel
|
|
* @noinspection PhpCastIsUnnecessaryInspection
|
|
* @noinspection UnnecessaryCastingInspection
|
|
*/
|
|
public function setChannel(?int $channel=null): void
|
|
{
|
|
if($channel === null)
|
|
{
|
|
$this->channel = 0;
|
|
return;
|
|
}
|
|
|
|
$this->channel = (int)$channel;
|
|
}
|
|
|
|
/**
|
|
* Returns the Job Type of the JobPacket
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getJobType(): int
|
|
{
|
|
return (int)$this->job_type;
|
|
}
|
|
|
|
/**
|
|
* @param int $job_type
|
|
*/
|
|
public function setJobType(int $job_type): void
|
|
{
|
|
$this->job_type = $job_type;
|
|
}
|
|
|
|
/**
|
|
* @return int
|
|
*/
|
|
public function getStatus(): int
|
|
{
|
|
return (int)$this->status;
|
|
}
|
|
|
|
/**
|
|
* @param int $status
|
|
*/
|
|
public function setStatus(int $status): void
|
|
{
|
|
$this->status = (int)$status;
|
|
}
|
|
|
|
/**
|
|
* @return string|null
|
|
*/
|
|
public function getWorkerId(): ?string
|
|
{
|
|
return (string)$this->worker_id;
|
|
}
|
|
|
|
/**
|
|
* @param string|null $worker_id
|
|
*/
|
|
public function setWorkerId(?string $worker_id): void
|
|
{
|
|
$this->worker_id = $worker_id;
|
|
}
|
|
|
|
/**
|
|
* @return mixed|null
|
|
*/
|
|
public function getParameters(): mixed
|
|
{
|
|
return $this->parameters;
|
|
}
|
|
|
|
/**
|
|
* @param mixed|null $parameters
|
|
*/
|
|
public function setParameters(mixed $parameters): void
|
|
{
|
|
$this->parameters = $parameters;
|
|
}
|
|
|
|
/**
|
|
* @return mixed|null
|
|
*/
|
|
public function getPayload(): mixed
|
|
{
|
|
return $this->payload;
|
|
}
|
|
|
|
/**
|
|
* @param mixed|null $payload
|
|
*/
|
|
public function setPayload(mixed $payload): void
|
|
{
|
|
$this->payload = $payload;
|
|
}
|
|
|
|
/**
|
|
* @return mixed|null
|
|
*/
|
|
public function getReturnValue(): mixed
|
|
{
|
|
return $this->return_value;
|
|
}
|
|
|
|
/**
|
|
* @param mixed|null $return_value
|
|
*/
|
|
public function setReturnValue(mixed $return_value): void
|
|
{
|
|
$this->return_value = $return_value;
|
|
}
|
|
|
|
/**
|
|
* @return string|null
|
|
*/
|
|
public function getException(): ?string
|
|
{
|
|
return $this->exception;
|
|
}
|
|
|
|
/**
|
|
* @param string|null $exception
|
|
*/
|
|
public function setException(?string $exception): void
|
|
{
|
|
$this->exception = $exception;
|
|
}
|
|
|
|
/**
|
|
* @return string|null
|
|
*/
|
|
public function getReturnChannel(): ?string
|
|
{
|
|
return $this->return_channel;
|
|
}
|
|
|
|
/**
|
|
* @param string|null $return_channel
|
|
*/
|
|
public function setReturnChannel(?string $return_channel): void
|
|
{
|
|
$this->return_channel = $return_channel;
|
|
}
|
|
|
|
|
|
/**
|
|
* @return int|mixed
|
|
*/
|
|
public function getCreatedAt(): mixed
|
|
{
|
|
return $this->created_at;
|
|
}
|
|
|
|
/**
|
|
* @param int|mixed $created_at
|
|
*/
|
|
public function setCreatedAt(mixed $created_at): void
|
|
{
|
|
$this->created_at = $created_at;
|
|
}
|
|
|
|
/**
|
|
* Returns an array representation of the JobPacket
|
|
*
|
|
* @return array
|
|
*/
|
|
public function toArray(): array
|
|
{
|
|
return [
|
|
'id' => $this->getId(),
|
|
'channel' => $this->getChannel(),
|
|
'job_type' => $this->getJobType(),
|
|
'status' => $this->getStatus(),
|
|
'worker_id' => $this->getWorkerId(),
|
|
'parameters' => $this->getParameters(),
|
|
'payload' => $this->getPayload(),
|
|
'return_value' => $this->getReturnValue(),
|
|
'exception' => $this->getException(),
|
|
'return_channel' => $this->getReturnChannel(),
|
|
'created_at' => $this->getCreatedAt()
|
|
];
|
|
}
|
|
} |