Class: Baton::Service
Instance Attribute Summary collapse
-
#channel ⇒ Object
Returns the value of attribute channel.
-
#daemonize ⇒ Object
Returns the value of attribute daemonize.
-
#server ⇒ Object
Returns the value of attribute server.
Instance Method Summary collapse
-
#add_consumer(consumer) ⇒ Object
Public: Adds a given consumer to the AMQP channel.
-
#get_pid ⇒ Object
Get the PID of the running daemon.
-
#go ⇒ Object
Public: Method that starts the service.
-
#initialize(daemonize = false) ⇒ Service
constructor
Public: Initialize a Service.
- #run ⇒ Object
-
#setup_consumers ⇒ Object
Public: Method that allows implementations to setup new consumers depending on their needs.
- #stop ⇒ Object
Methods included from Logging
Constructor Details
#initialize(daemonize = false) ⇒ Service
Public: Initialize a Service. Sets up a new server for this service.
17 18 19 20 21 22 |
# File 'lib/baton/service.rb', line 17 def initialize(daemonize=false) @server = Baton::Server.new @daemonize = daemonize @pid_file = Baton.configuration.pid_file || "/var/run/baton.pid" Baton::Logging.logger = Baton.configuration.log_file || STDOUT end |
Instance Attribute Details
#channel ⇒ Object
Returns the value of attribute channel.
14 15 16 |
# File 'lib/baton/service.rb', line 14 def channel @channel end |
#daemonize ⇒ Object
Returns the value of attribute daemonize.
14 15 16 |
# File 'lib/baton/service.rb', line 14 def daemonize @daemonize end |
#server ⇒ Object
Returns the value of attribute server.
14 15 16 |
# File 'lib/baton/service.rb', line 14 def server @server end |
Instance Method Details
#add_consumer(consumer) ⇒ Object
106 107 108 |
# File 'lib/baton/service.rb', line 106 def add_consumer(consumer) channel.add_consumer(consumer) end |
#get_pid ⇒ Object
Get the PID of the running daemon
Returns the PID of the daemon
84 85 86 |
# File 'lib/baton/service.rb', line 84 def get_pid File.exists?(@pid_file) ? File.read(@pid_file).strip : 0 end |
#go ⇒ Object
Public: Method that starts the service.
Returns nothing.
27 28 29 30 31 32 33 34 35 |
# File 'lib/baton/service.rb', line 27 def go logger.info "Starting Baton v#{Baton::VERSION}" EM.run do Signal.trap('INT') { stop } Signal.trap('TERM'){ stop } @channel = Baton::Channel.new setup_consumers end end |
#run ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/baton/service.rb', line 37 def run if @daemonize pid = get_pid if pid != 0 logger.error "Baton is already running! (PID: #{pid})" exit -1 end pid = fork { go } begin File.open(@pid_file, "w") { |f| f.write pid } Process.detach(pid) rescue => exc Process.kill('TERM', pid) logger.error "Couldn't daemonize: #{exc.}" end else go end end |
#setup_consumers ⇒ Object
Public: Method that allows implementations to setup new consumers depending on their needs. An example would be add a deploy consumer which will listen to deploy messages. For each consumer, add_consumer should be called to attach the consumers to the AMQP channel.
Returns Output depends on the implementation.
94 95 |
# File 'lib/baton/service.rb', line 94 def setup_consumers end |
#stop ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/baton/service.rb', line 60 def stop if @daemonize pid = get_pid begin EM.stop rescue end if pid != 0 Process.kill('HUP', pid.to_i) File.delete(@pid_file) logger.info "Stopped" else logger.warn "Daemon not running" exit -1 end else EM.stop end end |