Class: Aspera::Faspex4PostProcServlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/aspera/faspex_postproc.rb

Overview

Start a Faspex-4 style post-processing script using Faspex-5 webhook

Constant Summary collapse

ALLOWED_PARAMETERS =
%i[root script_folder fail_on_error timeout_seconds].freeze

Instance Method Summary collapse

Constructor Details

#initialize(server, parameters) ⇒ Faspex4PostProcServlet

Returns a new instance of Faspex4PostProcServlet.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aspera/faspex_postproc.rb', line 14

def initialize(server, parameters)
  Aspera.assert_type(parameters, Hash)
  @parameters = parameters.symbolize_keys
  Log.dump(:post_proc_parameters, @parameters)
  not_allowed = @parameters.keys - ALLOWED_PARAMETERS
  raise "unsupported parameters: #{not_allowed.join(', ')}" unless not_allowed.empty?
  @parameters[:script_folder] ||= '.'
  @parameters[:fail_on_error] ||= false
  @parameters[:timeout_seconds] ||= 60
  super(server)
  Log.log.debug{'Faspex4PostProcServlet initialized'}
end

Instance Method Details

#do_POST(request, response) ⇒ Object

:reek:UncommunicativeMethodName



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
69
70
71
72
73
74
75
76
# File 'lib/aspera/faspex_postproc.rb', line 28

def do_POST(request, response)
  Log.log.debug{"request=#{request.path}"}
  begin
    # only accept requests on the root
    if !request.path.start_with?(@parameters[:root])
      response.status = 400
      response['Content-Type'] = Rest::MIME_JSON
      response.body = {status: 'error', message: 'Request outside domain'}.to_json
      return
    end
    if request.body.nil?
      response.status = 400
      response['Content-Type'] = Rest::MIME_JSON
      response.body = {status: 'error', message: 'Empty request'}.to_json
      return
    end
    # build script path by removing domain and adding script folder
    script_file = request.path[@parameters[:root].size..]
    Log.log.debug{"script file=#{script_file}"}
    script_path = File.join(@parameters[:script_folder], script_file)
    Log.log.debug{"script=#{script_path}"}
    webhook_parameters = JSON.parse(request.body)
    Log.dump(:webhook_parameters, webhook_parameters)
    # env expects only strings
    environment = webhook_parameters.each_with_object({}){ |(k, v), h| h[k] = v.to_s}
    post_proc_pid = Environment.secure_spawn(env: environment, exec: script_path)
    Timeout.timeout(@parameters[:timeout_seconds]) do
      # "wait" for process to avoid zombie
      Process.wait(post_proc_pid)
      post_proc_pid = nil
    end
    process_status = $CHILD_STATUS
    raise "script #{script_path} failed with code #{process_status.exitstatus}" if !process_status.success? && @parameters[:fail_on_error]
    response.status = 200
    response.content_type = Rest::MIME_JSON
    response.body = JSON.generate({status: 'success', script: script_path, exit_code: process_status.exitstatus})
    Log.log.debug{'Script executed successfully'}
  rescue => e
    Log.log.error("Script failed: #{e.class}:#{e.message}")
    if !post_proc_pid.nil?
      Process.kill('SIGKILL', post_proc_pid)
      Process.wait(post_proc_pid)
      Log.log.error("Killed process: #{post_proc_pid}")
    end
    response.status = 500
    response['Content-Type'] = Rest::MIME_JSON
    response.body = {status: 'error', script: script_path, message: e.message}.to_json
  end
end