Class: Datahen::Scraper::RubyFinisherExecutor

Inherits:
Executor
  • Object
show all
Defined in:
lib/datahen/scraper/ruby_finisher_executor.rb

Constant Summary

Constants inherited from Executor

Executor::MAX_FIND_OUTPUTS_PER_PAGE

Instance Attribute Summary collapse

Attributes inherited from Executor

#filename, #gid, #job_id, #page

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Executor

#clean_backtrace, #eval_with_context, #exec_parser, #find_output, #find_outputs, #finish, #finisher_update, #get_content, #get_failed_content, #get_job_id, #init_global_page, #init_job_page, #init_page, #parsing_update, #remove_old_dups!, #remove_old_output_dups!, #remove_old_page_dups!, #save_outputs, #save_pages, #save_pages_and_outputs, #seeding_update

Methods included from Plugin::ContextExposer

#create_context, #expose_to, #exposed_env, #exposed_methods, #isolated_binding, #var_or_proc

Constructor Details

#initialize(options = {}) ⇒ RubyFinisherExecutor

Returns a new instance of RubyFinisherExecutor.



6
7
8
9
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 6

def initialize(options={})
  @filename = options.fetch(:filename) { raise "Filename is required"}
  @job_id = options[:job_id]
end

Instance Attribute Details

#saveObject

Returns the value of attribute save.



4
5
6
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 4

def save
  @save
end

Class Method Details

.exposed_methodsObject



11
12
13
14
15
16
17
18
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 11

def self.exposed_methods
  [
    :outputs,
    :save_outputs,
    :find_output,
    :find_outputs
  ].freeze
end

Instance Method Details

#eval_finisher_script(save = false) ⇒ Object



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
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 31

def eval_finisher_script(save=false)
  update_finisher_starting_status

  proc = Proc.new do
    outputs = []

    begin
      context = isolated_binding({
        outputs: outputs,
        job_id: job_id
      })
      eval_with_context filename, context
    rescue Error::SafeTerminateError => e
      # do nothing, this is fine
    rescue SyntaxError => e
      handle_error(e) if save
      raise e
    rescue => e
      handle_error(e) if save
      raise e
    end

    puts "=========== Finisher Executed ==========="
    begin
      save_outputs(outputs)
    rescue => e
      handle_error(e) if save
      raise e
    end

    update_finisher_done_status
  end
  proc.call
end

#exec_finisher(save = false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 20

def exec_finisher(save=false)
  @save = save
  if save
    puts "Executing finisher script"
  else
    puts "Trying finisher script"
  end

  eval_finisher_script(save)
end

#handle_error(e) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 107

def handle_error(e)
  error = ["Finisher #{e.class}: #{e.to_s} (Job:#{job_id}",clean_backtrace(e.backtrace)].join("\n")

  finisher_update(
    job_id: job_id,
    finisher_status: :failed,
    log_error: error)
end

#save_typeObject



66
67
68
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 66

def save_type
  :executing
end

#update_finisher_done_statusObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 92

def update_finisher_done_status
  return unless save

  response = finisher_update(
    job_id: job_id,
    finisher_status: :done)

  if response.code == 200
    puts "Finisher Done."
  else
    puts "Error: Unable to save Finisher Done Status to server: #{response.body}"
    raise "Unable to save Finisher Done Status to server: #{response.body}"
  end
end

#update_finisher_starting_statusObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 77

def update_finisher_starting_status
  return unless save

  response = finisher_update(
    job_id: job_id,
    finisher_status: :starting)

  if response.code == 200
    puts "Finisher Status Updated."
  else
    puts "Error: Unable to save Finisher Status to server: #{response.body}"
    raise "Unable to save Finisher Status to server: #{response.body}"
  end
end

#update_to_server(opts = {}) ⇒ Object



70
71
72
73
74
75
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 70

def update_to_server(opts = {})
  finisher_update(
    job_id: opts[:job_id],
    outputs: opts[:outputs],
    finisher_status: opts[:status])
end