Class: HWSP4Cleanup

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

Overview

Rack middleware that cleans up any 'p4' handle added to the environment during processing.

This should always run, even in the face of exceptions. Make sure any middleware configured after this step does NOT swallow exceptions.

This expects that if a temporary workspace was generated, the 'p4_root' variable was also added into the environment. See P4Helpers.

Instance Method Summary (collapse)

Constructor Details

- (HWSP4Cleanup) initialize(app)

Returns a new instance of HWSP4Cleanup



13
14
15
# File 'lib/hws_p4_cleanup.rb', line 13

def initialize(app)
  @app = app
end

Instance Method Details

- (Object) call(env)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hws_p4_cleanup.rb', line 17

def call(env)
  ex = nil
  begin
    results = @app.call(env)
  rescue StandardError => e
    ex = e
  end

  # We need to convert any exception before attempting to delete,
  # otherwise the p4.messages and p4.errors will get reset at the next
  # method call.
  if ex.instance_of?(P4Exception)
    # TODO define common error classes and replace this
    # ex = Util.make_p4_error(p4)
  end

  if env.key?('p4')
    cleanup_p4(p4: env['p4'], p4_root: env['p4_root'])
  end

  raise ex unless ex.nil?

  results
end

- (Object) cleanup_p4(p4: nil, p4_root: nil)



43
44
45
46
47
48
49
# File 'lib/hws_p4_cleanup.rb', line 43

def cleanup_p4(p4: nil, p4_root: nil)
  if p4 and p4.client != 'INVALID'
    p4.run_client('-d', p4.client)
    FileUtils.rmtree(p4_root) unless p4_root.nil?
  end
  p4.disconnect if p4 and p4.connected?
end