thinkfluent/fanfan-client-php

FanFan 的 PHP 客户端 - 一个适用于 Google Cloud Pub/Sub 的无服务器 Fan-out, Fan-in 框架

v1.1.0 2023-08-26 16:42 UTC

This package is auto-updated.

Last update: 2024-09-26 19:25:48 UTC


README

CI Tests

有关 FanFan 的更多详细信息,请查看主项目: thinkfluent/fanfan

如果您想查看一个工作演示应用,请查看此项目: thinkfluent/fanfan-demo-php

安装

composer require thinkfluent/fanfan-client-php

用法

我建议您首先在此处审查并理解消息和工作流概念: thinkfluent/fanfan

创建 Fan-out 任务

注意:fanfan-client-php 库不需要或操作 Google Pub/Sub 客户端包。您需要自己包含它,并向其发送消息。

use FanFan\Client\Message\JobRequest;
use Google\Cloud\PubSub\PubSubClient;

// Prepare a JobRequest
$jobRequest = (new JobRequest())
    ->action('process_order')
    ->forEach('order', [4, 2, 19, 79]);

// Send to FanFan (via Pub/Sub)
$jobRequestTopic = (new PubSubClient())->topic('projects/get-fanfan/topics/fanfan-job-request');
$jobRequestTopic->publish($jobRequest->formatForPubSub());

执行单个分发任务并发送响应

使用简单的 "HTTP 推送" 订阅。您的应用程序将接收到类似这样的 1-多个请求

use FanFan\Client\Message\Builder\TaskBuilder;
use Google\Cloud\PubSub\PubSubClient;

// Grab the Task data from the POST body (most of this is extracting the base64 message from the Pub/Sub payload)
// Your framework of choice may well have tooling to support you with this (Like PSR-7 Requests)
$payload = \json_decode(\file_get_contents('php://input'), false, 512, JSON_THROW_ON_ERROR);
$messageData = \json_decode(\base64_decode($payload->message->data), false, 512, JSON_THROW_ON_ERROR);

// Build `FanFan\Client\Message\Task`, contains instructions & payload for ONE fanned-out task
$task = TaskBuilder::fromPubSub($messageData);

// ...do work here...
// e.g. switch on $task->getAction(), use the data from $task->getPayload();

// Create & publish the Task outcome
$outcome = $task->createOutcome(TaskStatus::SUCCEEDED);
$jobRequestTopic = (new PubSubClient())->topic('projects/get-fanfan/topics/fanfan-task-done');
$jobRequestTopic->publish($outcome->formatForPubSub());

接收作业结果

use FanFan\Client\Message\Builder\JobOutcomeBuilder;
// Extract Pub/Sub message into `$messageData` as per above example
$payload = \json_decode(\file_get_contents('php://input'), false, 512, JSON_THROW_ON_ERROR);
$messageData = \json_decode(\base64_decode($payload->message->data), false, 512, JSON_THROW_ON_ERROR);

// Build `FanFan\Client\Message\JobOutcome`
$outcome = JobOutcomeBuilder::fromPubSub($messageData);

// Work with the output
echo $outcome->getStatus(), PHP_EOL;
echo $outcome->getTaskCounts()['SUCCEEDED'], PHP_EOL;
echo $outcome->getTaskCounts()['FAILED'], PHP_EOL;