lib/woodhouse/process.rb in woodhouse-0.1.5 vs lib/woodhouse/process.rb in woodhouse-1.0.0
- old
+ new
@@ -1,12 +1,22 @@
# TODO: take arguments. Also consider using thor.
class Woodhouse::Process
def initialize(keyw = {})
@server = keyw[:server] || build_default_server(keyw)
+ self.class.register_instance self
end
-
+
+ def self.register_instance(instance)
+ @instance = instance
+ end
+
+ # Returns the current global Woodhouse process instance, if it is running.
+ def self.instance
+ @instance
+ end
+
def execute
# Borrowed this from sidekiq. https://github.com/mperham/sidekiq/blob/master/lib/sidekiq/cli.rb
trap "INT" do
Thread.main.raise Interrupt
end
@@ -14,26 +24,26 @@
trap "TERM" do
Thread.main.raise Interrupt
end
Woodhouse::Watchdog.start
- Woodhouse::Watchdog.listen do |id, transition|
- Woodhouse.global_configuration.logger.info "[##{id}] #{transition}"
- end
begin
- @server.start!
+ @server.async.start
puts "Woodhouse serving as of #{Time.now}. Ctrl-C to stop."
@server.wait(:shutdown)
rescue Interrupt
- puts "Shutting down."
- @server.shutdown!
- @server.wait(:shutdown)
+ shutdown
ensure
@server.terminate
Woodhouse::Watchdog.stop
- exit
end
+ end
+
+ def shutdown
+ puts "Shutting down."
+ @server.async.shutdown
+ @server.wait(:shutdown)
end
private
def build_default_server(keyw)