Class: ImageServer::Spawner

Inherits:
Object
  • Object
show all
Defined in:
lib/image_server/spawner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration: ImageServer.configuration) ⇒ Spawner

Returns a new instance of Spawner.



8
9
10
# File 'lib/image_server/spawner.rb', line 8

def initialize(configuration: ImageServer.configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



6
7
8
# File 'lib/image_server/spawner.rb', line 6

def configuration
  @configuration
end

#pidObject

Returns the value of attribute pid.



6
7
8
# File 'lib/image_server/spawner.rb', line 6

def pid
  @pid
end

#serverObject

Returns the value of attribute server.



6
7
8
# File 'lib/image_server/spawner.rb', line 6

def server
  @server
end

#startedObject

Returns the value of attribute started.



6
7
8
# File 'lib/image_server/spawner.rb', line 6

def started
  @started
end

Instance Method Details

#give_feedbackObject



40
41
42
43
# File 'lib/image_server/spawner.rb', line 40

def give_feedback
  logger.info("image server is starting on port #{configuration.port}...") while starting
  logger.info "image server took #{seconds} seconds to start"
end

#kill_at_exitObject



31
32
33
34
35
36
37
38
# File 'lib/image_server/spawner.rb', line 31

def kill_at_exit
  at_exit do
    if pid
      logger.info 'image server is stopping'
      `pkill -TERM -P #{pid}` # also stops child processes
    end
  end
end

#secondsObject



45
46
47
# File 'lib/image_server/spawner.rb', line 45

def seconds
  '%.3f' % (Time.now - started)
end

#startObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/image_server/spawner.rb', line 12

def start
  unless started?
    self.started = Time.now
    self.pid = fork do
      $stderr.reopen('/dev/null')
      $stdout.reopen('/dev/null')
      server_exec.run
    end
    kill_at_exit
    give_feedback
  end
end

#started?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/image_server/spawner.rb', line 25

def started?
  Net::HTTP.get_response(URI.parse("#{configuration.upload_host}:#{configuration.port}/status_check"))
rescue StandardError
  nil
end

#startingObject



49
50
51
52
53
54
55
56
# File 'lib/image_server/spawner.rb', line 49

def starting
  response = started?
  return false unless response
  raise 'Unable to start image server' unless response.kind_of?(Net::HTTPSuccess)
  false
rescue Errno::ECONNREFUSED
  true
end