Module: SWF::Boot

Extended by:
Boot
Included in:
Boot
Defined in:
lib/swf/boot.rb

Defined Under Namespace

Classes: DeciderStartupFailure, WorkerStartupFailure

Instance Method Summary collapse

Instance Method Details

#settingsObject



99
100
101
102
# File 'lib/swf/boot.rb', line 99

def settings
  # override this in your usage to pull settings from, e.g., ENV or EC2 tags
  {domain_name: 'domain', task_list_name: 'task_list_name'}
end

#startup(deciders, workers, wait_for_children = false, &at_rescue) ⇒ Object



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
# File 'lib/swf/boot.rb', line 14

def startup(deciders, workers, wait_for_children = false, &at_rescue)
  child_pids =  deciders.to_i.times.map {
    Process.fork {
      Process.daemon(true) unless wait_for_children
      rescued = false
      begin
        swf_runner.be_decider
      rescue => e
        error = {
          error: e.inspect,
          backtrace: e.backtrace
        }
        if rescued
          begin
            raise SWF::Boot::DeciderStartupFailure, JSON.pretty_unparse(error)
          rescue SWF::Boot::DeciderStartupFailure => rescued_e
            if at_rescue
              at_rescue.call(rescued_e.to_s)
            else
              raise rescued_e
            end
          end
        else
          rescued = true
          retry
        end
      end
    }
  }

  child_pids += workers.to_i.times.map {
    Process.fork {
      Process.daemon(true) unless wait_for_children
      rescued = false
      begin
        swf_runner.be_worker
      rescue => e
        error = {
          error: e.inspect,
          backtrace: e.backtrace
        }
        if rescued
          begin
            raise SWF::Boot::WorkerStartupFailure, JSON.pretty_unparse(error)
          rescue SWF::Boot::WorkerStartupFailure => rescued_e
            if at_rescue
              at_rescue.call(rescued_e.to_s)
            else
              raise rescued_e
            end
          end
        else
          rescued = true
          retry
        end
      end
    }
  }

  puts "Forked #{deciders} deciders and #{workers} workers..."

  if wait_for_children
    %w(TERM INT).each {|signal| Signal.trap(signal) { terminate_children(child_pids) } }
    puts "Waiting on them..."
    child_pids.each {|pid| Process.wait(pid) }
  else
    child_pids.each {|pid| Process.detach(pid) }
  end

  child_pids

end

#swf_runnerObject



94
95
96
97
# File 'lib/swf/boot.rb', line 94

def swf_runner
  # define this in your usage
  SWF::Runner.new(settings[:domain_name], settings[:task_list_name])
end

#terminate_children(child_pids) ⇒ Object



87
88
89
90
91
92
# File 'lib/swf/boot.rb', line 87

def terminate_children(child_pids)
  child_pids.each {|pid|
    puts "Terminating #{pid}"
    Process.kill("TERM", pid)
  }
end