Class: SimplerWorkflow::Activity

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Activity.



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

def initialize(domain, name, version, options = {})
  Activity.activities[[name, version]] ||= begin
    default_options = {
      :default_task_list => name,
      :default_task_start_to_close_timeout => 1 * 60 * 60,
      :default_task_schedule_to_start_timeout => 20,
      :default_task_schedule_to_close_timeout => 1 * 60 * 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

#method_missing(meth_name, *args) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/simpler_workflow/activity.rb', line 23

def method_missing(meth_name, *args)
  if @options.has_key?(meth_name.to_sym)
    @options[meth_name.to_sym] = args[0]
  else
    super
  end
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



3
4
5
# File 'lib/simpler_workflow/activity.rb', line 3

def domain
  @domain
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/simpler_workflow/activity.rb', line 3

def name
  @name
end

#next_activityObject (readonly)

Returns the value of attribute next_activity.



3
4
5
# File 'lib/simpler_workflow/activity.rb', line 3

def next_activity
  @next_activity
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/simpler_workflow/activity.rb', line 3

def options
  @options
end

#versionObject (readonly)

Returns the value of attribute version.



3
4
5
# File 'lib/simpler_workflow/activity.rb', line 3

def version
  @version
end

Class Method Details

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



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/simpler_workflow/activity.rb', line 96

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



108
109
110
# File 'lib/simpler_workflow/activity.rb', line 108

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

Instance Method Details

#failure_policyObject



49
50
51
# File 'lib/simpler_workflow/activity.rb', line 49

def failure_policy
  @failure_policy || :abort
end

#on_fail(failure_policy) ⇒ Object



45
46
47
# File 'lib/simpler_workflow/activity.rb', line 45

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

#on_success(activity, version = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/simpler_workflow/activity.rb', line 31

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



53
54
55
# File 'lib/simpler_workflow/activity.rb', line 53

def perform_activity(&block)
  @perform_task = block
end

#perform_task(task) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/simpler_workflow/activity.rb', line 57

def perform_task(task)
  logger.info("Performing task #{name}")
  @perform_task.call(task)
rescue => e
  logger.error e.message
  logger.error e.backtrace.join("\n")
  task.fail! :reason => e.message, :details => {:failure_policy => failure_policy}.to_json
end

#start_activity_loopObject



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
# File 'lib/simpler_workflow/activity.rb', line 70

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|
          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
        end
      rescue Timeout::Error => e
      rescue => e
        logger.error(e.message)
        logger.error(e.backtrace.join("\n"))
      end
    end
  end
end

#to_activity_typeObject



66
67
68
# File 'lib/simpler_workflow/activity.rb', line 66

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