Class: Rivendell::Import::Task

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Rivendell::Import::Tasking::Cart, Rivendell::Import::Tasking::Destination, Rivendell::Import::Tasking::File, Rivendell::Import::Tasking::Status, Rivendell::Import::Tasking::Tags
Defined in:
lib/rivendell/import/task.rb

Constant Summary collapse

RAN_STATUSES =
%w{completed failed canceled}.freeze
@@default_xport_options =
{}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rivendell::Import::Tasking::Destination

#calculate_destination, #destination, included, #reset_destination!

Methods included from Rivendell::Import::Tasking::Status

#change_status!, #define_default_status, included, #invoke_status_changed_callbacks, #raw_status, #status

Methods included from Rivendell::Import::Tasking::Cart

#cart, included, #raw_cart, #write_cart

Methods included from Rivendell::Import::Tasking::Tags

included, #raw_tags, #tag, #tags, #write_tags

Methods included from Rivendell::Import::Tasking::File

#close_file, #delete_file!, #file, #file=

Class Method Details

.by_priorityObject



23
24
25
# File 'lib/rivendell/import/task.rb', line 23

def self.by_priority
  order(["priority desc", "created_at"])
end

.pendingObject



15
16
17
# File 'lib/rivendell/import/task.rb', line 15

def self.pending
  where :status => "pending"
end

.purge!Object



150
151
152
# File 'lib/rivendell/import/task.rb', line 150

def self.purge!
  where("created_at < ?", 24.hours.ago).destroy_all
end

.ranObject



29
30
31
# File 'lib/rivendell/import/task.rb', line 29

def self.ran
  where :status => RAN_STATUSES
end

.readyObject



19
20
21
# File 'lib/rivendell/import/task.rb', line 19

def self.ready
  pending.by_priority.select(&:ready?)
end

.search(text) ⇒ Object



33
34
35
# File 'lib/rivendell/import/task.rb', line 33

def self.search(text)
  where [ "lower(file_name) like ?", "%#{text.downcase}%" ]
end

Instance Method Details

#cancel!Object



109
110
111
# File 'lib/rivendell/import/task.rb', line 109

def cancel!
  self.status = "canceled"
end

#destroy_file!Object



105
106
107
# File 'lib/rivendell/import/task.rb', line 105

def destroy_file!
  file.destroy! if delete_file?
end

#detailed_statusObject



93
94
95
96
97
98
99
# File 'lib/rivendell/import/task.rb', line 93

def detailed_status
  if status.pending? and not ready?
    "waiting"
  else
    status
  end
end

#file_ready?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/rivendell/import/task.rb', line 101

def file_ready?
  file.ready?
end

#loggerObject



71
72
73
# File 'lib/rivendell/import/task.rb', line 71

def logger
  Rivendell::Import.logger
end

#notify!Object



82
83
84
85
86
# File 'lib/rivendell/import/task.rb', line 82

def notify!
  notifiers.each do |notifier|
    notifier.notify
  end
end

#prepare(&block) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/rivendell/import/task.rb', line 61

def prepare(&block)
  begin
    Context.new(self).run(&block)
  rescue => e
    logger.error "Task preparation failed : #{e}"
    change_status! :failed
  end
  self
end

#purge!Object



146
147
148
# File 'lib/rivendell/import/task.rb', line 146

def purge!
  self.class.purge!
end

#ran?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rivendell/import/task.rb', line 37

def ran?
  status.in? RAN_STATUSES
end

#raw_xport_optionsObject



44
45
46
# File 'lib/rivendell/import/task.rb', line 44

def raw_xport_options
  read_attribute :xport_options
end

#ready?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/rivendell/import/task.rb', line 89

def ready?
  status.pending? and file_ready?
end

#runObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rivendell/import/task.rb', line 113

def run
  if status.canceled?
    logger.debug "Don't run canceled task : #{self.inspect}"
    return
  end

  logger.debug "Run #{self.inspect}"
  change_status! :running

  reset_destination!

  cart.create
  cart.import file
  cart.update

  destroy_file!
  change_status! :completed

  logger.info "Imported Cart #{cart.number}"
rescue Exception => e
  logger.error "Task failed : #{e}"
  logger.debug e.backtrace.join("\n")
ensure
  close_file

  unless ran?
    change_status! :failed
  end
  save!
end

#to_sObject



75
76
77
# File 'lib/rivendell/import/task.rb', line 75

def to_s
  "Import '#{file}' in #{destination}"
end

#write_xport_optionsObject



52
53
54
# File 'lib/rivendell/import/task.rb', line 52

def write_xport_options
  write_attribute :xport_options, (xport_options.present? ? xport_options.to_json : nil)
end

#xportObject



57
58
59
# File 'lib/rivendell/import/task.rb', line 57

def xport
  @xport ||= Rivendell::API::Xport.new(xport_options)
end

#xport_optionsObject



48
49
50
# File 'lib/rivendell/import/task.rb', line 48

def xport_options
  @xport_options ||= (raw_xport_options ? JSON.parse(raw_xport_options).with_indifferent_access : default_xport_options.dup)
end