承接 thinkfluent/fanfan-client-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thinkfluent/fanfan-client-php

最新稳定版本:v1.1.1

Composer 安装命令:

composer require thinkfluent/fanfan-client-php

包简介

PHP Client for FanFan - a Serverless Fan-out, Fan-in Framework for Google Cloud Pub/Sub

README 文档

README

CI Tests

For more detail on FanFan, see the main project here: thinkfluent/fanfan

If you want to see a working demo application, see this project: thinkfluent/fanfan-demo-php

Installation

composer require thinkfluent/fanfan-client-php

Usage

I recommend you review & understand the message & workflow concepts first here: thinkfluent/fanfan

Create a Fan-out Job

Note: The fanfan-client-php library does not require or operate on the the Google Pub/Sub client package. You will need to include that yourself, and emit messages to it.

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());

Execute a Single Fanned-out Task & Emit Response

Using a simple "HTTP Push" subscription. Your application will receive 1-many requests like this

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());

Receive Job Outcome

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;

统计信息

  • 总下载量: 8.75k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-10