class Datahen::Scraper::RubyFinisherExecutor

Attributes

save[RW]

Public Class Methods

exposed_methods() click to toggle source
# File lib/datahen/scraper/ruby_finisher_executor.rb, line 11
def self.exposed_methods
  [
    :outputs,
    :save_outputs,
    :find_output,
    :find_outputs
  ].freeze
end
new(options={}) click to toggle source
# 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

Public Instance Methods

eval_finisher_script(save=false) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_type() click to toggle source
# File lib/datahen/scraper/ruby_finisher_executor.rb, line 66
def save_type
  :executing
end
update_finisher_done_status() click to toggle source
# 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_status() click to toggle source
# 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 = {}) click to toggle source
# 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