gpt4 book ai didi

php - 亚马逊 AWS 简单工作流服务 SWF PHP 示例

转载 作者:可可西里 更新时间:2023-11-01 13:10:13 24 4
gpt4 key购买 nike

我想知道是否有适用于 AWS PHPSDK 2+ 的 SWF 工作流 PHP 示例代码?

最佳答案

我寻找教程,但没有找到。最后,我浏览了使用 Ruby 和 Web API 的文档和示例,并拼凑了使用 PHP SDK 的具体细节。

您需要做的第一件事是注册您的域、工作流和事件。这可以通过 AWS 控制台或使用 PHP SDK 完成。使用 SDK,使用以下内容:

<?php

require_once "path/to/aws.phar";

use Aws\Swf\SwfClient;

// Create an instance of the SWF class
$client = SwfClient::factory(array(
"key" => "your_aws_key",
"secret" => "your_aws_secret_key",
"region" => "your_aws_region"
));

// Register your domain
$client->registerDomain(array(
"name" => "domain name you want",
"description" => "this is a test domain",
"workflowExecutionRetentionPeriodInDays" => "7"
));

// Register your workflow
$client->registerWorkflowType(array(
"domain" => "domain name you registered in previous call",
"name" => "workflow name you want",
"version" => "1.0",
"description" => "this is a sample",
"defaultTaskList" => array(
"name" => "mainTaskList"
),
"defaultChildPolicy" => "TERMINATE"
));

// Register an activity
$client->registerActivityType(array(
"domain" => "domain name you registered above",
"name" => "activity name you want",
"version" => "1.0",
"description" => "first activity in our workflow",
"defaultTaskList" => array(
"name" => "mainTaskList"
)
));

// Follow activity registration example above and register
// more activities as you wish

下一步是创建决策程序。这是充当事件(工作)节点的协调节点的脚本。

// Ask SWF for things the decider needs to know
$result = $client->pollForDecisionTask(array(
"domain" => "your domain name",
"taskList" => array(
"name" => "mainTaskList"
),
"identify" => "default",
"maximumPageSize" => 50,
"reverseOrder" => true
));

// Current version of activity types we are using
$activity_type_version = "1.0";

// Parse info we need returned from the pollForDecisionTask call
$task_token = $result["taskToken"];
$workflow_id = $result["workflowExecution"]["workflowId"];
$run_id = $result["workflowExecution"]["runId"];
$last_event = $result["events"][0]["eventId"];

// Our logic that decides what happens next
if($last_event == "3"){
$activity_type_name = "activity to start if last event ID was 3";
$task_list = "mainTaskList";
$activity_id = "1";
$continue_workflow = true;
}
elseif($last_event == "8"){
$activity_type_name = "activity to start if last event ID was 8";
$task_list = "mainTaskList";
$activity_id = "2";
$continue_workflow = false;
}

// Now that we populated our variables based on what we received
// from SWF, we need to tell SWF what we want to do next
if($continue_workflow === true){
$client->respondDecisionTaskCompleted(array(
"taskToken" => $task_token,
"decisions" => array(
array(
"decisionType" => "ScheduleActivityTask",
"scheduleActivityTaskDecisionAttributes" => array(
"activityType" => array(
"name" => $activity_type_name,
"version" => $activity_type_version
),
"activityId" => $activity_id,
"control" => "this is a sample message",
// Customize timeout values
"scheduleToCloseTimeout" => "360",
"scheduleToStartTimeout" => "300",
"startToCloseTimeout" => "60",
"heartbeatTimeout" => "60",
"taskList" => array(
"name" => $task_list
),
"input" => "this is a sample message"
)
)
)
));
}
// End workflow if last event ID was 8
else if($continue_workflow === false){
$client->respondDecisionTaskCompleted(array(
"taskToken" => $task_token,
"decisions" => array(
array(
"decisionType" => "CompleteWorkflowExecution"
)
)
));
}

最后一步是创建您的事件工作人员。您可以使用以下格式旋转它们:

// Check with SWF for activities
$result = $client->pollForActivityTask(array(
"domain" => "domain name you registered",
"taskList" => array(
"name" => "mainTaskList"
)
));

// Take out task token from the response above
$task_token = $result["taskToken"];

// Do things on the computer that this script is saved on
exec("my program i want to execute");

// Tell SWF that we finished what we need to do on this node
$client->respondActivityTaskCompleted(array(
"taskToken" => $task_token,
"result" => "I've finished!"
));

您的事件 worker 和决策者的脚本可以放在任何服务器上。这些脚本都调用 SWF 以便相互通信。

最后,要启动您刚刚创建的工作流程,请使用:

// Generate a random workflow ID
$workflowId = mt_rand(1000, 9999);

// Starts a new instance of our workflow
$client->startWorkflowExecution(array(
"domain" => "your domain name",
"workflowId" => $workflowId,
"workflowType" => array(
"name" => "your workflow name",
"version" => "1.0"
),
"taskList" => array(
"name" => "mainTaskList"
),
"input" => "a message goes here",
"executionStartToCloseTimeout" => "300",
'taskStartToCloseTimeout' => "300",
"childPolicy" => "TERMINATE"
));

关于php - 亚马逊 AWS 简单工作流服务 SWF PHP 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22765377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com