Skip to content

Commit

Permalink
Moved the pid file handling into the monitor.
Browse files Browse the repository at this point in the history
  • Loading branch information
roidrage committed Feb 15, 2010
1 parent 8a6b988 commit 19ec49b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
10 changes: 1 addition & 9 deletions lib/nanite/agent.rb
Expand Up @@ -3,7 +3,6 @@ class Agent
include AMQPHelper
include FileStreaming
include ConsoleHelper
include DaemonizeHelper

attr_reader :identity, :options, :serializer, :dispatcher, :registry, :amqp, :tags, :heartbeat
attr_accessor :status_proc
Expand Down Expand Up @@ -99,19 +98,12 @@ def run
Log.level = @options[:log_level] if @options[:log_level]
@serializer = Serializer.new(@options[:format])
@status_proc = lambda { parse_uptime(`uptime 2> /dev/null`) rescue 'no status' }
pid_file = PidFile.new(@identity, @options)
pid_file.check
if @options[:daemonize]
daemonize(@identity, @options)
pid_file.write
at_exit { pid_file.remove }
end
Nanite::Agent::Monitor.new(self, @options)
@amqp = start_amqp(@options)
@registry = ActorRegistry.new
@dispatcher = Dispatcher.new(@amqp, @registry, @serializer, @identity, @options)
setup_mapper_proxy
load_actors
Nanite::Agent::Monitor.new(self, @options)
setup_queue
advertise_services
setup_heartbeat
Expand Down
17 changes: 16 additions & 1 deletion lib/nanite/agent/monitor.rb
@@ -1,14 +1,29 @@
module Nanite
class Agent
class Monitor
attr_reader :agent, :options, :shutting_down
include DaemonizeHelper

attr_reader :agent, :options, :shutting_down, :pid_file

def initialize(agent, options = {})
@agent = agent
@options = options
setup_pid_file
daemonize_agent if options[:daemonize]
setup_traps
end

def setup_pid_file
@pid_file = PidFile.new(agent.identity, options)
@pid_file.check
end

def daemonize_agent
daemonize(agent.identity, options)
pid_file.write
at_exit { pid_file.remove }
end

def setup_traps
['INT', 'TERM'].each do |signal|
trap signal do
Expand Down
29 changes: 27 additions & 2 deletions spec/monitor_spec.rb
Expand Up @@ -2,12 +2,37 @@

describe Nanite::Agent::Monitor do
include SpecHelpers

module Nanite::DaemonizeHelper
def daemonize(identity, options = {})
end
end

before(:each) do
@agent = stub(:agent, :unsubscribe => true, :un_register => true, :disconnect => true)
@agent = stub(:agent, :unsubscribe => true, :un_register => true, :disconnect => true, :identity => 'identity', :pid_file => "pid")
@monitor = Nanite::Agent::Monitor.new(@agent)
@monitor.stub!(:exit)
end

describe "When setting up the pid file" do
after(:each) do
Nanite::PidFile.new('identity', {}).remove
end

it "should set write a pid file when daemonize was requested" do
@monitor = Nanite::Agent::Monitor.new(@agent, :daemonize => true)
@monitor.stub!(:exit)
Nanite::PidFile.new('identity', {}).exists?.should == true
end

it "should raise an error if a pid file exists" do
Nanite::PidFile.new('identity', {}).write
lambda {
@monitor.setup_pid_file
}.should raise_error
end
end

describe "When gracefully shutting down" do
before(:each) do
EM.stub!(:add_timer).and_yield
Expand Down Expand Up @@ -36,7 +61,7 @@
end
end

describe "when initiating shutdown" do
describe "When initiating shutdown" do
it "should unsubscribe and unregister the agent queues and system" do
run_in_em do
@agent.should_receive(:unsubscribe)
Expand Down

0 comments on commit 19ec49b

Please sign in to comment.