Class: Trinidad::Lifecycle::Host::RollingReload

Inherits:
Object
  • Object
show all
Defined in:
lib/trinidad/lifecycle/host/rolling_reload.rb

Overview

Rolls a new context that replaces the current one on reloads.

Defined Under Namespace

Classes: Takeover

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ RollingReload

Returns a new instance of RollingReload.



8
9
10
# File 'lib/trinidad/lifecycle/host/rolling_reload.rb', line 8

def initialize(server)
  @server = server
end

Class Method Details

.loggerObject

log into the same location as context.reload does :



52
53
54
# File 'lib/trinidad/lifecycle/host/rolling_reload.rb', line 52

def self.logger # log into the same location as context.reload does :
  Trinidad::Logging::LogFactory.getLog('org.apache.catalina.core.StandardContext')
end

Instance Method Details

#reload!(app_holder) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/trinidad/lifecycle/host/rolling_reload.rb', line 12

def reload!(app_holder)
  web_app, old_context = app_holder.web_app, app_holder.context
  logger = self.class.logger
  logger.info "Context with name [#{old_context.name}] has started rolling"

  web_app.reset! # force a new class loader + re-read state (from config)
  no_host = org.apache.catalina.Host.impl {} # do not add to parent yet
  new_context = @server.add_web_app(web_app, no_host, false)
  # Tomcat requires us to have unique names for its containers :
  new_context.name = "#{old_context.name}-#{java.lang.System.currentTimeMillis}"
  new_context.add_lifecycle_listener(takeover = Takeover.new(old_context))
  app_holder.context = new_context

  Thread.new do
    begin
      logger.debug "Starting a new Context for [#{new_context.path}]"
      old_context.parent.add_child new_context # NOTE: likely starts!
      
      new_context.start unless new_context.state_name =~ /START|STOP|FAILED/i
      
      if new_context.state_name =~ /STOP|FAILED/i
        logger.error("Context with name [#{old_context.name}] failed rolling")
        takeover.failed!(new_context)
      else
        logger.info "Context with name [#{old_context.name}] has completed rolling"
      end
    rescue => error
      e = org.jruby.exceptions.RaiseException.new(error)
      logger.error("Context with name [#{old_context.name}] failed rolling", e)
      takeover.failed!(new_context)
    rescue java.lang.Exception => e
      logger.error("Context with name [#{old_context.name}] failed rolling", e)
      takeover.failed!(new_context)
    ensure
      app_holder.unlock
    end
  end
  false # not yet reloaded do not release lock
end