Class: SimplerWorkflow::Activity

Inherits:
Object
  • Object
show all
Includes:
OptionsAsMethods
Defined in:
lib/simpler_workflow/activity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OptionsAsMethods

#method_missing

Constructor Details

#initialize(domain, name, version, options = {}) ⇒ Activity

Returns a new instance of Activity.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/simpler_workflow/activity.rb', line 7

def initialize(domain, name, version, options = {})
  Activity.activities[[name, version]] ||= begin
    default_options = {
      :default_task_list => name,
      :default_task_start_to_close_timeout => 5 * 60,
      :default_task_schedule_to_start_timeout => 5 * 60,
      :default_task_schedule_to_close_timeout => 10 * 60,
      :default_task_heartbeat_timeout => :none
    }
    @options = default_options.merge(options)
    @domain = domain
    @name = name
    @version = version
    @failure_policy = :abort
    self
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SimplerWorkflow::OptionsAsMethods

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



5
6
7
# File 'lib/simpler_workflow/activity.rb', line 5

def domain
  @domain
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/simpler_workflow/activity.rb', line 5

def name
  @name
end

#next_activityObject (readonly)

Returns the value of attribute next_activity.



5
6
7
# File 'lib/simpler_workflow/activity.rb', line 5

def next_activity
  @next_activity
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/simpler_workflow/activity.rb', line 5

def options
  @options
end

#versionObject (readonly)

Returns the value of attribute version.



5
6
7
# File 'lib/simpler_workflow/activity.rb', line 5

def version
  @version
end

Class Method Details

.[](name, version = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/simpler_workflow/activity.rb', line 109

def self.[](name, version = nil)
  case name
  when String
    name = name.to_sym
  when Hash
    name.symbolize_keys!
    version = name[:version]
    name = name[:name]
  end
  activities[[name, version]]
end

.register(name, version, activity) ⇒ Object



121
122
123
# File 'lib/simpler_workflow/activity.rb', line 121

def self.register(name, version, activity)
  activities[[name, version]] = activity
end

Instance Method Details

#countObject



105
106
107
# File 'lib/simpler_workflow/activity.rb', line 105

def count
  domain.activity_tasks.count(name).to_i
end

#failure_policyObject



43
44
45
# File 'lib/simpler_workflow/activity.rb', line 43

def failure_policy
  @failure_policy || :abort
end

#on_fail(failure_policy) ⇒ Object



39
40
41
# File 'lib/simpler_workflow/activity.rb', line 39

def on_fail(failure_policy)
  @failure_policy = failure_policy.to_sym
end

#on_success(activity, version = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/simpler_workflow/activity.rb', line 25

def on_success(activity, version = nil)
  case activity
  when Hash
    activity.symbolize_keys!
    name = activity[:name].to_sym
    version = activity[:version]
  when String
    name = activity.to_sym
  when Symbol
    name = activity
  end
  @next_activity = { :name => name, :version => version }
end

#perform_activity(&block) ⇒ Object



47
48
49
# File 'lib/simpler_workflow/activity.rb', line 47

def perform_activity(&block)
  @perform_task = block
end

#perform_task(task) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/simpler_workflow/activity.rb', line 51

def perform_task(task)
  logger.info("Performing task #{name}")
  @perform_task.call(task)
rescue => e
  context = {}
  context[:activity_type] = [name.to_s, version]
  context[:input] = task.input
  context[:activity_id] = task.activity_id
  SimplerWorkflow.exception_reporter.report(e, context)
  task.fail! :reason => e.message[0..250], :details => {:failure_policy => failure_policy}.to_json
end

#poll_for_single_taskObject



100
101
102
103
# File 'lib/simpler_workflow/activity.rb', line 100

def poll_for_single_task
  logger.info("Polling for single task for #{name}")
  domain.activity_tasks.poll_for_single_task(name.to_s)
end

#start_activity_loopObject



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
94
95
96
97
98
# File 'lib/simpler_workflow/activity.rb', line 67

def start_activity_loop
  Thread.new do
    loop do
      begin
        logger.info("Starting activity_loop for #{name}")
        domain.activity_tasks.poll(name.to_s) do |task|
          begin
            logger.info("Received task...")
            perform_task(task)
            unless task.responded?
              if next_activity
                result = {:next_activity => next_activity}.to_json
                task.complete!(:result => result)
              else
                task.complete!
              end
            end
          rescue => e
            context = {}
            context[:activity_type] = [name.to_s, version]
            context[:input] = task.input
            context[:activity_id] = task.activity_id
            SimplerWorkflow.exception_reporter.report(e, context)
            task.fail! :reason => e.message, :details => { :failure_policy => :abort }.to_json unless task.responded?
          end
        end
      rescue Timeout::Error => e
        retry
      end
    end
  end
end

#to_activity_typeObject



63
64
65
# File 'lib/simpler_workflow/activity.rb', line 63

def to_activity_type
  domain.activity_types[name.to_s, version]
end