Class: AWS::SimpleWorkflow::DecisionTaskCollection
- Inherits:
-
Object
- Object
- AWS::SimpleWorkflow::DecisionTaskCollection
- Includes:
- Core::Model, OptionFormatters
- Defined in:
- lib/aws/simple_workflow/decision_task_collection.rb
Overview
Provides an interface to count, and receive decision tasks (DecisionTask objects) from the service.
Counting
To get a count of decision tasks needing attention, call #count with a task list name:
domain.decision_tasks.count('my-task-list').to_i #=> 7
Getting a single decision task
To process a single task use #poll_for_single_task:
domain.decision_tasks.poll_for_single_task('my-task-list') do |task|
# this block is yielded to only if a task is found
# within 60 seconds.
end
At the end of the block, the decision task is auto-completed. If you prefer you can omit the block and nil
or a DecisionTask will be returned.
if task = domain.decision_tasks.poll_for_single_task('my-task-list')
# you need to call complete! or the decision task will time out
task.complete!
end
Polling for Tasks in a Loop
You can poll indefinetly for tasks in a loop with #poll:
domain.decision_tasks.poll('my-task-list') do |task|
# yields once for every decision task found
end
Just like the block form above, the decision task is auto completed at the end of the block. Please note, if you call break
or return
from inside the block, you MUST call AWS::SimpleWorkflow::DecisionTask#complete! or the task will timeout.
Events and Decisions
Each decision task provides an enumerable collection of both new events (AWS::SimpleWorkflow::DecisionTask#new_events) and all events (AWS::SimpleWorkflow::DecisionTask#events).
Based on the events in the workflow execution history, you should call methods on the decision task. See DecisionTask for a complete list of decision methods.
Instance Attribute Summary collapse
- #domain ⇒ Domain readonly
Attributes included from Core::Model
Instance Method Summary collapse
-
#count(task_list) ⇒ Count
Returns the number of decision tasks in the specified
task_list
. -
#initialize(domain, options = {}) ⇒ DecisionTaskCollection
constructor
A new instance of DecisionTaskCollection.
-
#poll(task_list, options = {}) {|decision_task| ... } ⇒ nil
Polls indefinetly for decision tasks.
-
#poll_for_single_task(task_list, options = {}) {|decision_task| ... } ⇒ DecisionTask?
Polls the service for a single decision task.
Methods included from Core::Model
#client, #config_prefix, #inspect
Constructor Details
#initialize(domain, options = {}) ⇒ DecisionTaskCollection
Returns a new instance of DecisionTaskCollection.
78 79 80 81 |
# File 'lib/aws/simple_workflow/decision_task_collection.rb', line 78 def initialize domain, = {} @domain = domain super end |
Instance Attribute Details
#domain ⇒ Domain (readonly)
84 85 86 |
# File 'lib/aws/simple_workflow/decision_task_collection.rb', line 84 def domain @domain end |
Instance Method Details
#count(task_list) ⇒ Count
This operation is eventually consistent. The results are best effort and may not exactly reflect recent updates and changes.
Returns the number of decision tasks in the specified task_list
.
count = decision_tasks.count('task-list-name')
count.truncated? #=> false
count.to_i #=> 7
102 103 104 105 106 107 108 |
# File 'lib/aws/simple_workflow/decision_task_collection.rb', line 102 def count task_list = {} [:domain] = domain.name [:task_list] = { :name => task_list } response = client.count_pending_decision_tasks() Count.new(response.data['count'], response.data['truncated']) end |
#poll(task_list, options = {}) {|decision_task| ... } ⇒ nil
If you to terminate the block (by calling break
or return
) then it is your responsibility to call #complete! on the decision task.
Polls indefinetly for decision tasks. Each deicsion task found is yielded to the block. At the end of the block the decision task is auto-completed (AWS::SimpleWorkflow::DecisionTask#complete! is called).
# yields once for each decision task found, indefinetly
domain.decision_tasks.poll do |decision_task|
# make decisions here
end
210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/aws/simple_workflow/decision_task_collection.rb', line 210 def poll task_list, = {}, &block loop do begin poll_for_single_task(task_list, ) do |decision_task| yield(decision_task) end rescue Timeout::Error retry end end nil end |
#poll_for_single_task(task_list, options = {}) {|decision_task| ... } ⇒ DecisionTask?
If you are not using the block form you must call AWS::SimpleWorkflow::DecisionTask#complete! yourself or your decision task will timeout.
Polls the service for a single decision task. The service may hold the request for up to 60 seconds before responding.
# try to get a single task, may return nil when no tasks available
task = domain.decision_tasks.poll_for_single_task('task-list')
if task
# make decisions ...
task.complete!
end
You can optionally pass a block and that will only be yielded to when a decision task is available within the 60 seconds.
domain.decision_tasks.poll_for_single_task('task-list') do |task|
# make decisions
# task.complete! is called for you at the end of the block
end
With the block form you do not need to call #complete! on the decision task. It will be called when the block exists.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/aws/simple_workflow/decision_task_collection.rb', line 162 def poll_for_single_task task_list, = {}, &block client_opts = {} client_opts[:domain] = domain.name client_opts[:identity] = identity_opt() client_opts[:task_list] = { :name => task_list } client_opts[:maximum_page_size] = [:event_batch_size] || 1000 client_opts[:reverse_order] = .key?(:reverse_event_order) ? [:reverse_event_order] : false response = client.poll_for_decision_task(client_opts) if response.data['taskToken'] decision_task = DecisionTask.new(domain, client_opts, response.data) if block_given? yield(decision_task) decision_task.complete! unless decision_task.responded? nil else decision_task end else nil end end |