Class: Datahen::Scraper::RubySeederExecutor

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

Returns a new instance of RubySeederExecutor.



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

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



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

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

Instance Method Details

#eval_seeder_script(save = false) ⇒ Object



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
70
71
# File 'lib/datahen/scraper/ruby_seeder_executor.rb', line 37

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



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

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



117
118
119
120
121
122
123
124
# File 'lib/datahen/scraper/ruby_seeder_executor.rb', line 117

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



73
74
75
# File 'lib/datahen/scraper/ruby_seeder_executor.rb', line 73

def save_type
  :seeding
end

#update_seeding_done_statusObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/datahen/scraper/ruby_seeder_executor.rb', line 102

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



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

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



77
78
79
80
81
82
83
# File 'lib/datahen/scraper/ruby_seeder_executor.rb', line 77

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