Class: Resque::Reports::ReportJob

Inherits:
Object
  • Object
show all
Extended by:
Extensions::EnqueueToFix
Includes:
Integration
Defined in:
app/jobs/resque/reports/report_job.rb

Overview

ReportJob accepts report_type, its arguments in json and building report in background @example:

ReportJob.enqueue('Resque::Reports::MyReport', [1, 2].to_json)
ReportJob.enqueue_to(:my_queue, 'Resque::Reports::MyReport', [1, 2].to_json)

Class Method Summary collapse

Methods included from Extensions::EnqueueToFix

extended

Class Method Details

.execute(report_type, args_json) ⇒ Object

resque-integration main job method

Parameters:

  • report_type (String)
    • name of BaseReport successor

    to build report for

  • args_json (String(JSON))
    • json array of report arguments



24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/jobs/resque/reports/report_job.rb', line 24

def self.execute(report_type, args_json)
  report_class = report_type.constantize # избавиться от ActiveSupport

  unless report_class < BaseReport
    fail "Supports only successors of BaseReport, but got #{report_class}"
  end

  args = JSON.parse(args_json)
  force = args.pop

  init_report(report_class, args).build(force)
end

.init_report(report_class, args_array) ⇒ Object

Initializes report of given class with given arguments



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/jobs/resque/reports/report_job.rb', line 40

def self.init_report(report_class, args_array)
  report = report_class.new(*args_array)

  report_class.on_progress do |progress, total|
    unless total.zero?
      at(progress, total, report.progress_message(progress, total))
    end
  end

  report_class.on_error do |error|
    meta = get_meta(@meta_id)
    meta['payload'] ||= {'error_messages' => []}
    meta['payload']['error_messages'] << report.error_message(error)
    meta.save
  end

  report
end