Class: ReportRunner

Inherits:
JobRunner show all
Defined in:
backend/app/lib/job_runners/reports_runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from JobRunner

#add_success_hook, #cancelation_signaler, #canceled?, for, #initialize, #parse_job_params_string, register_for_job_type, registered_job_types, registered_runner_for, #success!, #symbol_keys

Constructor Details

This class inherits a constructor from JobRunner

Class Method Details

.reportsObject



11
12
13
# File 'backend/app/lib/job_runners/reports_runner.rb', line 11

def self.reports
  ReportManager.registered_reports
end

Instance Method Details

#runObject



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
51
52
53
54
55
# File 'backend/app/lib/job_runners/reports_runner.rb', line 16

def run
  @job.write_output('Generating report')
  file = ASUtils.tempfile('report_job_')
  begin
    job_data = @json.job

    # we need to massage the json sometimes..
    begin
      params = ASUtils.json_parse(@json.job_params[1..-2].delete('\\'))
    rescue JSON::ParserError
      params = {}
    end
    params[:format] = job_data['format'] || 'pdf'
    params[:repo_id] = @json.repo_id

    report_model = ReportRunner.reports[job_data['report_type']][:model]

    report = DB.open do |db|
      report_model.new(params, @job, db)
    end

    file = ASUtils.tempfile('report_job_')
    ReportGenerator.new(report).generate(file)

    file.rewind
    @job.write_output('Adding report file.')

    @job.add_file(file)

    self.success!
  rescue Exception => e
    @job.write_output(e.message)
    @job.write_output(e.backtrace)
    raise e
  ensure
    file.close
    file.unlink
    @job.write_output('Done.')
  end
end