Skip to content

Commit

Permalink
Fix inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
michielkempen committed Jul 25, 2023
1 parent cf8bceb commit 7b6fec0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 42 deletions.
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ You can interact with your Shipmate job queues as follows:
use Shipmate\Shipmate\JobQueue\JobQueue;
use Shipmate\Shipmate\JobQueue\Job;

$jobQueue = new JobQueue;
$jobQueue = new JobQueue(
name: getenv('SHIPMATE_JOB_QUEUE_NAME'),
workerUrl: getenv('SHIPMATE_JOB_QUEUE_WORKER_URL'),
);

// publish a job

Expand All @@ -32,17 +35,13 @@ $job = new Job(
],
)

$jobQueue->publishJob(
queueName: getenv('SHIPMATE_JOB_QUEUE_NAME'),
queueWorkerUrl: getenv('SHIPMATE_JOB_QUEUE_WORKER_URL'),
job: $job
);
$jobQueue->publishJob($job);

// handle a job

$requestContents = $httpRequest->getContents();

$job = $jobQueue->parseJob($requestContents);
$job = JobQueue::parseJob($requestContents);

$job->payload['action'];
$job->payload['data']['first_name'];
Expand All @@ -57,7 +56,9 @@ You can interact with your Shipmate message queues as follows:
use Shipmate\Shipmate\MessageQueue\MessageQueue;
use Shipmate\Shipmate\MessageQueue\Message;

$messageQueue = new MessageQueue;
$messageQueue = new MessageQueue(
name: getenv('SHIPMATE_MESSAGE_QUEUE_NAME'),
);

// publish a message

Expand All @@ -69,16 +70,13 @@ $message = new Message(
],
)

$messageQueue->publishMessage(
queueName: getenv('SHIPMATE_MESSAGE_QUEUE_NAME'),
message: $message,
);
$messageQueue->publishMessage($message);

// handle a message

$requestContents = $httpRequest->getContents();

$message = $messageQueue->parseMessage($requestContents);
$message = MessageQueue::parseMessage($requestContents);

$message->type;
$message->payload['first_name'];
Expand Down
34 changes: 18 additions & 16 deletions src/JobQueue/JobQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class JobQueue
private ShipmateConfig $shipmateConfig;
private CloudTasksClient $client;

public function __construct()
{
public function __construct(
private string $name,
private string $workerUrl,
) {
$this->shipmateConfig = new ShipmateConfig;

$this->client = new CloudTasksClient([
Expand All @@ -27,10 +29,21 @@ public function __construct()
]);
}

public function publishJob(string $queueName, string $queueWorkerUrl, Job $job, int $availableAt = null): void
public static function parseJob(string $requestPayload): Job
{
try {
return new Job(
payload: base64_decode(json_decode($requestPayload, true))
);
} catch (Exception) {
throw new UnableToParseJob;
}
}

public function publishJob(Job $job, int $availableAt = null): void
{
$httpRequest = new HttpRequest;
$httpRequest->setUrl($queueWorkerUrl);
$httpRequest->setUrl($this->workerUrl);
$httpRequest->setHttpMethod(HttpMethod::POST);
$httpRequest->setBody(base64_encode(json_encode($job->payload)));

Expand All @@ -48,23 +61,12 @@ public function publishJob(string $queueName, string $queueWorkerUrl, Job $job,
$fullyQualifiedQueueName = $this->client->queueName(
project: $this->shipmateConfig->getEnvironmentId(),
location: $this->shipmateConfig->getRegionId(),
queue: $queueName,
queue: $this->name,
);

$this->client->createTask($fullyQualifiedQueueName, $task);
}

public function parseJob(string $requestPayload): Job
{
try {
return new Job(
payload: base64_decode(json_decode($requestPayload, true))
);
} catch (Exception) {
throw new UnableToParseJob;
}
}

public function getGoogleClient(): CloudTasksClient
{
return $this->client;
Expand Down
27 changes: 14 additions & 13 deletions src/MessageQueue/MessageQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class MessageQueue
{
private PubSubClient $client;

public function __construct()
{
public function __construct(
private string $name
) {
$shipmateConfig = new ShipmateConfig;

$this->client = new PubSubClient([
Expand All @@ -21,17 +22,7 @@ public function __construct()
]);
}

public function publishMessage(string $queueName, Message $message): void
{
$this->client->topic($queueName)->publish([
'data' => json_encode($message->payload),
'attributes' => [
'type' => $message->type,
],
]);
}

public function parseMessage(string $requestPayload): Message
public static function parseMessage(string $requestPayload): Message
{
try {
$message = json_decode($requestPayload, true);
Expand All @@ -46,6 +37,16 @@ public function parseMessage(string $requestPayload): Message
}
}

public function publishMessage(Message $message): void
{
$this->client->topic($this->name)->publish([
'data' => json_encode($message->payload),
'attributes' => [
'type' => $message->type,
],
]);
}

public function getGoogleClient(): PubSubClient
{
return $this->client;
Expand Down

0 comments on commit 7b6fec0

Please sign in to comment.