fate 0.2.11 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/fate.rb +13 -6
  2. data/lib/fate/process_manager.rb +40 -27
  3. metadata +3 -3
data/lib/fate.rb CHANGED
@@ -35,16 +35,23 @@ class Fate
35
35
  end
36
36
 
37
37
  def run(&block)
38
- start
39
- if block
40
- yield(self)
41
- manager.stop
38
+ if start
39
+ if block
40
+ yield(self)
41
+ stop
42
+ end
43
+ else
44
+ logger.error "Failed to start"
42
45
  end
43
46
  end
44
47
 
45
48
  def start
46
- manager.start_group(@service.commands)
47
- logger.green "All commands are running."
49
+ if manager.start_group(@service.commands)
50
+ logger.green "All commands are running."
51
+ true
52
+ else
53
+ false
54
+ end
48
55
  end
49
56
 
50
57
  def stop
@@ -23,20 +23,24 @@ class Fate
23
23
  end
24
24
 
25
25
  def stop
26
- @names_by_pid.each do |pid, name|
27
- kill(name)
26
+ @mutex.synchronize do
27
+ @names_by_pid.each do |pid, name|
28
+ kill(name)
29
+ end
28
30
  end
29
31
  end
30
32
 
31
33
  def start_group(hash)
32
34
  hash.each do |name, command|
33
35
  @commands_by_name[name] = command
34
- start_command(name, command)
36
+ start_command(name, command) unless @down_in_flames
35
37
  end
36
38
 
37
39
  until @threads.size == hash.size
40
+ return false if @down_in_flames
38
41
  sleep 0.1
39
42
  end
43
+ return true
40
44
  end
41
45
 
42
46
  def start_command(name, command)
@@ -52,7 +56,7 @@ class Fate
52
56
  @names_by_pid.delete(pid)
53
57
  @pids_by_name.delete(name)
54
58
  @threads.delete(name)
55
- system "kill -s INT #{pid}"
59
+ system "kill -s TERM #{pid}"
56
60
  logger.info "Sent a kill signal to '#{name}' running at #{pid}"
57
61
  else
58
62
  logger.error "Could not find pid for '#{name}'"
@@ -66,25 +70,37 @@ class Fate
66
70
  def spawn(name, command)
67
71
  return Thread.new do
68
72
 
69
- handler = output_handlers[name]
70
- pipe = IO.popen(command, "r", :err => :out)
71
- pid = pipe.pid
72
- logger.info "Starting '#{name}' (pid #{pid})"
73
- # First line written to STDOUT is assumed to be the service
74
- # signalling that it is ready.
75
- line = pipe.gets
76
- logger.info "#{name} is running."
77
- handler.write(line)
78
-
73
+ pipe = nil
74
+ pid = nil
79
75
  @mutex.synchronize do
80
- @names_by_pid[pid] = name
81
- @pids_by_name[name] = pid
82
- @threads[name] = Thread.current
76
+ unless @down_in_flames
77
+ pipe = IO.popen(command, "r", :err => :out)
78
+ pid = pipe.pid
79
+ logger.info "Starting '#{name}' (pid #{pid})"
80
+
81
+ @names_by_pid[pid] = name
82
+ @pids_by_name[name] = pid
83
+ end
83
84
  end
84
85
 
85
- IO.copy_stream(pipe, handler)
86
- pid, status = Process.wait2(pid)
87
- handle_child_termination(pid, status)
86
+ unless @down_in_flames
87
+ # Obtain an IO-ish object
88
+ handler = output_handlers[name]
89
+ # First line written to STDOUT is assumed to be the service
90
+ # signalling that it is ready.
91
+ line = pipe.gets
92
+ @mutex.synchronize do
93
+ unless @down_in_flames
94
+ logger.info "#{name} is running."
95
+ handler.write(line)
96
+ end
97
+ end
98
+ @threads[name] = Thread.current
99
+
100
+ IO.copy_stream(pipe, handler)
101
+ pid, status = Process.wait2(pid)
102
+ handle_child_termination(pid, status)
103
+ end
88
104
 
89
105
  end
90
106
  end
@@ -99,23 +115,20 @@ class Fate
99
115
  def handle_child_termination(pid, status)
100
116
  if name = @names_by_pid.delete(pid)
101
117
  @pids_by_name.delete(name)
102
- # TODO: CLI and instantiation flags for @mode
103
- if (@mode != :production) && status != 0
104
- down_in_flames(name, pid, status)
105
- else
106
- # Probably should notify somebody somehow
107
- end
118
+ down_in_flames(name, pid, status)
108
119
  end
109
120
  end
110
121
 
111
122
  def down_in_flames(name, pid, status)
123
+ @down_in_flames = true
112
124
  if status.exitstatus
113
125
  logger.error "Process '#{name}' (pid #{pid}) exited with code #{status.exitstatus}."
114
126
  else
115
127
  logger.info "Process '#{name}' (pid #{pid}) was sent signal #{status.termsig}."
116
128
  end
117
129
  logger.info "Shutting down all processes."
118
- exit(1)
130
+
131
+ stop
119
132
  end
120
133
 
121
134
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.11
4
+ version: 0.2.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-14 00:00:00.000000000 Z
12
+ date: 2012-09-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -112,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  version: '0'
113
113
  requirements: []
114
114
  rubyforge_project:
115
- rubygems_version: 1.8.23
115
+ rubygems_version: 1.8.24
116
116
  signing_key:
117
117
  specification_version: 3
118
118
  summary: Tool for running and interacting with a multi-process service