Class: Aikido::Zen::DetachedAgent::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/aikido/zen/detached_agent/server.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: Aikido::Zen.config) ⇒ Server

Returns a new instance of Server.



14
15
16
17
18
19
20
21
# File 'lib/aikido/zen/detached_agent/server.rb', line 14

def initialize(config: Aikido::Zen.config)
  @started_at = nil

  @config = config

  @socket_path = config.expanded_detached_agent_socket_path
  @socket_uri = config.expanded_detached_agent_socket_uri
end

Class Method Details

.start(**opts) ⇒ Aikido::Zen::DetachedAgent::Server

Initialize and start a detached agent server instance.



10
11
12
# File 'lib/aikido/zen/detached_agent/server.rb', line 10

def self.start(**opts)
  new(**opts).tap(&:start!)
end

Instance Method Details

#start!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/aikido/zen/detached_agent/server.rb', line 27

def start!
  @config.logger.info("Starting DRb Server...")

  # Try to ensure that the DRb service can start if the DRb service did
  # not stop cleanly.
  begin
    # Check whether the Unix domain socket is in use by another process.
    UNIXSocket.new(@socket_path).close
  rescue Errno::ECONNREFUSED
    @config.logger.debug("Removing residual Unix domain socket...")

    # Remove the residual Unix domain socket.
    FileUtils.rm_f(@socket_path)
  rescue
    # empty
  end

  @front = FrontObject.new

  # If the Unix domain socket is in use by another process and/or the
  # residual Unix domain socket could not be removed DRb will raise an
  # appropriate error.
  @drb_server = DRb.start_service(@socket_uri, @front)

  # Only show DRb output in debug mode.
  @drb_server.verbose = @config.logger.debug?

  # Ensure that the DRb server is alive.
  max_attempts = 10
  attempts = 0
  until @drb_server.alive?
    @config.logger.info("DRb Server still not alive. #{max_attempts - attempts} attempts remaining")
    sleep 0.1
    attempts += 1
    raise Aikido::Zen::DetachedAgentError.new("Impossible to start the dRB server (socket=#{Aikido::Zen.config.detached_agent_socket_path})") \
      if attempts == max_attempts
  end

  @started_at = Time.now.utc

  at_exit { stop! if started? }
end

#started?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/aikido/zen/detached_agent/server.rb', line 23

def started?
  !!@started_at
end

#stop!Object



70
71
72
73
74
75
76
# File 'lib/aikido/zen/detached_agent/server.rb', line 70

def stop!
  @config.logger.info("Stopping DRb Server...")
  @started_at = nil

  @drb_server.stop_service if @drb_server.alive?
  DRb.stop_service
end