Class: Datahen::Scraper::RubyFinisherExecutor

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

Constant Summary collapse

FIND_OUTPUTS_RETRY_LIMIT =
nil

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.



8
9
10
11
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 8

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



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 13

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

Instance Method Details

#eval_finisher_script(save = false) ⇒ Object



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

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



25
26
27
28
29
30
31
32
33
34
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 25

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



112
113
114
115
116
117
118
119
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 112

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



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

def save_type
  :executing
end

#update_finisher_done_statusObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 97

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 82

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



75
76
77
78
79
80
# File 'lib/datahen/scraper/ruby_finisher_executor.rb', line 75

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