Class: Baton::Service

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/baton/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger, logger=

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

#channelObject

Returns the value of attribute channel.



14
15
16
# File 'lib/baton/service.rb', line 14

def channel
  @channel
end

#daemonizeObject

Returns the value of attribute daemonize.



14
15
16
# File 'lib/baton/service.rb', line 14

def daemonize
  @daemonize
end

#serverObject

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

Public: Adds a given consumer to the AMQP channel.

consumer - An instance of Baton::Consumer.

Examples

add_consumer(Baton::DeployConsumer.new("consumer_name", Baton::Server.new))

Returns nothing..



106
107
108
# File 'lib/baton/service.rb', line 106

def add_consumer(consumer)
  channel.add_consumer(consumer)
end

#get_pidObject

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

#goObject

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

#runObject



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.message}"
    end
  else
    go
  end
end

#setup_consumersObject

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

#stopObject



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