Class: Coupler::Models::Job

Inherits:
Sequel::Model
  • Object
show all
Includes:
CommonModel
Defined in:
lib/coupler/models/job.rb

Instance Method Summary collapse

Methods included from CommonModel

#after_destroy, #after_save, #before_create, #before_save, #before_update, included, #save!, #touch!

Instance Method Details

#executeObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/coupler/models/job.rb', line 15

def execute
  Logger.instance.info("Starting job #{id} (#{name})")

  opts = {}
  case name
  when 'transform'
    opts[:total] = resource.source_dataset_count
  when 'import'
    opts[:total] = import.data.file.size
  end

  begin
    opts[:status] = 'running'
    opts[:started_at] = Time.now
    update(opts)
    send("execute_#{name}")
  rescue Exception => e
    message = "%s: %s\n  %s" % [e.class.to_s, e.to_s, e.backtrace.join("\n  ")]
    update({
      :status => 'failed',
      :completed_at => Time.now,
      :error_msg => message
    })
    Logger.instance.error("Job #{id} (#{name}) crashed: #{message}")
    raise e
  end
  self.status = 'done'
  self.completed_at = Time.now
  save
  Logger.instance.info("Job #{id} (#{name}) finished successfully")
end

#percent_completedObject



11
12
13
# File 'lib/coupler/models/job.rb', line 11

def percent_completed
  total > 0 ? completed * 100 / total : 0
end