Class: Async::Container::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/async/container/controller.rb

Overview

Manages the life-cycle of a container.

Constant Summary collapse

SIGHUP =
Signal.list["HUP"]
DEFAULT_TIMEOUT =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(startup_duration: DEFAULT_TIMEOUT) ⇒ Controller

Returns a new instance of Controller.



42
43
44
45
46
# File 'lib/async/container/controller.rb', line 42

def initialize(startup_duration: DEFAULT_TIMEOUT)
  @container = nil
  
  @startup_duration = startup_duration
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



48
49
50
# File 'lib/async/container/controller.rb', line 48

def container
  @container
end

Instance Method Details

#create_containerObject



50
51
52
# File 'lib/async/container/controller.rb', line 50

def create_container
  Container.new
end

#restart(duration = @startup_duration) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/async/container/controller.rb', line 66

def restart(duration = @startup_duration)
  hup_action = Signal.trap(:HUP, :IGNORE)
  container = self.create_container
  
  begin
    self.setup(container)
  rescue
    raise ContainerFailed, container
  end
  
  Async.logger.debug(self, "Waiting for startup...")
  container.sleep(duration)
  Async.logger.debug(self, "Finished startup.")
  
  if container.failed?
    container.stop
    
    raise ContainerFailed, container
  end
  
  @container&.stop
  @container = container
ensure
  Signal.trap(:HUP, hup_action)
end

#runObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/async/container/controller.rb', line 92

def run
  Async.logger.debug(self) {"Starting container..."}
  
  self.start
  
  while true
    begin
      @container.wait
    rescue SignalException => exception
      if exception.signo == SIGHUP
        Async.logger.info(self) {"Reloading container..."}
        
        begin
          self.restart
        rescue ContainerFailed => failure
          Async.logger.error(self) {failure}
        end
      else
        raise
      end
    end
  end
ensure
  self.stop
end

#setup(container) ⇒ Object



54
55
# File 'lib/async/container/controller.rb', line 54

def setup(container)
end

#startObject



57
58
59
# File 'lib/async/container/controller.rb', line 57

def start
  self.restart
end

#stop(graceful = true) ⇒ Object



61
62
63
64
# File 'lib/async/container/controller.rb', line 61

def stop(graceful = true)
  @container&.stop(graceful)
  @container = nil
end