2014-03-31 41 views

回答

12

我找了一個教程,無法找到一個。最後,我使用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!" 
)); 

您的活動工作者和決策者的腳本可以放在任何服務器上。這些腳本都調用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" 
)); 
+0

因此,如何全面做SWF的工作適合你?推薦?看看我從哪裏來https://devops.stackexchange.com/a/3034/4997 – abbood