fate 0.2.10 → 0.2.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,6 +28,7 @@ class Fate
28
28
 
29
29
  # duck typing for IO
30
30
  def write(string)
31
+ return 0 unless string
31
32
  num = @io.write(@master.format(@name, string))
32
33
  @io.flush
33
34
  num
@@ -1,14 +1,4 @@
1
- require "fileutils"
2
1
  require "pp"
3
- # Cross-VM compatibility
4
- # thanks to http://ku1ik.com/2010/09/18/open3-and-the-pid-of-the-spawn.html
5
- if IO.respond_to?(:popen4)
6
- def open4(*args)
7
- IO.popen4(*args)
8
- end
9
- else
10
- require 'open4'
11
- end
12
2
 
13
3
  class Fate
14
4
 
@@ -18,6 +8,7 @@ class Fate
18
8
 
19
9
  attr_reader :logger, :output_handlers
20
10
  def initialize(service)
11
+ @mutex = Mutex.new
21
12
  @service = service
22
13
  @output_handlers = @service.output_handlers
23
14
  @logger = @service.logger["Fate Manager"]
@@ -32,7 +23,6 @@ class Fate
32
23
  end
33
24
 
34
25
  def stop
35
- # FIXME: race condition involving spawn and stop at the same time
36
26
  @names_by_pid.each do |pid, name|
37
27
  kill(name)
38
28
  end
@@ -74,30 +64,28 @@ class Fate
74
64
  end
75
65
 
76
66
  def spawn(name, command)
77
- # TODO: check to see if command is already running
78
67
  return Thread.new do
79
68
 
80
- pid, stdin, stdout, stderr = open4(command)
69
+ handler = output_handlers[name]
70
+ pipe = IO.popen(command, "r", :err => :out)
71
+ pid = pipe.pid
81
72
  logger.info "Starting '#{name}' (pid #{pid})"
82
- @names_by_pid[pid] = name
83
- @pids_by_name[name] = pid
84
-
85
- io = output_handlers[name]
86
- Thread.new do
87
- IO.copy_stream(stderr, io)
88
- end
89
-
90
- # First line written to STDOUT is interpreted as the service
73
+ # First line written to STDOUT is assumed to be the service
91
74
  # signalling that it is ready.
92
- line = stdout.gets
75
+ line = pipe.gets
93
76
  logger.info "#{name} is running."
77
+ handler.write(line)
94
78
 
95
- io.write(line)
96
- @threads[name] = Thread.current
79
+ @mutex.synchronize do
80
+ @names_by_pid[pid] = name
81
+ @pids_by_name[name] = pid
82
+ @threads[name] = Thread.current
83
+ end
97
84
 
98
- IO.copy_stream(stdout, io)
85
+ IO.copy_stream(pipe, handler)
99
86
  pid, status = Process.wait2(pid)
100
87
  handle_child_termination(pid, status)
88
+
101
89
  end
102
90
  end
103
91
 
@@ -12,7 +12,7 @@ class Fate
12
12
  puts "* Available commands: " << commands.sort.join(" ")
13
13
  end
14
14
 
15
- command("quit") do
15
+ command("quit", :alias => "q") do
16
16
  exit
17
17
  end
18
18
 
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.10
4
+ version: 0.2.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -43,22 +43,6 @@ dependencies:
43
43
  - - '='
44
44
  - !ruby/object:Gem::Version
45
45
  version: 0.2.7
46
- - !ruby/object:Gem::Dependency
47
- name: open4
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: 1.3.0
54
- type: :runtime
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: 1.3.0
62
46
  - !ruby/object:Gem::Dependency
63
47
  name: squeeze
64
48
  requirement: !ruby/object:Gem::Requirement