Class: Datahen::Scraper::RubySeederExecutor

Inherits:
Executor
  • Object
show all
Defined in:
lib/datahen/scraper/ruby_seeder_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 = {}) ⇒ RubySeederExecutor

Returns a new instance of RubySeederExecutor.



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

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

Instance Attribute Details

#saveObject

Returns the value of attribute save.



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

def save
  @save
end

Class Method Details

.exposed_methodsObject



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

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

Instance Method Details

#eval_seeder_script(save = false) ⇒ Object



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
65
66
67
68
# File 'lib/datahen/scraper/ruby_seeder_executor.rb', line 34

def eval_seeder_script(save=false)
  update_seeding_starting_status

  proc = Proc.new do
    outputs = []
    pages = []

    begin
      context = isolated_binding({
        outputs: outputs,
        pages: pages
      })
      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 "=========== Seeding Executed ==========="
    begin
      save_pages_and_outputs(pages, outputs, :seeding)
    rescue => e
      handle_error(e) if save
      raise e
    end

    update_seeding_done_status
  end
  proc.call
end

#exec_seeder(save = false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/datahen/scraper/ruby_seeder_executor.rb', line 23

def exec_seeder(save=false)
  @save = save
  if save
    puts "Executing seeder script"
  else
    puts "Trying seeder script"
  end

  eval_seeder_script(save)
end

#handle_error(e) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/datahen/scraper/ruby_seeder_executor.rb', line 114

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

  seeding_update(
    job_id: job_id,
    seeding_status: :failed,
    log_error: error)
end

#save_typeObject



70
71
72
# File 'lib/datahen/scraper/ruby_seeder_executor.rb', line 70

def save_type
  :seeding
end

#update_seeding_done_statusObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/datahen/scraper/ruby_seeder_executor.rb', line 99

def update_seeding_done_status
  return unless save

  response = seeding_update(
    job_id: job_id,
    seeding_status: :done)

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

#update_seeding_starting_statusObject



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

def update_seeding_starting_status
  return unless save

  response = seeding_update(
    job_id: job_id,
    seeding_status: :starting,
    keep_outputs: @keep_outputs
  )

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

#update_to_server(opts = {}) ⇒ Object



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

def update_to_server(opts = {})
  seeding_update(
    job_id: opts[:job_id],
    pages: opts[:pages],
    outputs: opts[:outputs],
    seeding_status: opts[:status])
end