service_manager 0.4 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,7 +8,10 @@ module ServiceManager
8
8
  extend self
9
9
 
10
10
  def services
11
- @services ||= []
11
+ return @services if @services
12
+ @services = []
13
+ load_services
14
+ @services
12
15
  end
13
16
 
14
17
  def load_services(path = nil)
@@ -27,22 +30,24 @@ module ServiceManager
27
30
  end
28
31
 
29
32
  def services_hash
30
- Hash[ServiceManager.services.map { |s| [s.name.to_sym, s]}]
33
+ Hash[services.map { |s| [s.name.to_sym, s]}]
31
34
  end
32
35
 
33
- def stop(which = :all)
36
+ # Stop all services. If service wasn't started by this service manager session, don't try and stop it.
37
+ def stop
38
+ return unless services.any? { |s| s.process }
34
39
  puts "Stopping the services..."
35
40
  services.map {|s| Thread.new { s.stop } }.map(&:join)
36
41
  end
37
42
 
38
- def start(which = :all)
39
- load_services
43
+ # Starts all configured services. If service is detected as running already, don't try and start it.
44
+ def start
40
45
  raise RuntimeError, "No services defined" if services.empty?
41
46
  threads = services.map do |s|
42
47
  Thread.new do
43
48
  begin
44
49
  s.start
45
- rescue ServiceManager::Service::ServerDidntStart
50
+ rescue ServiceManager::Service::ServiceDidntStart
46
51
  puts "Quitting due to failure."
47
52
  exit(1)
48
53
  rescue Exception => e
@@ -1,9 +1,11 @@
1
+ require "thread"
1
2
  class ServiceManager::Service
3
+ CHDIR_SEMAPHORE = Mutex.new
2
4
  NORMAL_COLOR = 37
3
5
 
4
6
  attr_accessor :name, :host, :port, :cwd, :reload_uri, :start_cmd, :process, :loaded_cue, :timeout, :color
5
7
 
6
- class ServerDidntStart < Exception; end
8
+ class ServiceDidntStart < Exception; end
7
9
 
8
10
  def initialize(options = {})
9
11
  options.each { |k,v| send("#{k}=", v) }
@@ -51,10 +53,12 @@ class ServiceManager::Service
51
53
  end
52
54
 
53
55
  puts "Starting #{colorized_service_name} in #{cwd} with '#{start_cmd}'"
54
- Dir.chdir(cwd) do
55
- without_bundler_env do
56
- # system("bash -c set")
57
- self.process = PTYBackgroundProcess.run(start_cmd)
56
+ CHDIR_SEMAPHORE.synchronize do
57
+ Dir.chdir(cwd) do
58
+ without_bundler_env do
59
+ # system("bash -c set")
60
+ self.process = PTYBackgroundProcess.run(start_cmd)
61
+ end
58
62
  end
59
63
  end
60
64
  at_exit { stop }
@@ -62,6 +66,7 @@ class ServiceManager::Service
62
66
  puts "Server #{colorized_service_name} is up."
63
67
  end
64
68
 
69
+ # stop the service. If we didn't start it, do nothing.
65
70
  def stop
66
71
  return unless process
67
72
  puts "Shutting down #{colorized_service_name}"
@@ -76,6 +81,7 @@ class ServiceManager::Service
76
81
  true
77
82
  end
78
83
 
84
+ # reload the service by hitting the configured reload_url. In this case, the service needs to be a web service, and needs to have an action that you can hit, in test mode, that will cause the process to gracefully reload itself.
79
85
  def reload
80
86
  return false unless reload_uri
81
87
  puts "Reloading #{colorized_service_name} app by hitting http://#{host}:#{port}#{reload_uri} ..."
@@ -84,6 +90,7 @@ class ServiceManager::Service
84
90
  true
85
91
  end
86
92
 
93
+ # detects if the service is running on the configured host and port (will return true if we weren't the ones who started it)
87
94
  def running?
88
95
  TCPSocket.listening_service?(:port => port, :host => host)
89
96
  end
@@ -103,14 +110,14 @@ protected
103
110
 
104
111
  def wait
105
112
  if loaded_cue
106
- raise(ServerDidntStart) unless watch_for_cue
113
+ raise(ServiceDidntStart) unless watch_for_cue
107
114
  start_output_stream_thread
108
115
  else
109
116
  start_output_stream_thread
110
117
  begin
111
118
  TCPSocket.wait_for_service_with_timeout({:host => host, :port => port, :timeout => timeout})
112
119
  rescue SocketError
113
- raise ServerDidntStart
120
+ raise ServiceDidntStart
114
121
  end
115
122
  end
116
123
  true
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_manager
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 1
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
9
- version: "0.4"
8
+ - 5
9
+ version: "0.5"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Tim Harper
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-17 00:00:00 -06:00
17
+ date: 2010-09-20 00:00:00 -06:00
18
18
  default_executable: start_services
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency