-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswf_sns_activities.rb
executable file
·94 lines (85 loc) · 3.1 KB
/
swf_sns_activities.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
##
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/apache2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
##
require_relative 'get_contact_activity.rb'
require_relative 'subscribe_topic_activity.rb'
require_relative 'wait_for_confirmation_activity.rb'
require_relative 'send_result_activity.rb'
require_relative 'transform_activity.rb'
# The **ActivitiesPoller** for the SWF/SNS sample. This handles launching the
# activities and responding to events on the activities task list.
class ActivitiesPoller
def initialize(domain, task_list)
@domain = domain
@task_list = task_list
@activities = {}
# These are the activities we'll run
activity_list = [
GetContactActivity,
SubscribeTopicActivity,
WaitForConfirmationActivity,
SendResultActivity,
TransformActivity ]
activity_list.each do | activity_class |
activity_obj = activity_class.new
puts "** initialized and registered activity: #{activity_obj.name}"
# add it to the hash
@activities[activity_obj.name.to_sym] = activity_obj
end
end
# Polls for activities until the activity is marked complete.
def poll_for_activities
@domain.activity_tasks.poll(@task_list) do | task |
activity_name = task.activity_type.name
# find the task on the activities list, and run it.
if @activities.key?(activity_name.to_sym)
activity = @activities[activity_name.to_sym]
puts "** Starting activity task: #{activity_name}"
if activity.do_activity(task)
puts "++ Activity task completed: #{activity_name}"
task.complete!({ :result => activity.results })
# if this is the final activity, stop polling.
if activity_name == 'transform_activity'
return true
end
else
puts "-- Activity task failed: #{activity_name}"
task.fail!(
{ :reason => activity.results[:reason],
:details => activity.results[:detail] } )
end
else
puts "couldn't find key in @activities list: #{activity_name}"
puts "contents: #{@activities.keys}"
end
end
end
end
# if the file was run from the command-line, instantiate the class and begin the
# activities
#
# Use a different task list name every time we start a new activities worker
# execution.
#
# This avoids issues if our pollers re-start before SWF considers them closed,
# causing the pollers to get events from previously-run executions.
if __FILE__ == $0
if ARGV.count < 1
puts "You must supply a task-list name to use!"
exit
end
poller = ActivitiesPoller.new(init_domain, ARGV[0])
poller.poll_for_activities
puts "All done!"
end