Class: Rivendell::Import::Task
Constant Summary
collapse
- RAN_STATUSES =
%w{completed failed canceled}.freeze
- @@default_xport_options =
{}
Class Method Summary
collapse
Instance Method Summary
collapse
#calculate_destination, #destination, included, #reset_destination!
#change_status!, #define_default_status, included, #invoke_status_changed_callbacks, #raw_status, #status
#cart, included, #raw_cart, #write_cart
included, #raw_tags, #tag, #tags, #write_tags
#close_file, #delete_file!, #file, #file=
Class Method Details
.by_priority ⇒ Object
23
24
25
|
# File 'lib/rivendell/import/task.rb', line 23
def self.by_priority
order(["priority desc", "created_at"])
end
|
.pending ⇒ Object
15
16
17
|
# File 'lib/rivendell/import/task.rb', line 15
def self.pending
where :status => "pending"
end
|
.purge! ⇒ Object
149
150
151
|
# File 'lib/rivendell/import/task.rb', line 149
def self.purge!
where("created_at < ?", 24.hours.ago).destroy_all
end
|
.ran ⇒ Object
29
30
31
|
# File 'lib/rivendell/import/task.rb', line 29
def self.ran
where :status => RAN_STATUSES
end
|
.ready ⇒ Object
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
108
109
110
|
# File 'lib/rivendell/import/task.rb', line 108
def cancel!
self.status = "canceled"
end
|
#destroy_file! ⇒ Object
104
105
106
|
# File 'lib/rivendell/import/task.rb', line 104
def destroy_file!
file.destroy! if delete_file?
end
|
#detailed_status ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/rivendell/import/task.rb', line 92
def detailed_status
if status.pending? and not ready?
"waiting"
else
status
end
end
|
#file_ready? ⇒ Boolean
100
101
102
|
# File 'lib/rivendell/import/task.rb', line 100
def file_ready?
file.ready?
end
|
#logger ⇒ Object
70
71
72
|
# File 'lib/rivendell/import/task.rb', line 70
def logger
Rivendell::Import.logger
end
|
#notify! ⇒ Object
81
82
83
84
85
|
# File 'lib/rivendell/import/task.rb', line 81
def notify!
notifiers.each do |notifier|
notifier.notify
end
end
|
#prepare(&block) ⇒ Object
61
62
63
64
65
66
67
68
|
# 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}"
end
self
end
|
#purge! ⇒ Object
145
146
147
|
# File 'lib/rivendell/import/task.rb', line 145
def purge!
self.class.purge!
end
|
#ran? ⇒ Boolean
37
38
39
|
# File 'lib/rivendell/import/task.rb', line 37
def ran?
status.in? RAN_STATUSES
end
|
#raw_xport_options ⇒ Object
44
45
46
|
# File 'lib/rivendell/import/task.rb', line 44
def raw_xport_options
read_attribute :xport_options
end
|
#ready? ⇒ Boolean
88
89
90
|
# File 'lib/rivendell/import/task.rb', line 88
def ready?
status.pending? and file_ready?
end
|
#run ⇒ Object
112
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
|
# File 'lib/rivendell/import/task.rb', line 112
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_s ⇒ Object
74
75
76
|
# File 'lib/rivendell/import/task.rb', line 74
def to_s
"Import '#{file}' in #{destination}"
end
|
#write_xport_options ⇒ Object
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
|
#xport ⇒ Object
57
58
59
|
# File 'lib/rivendell/import/task.rb', line 57
def xport
@xport ||= Rivendell::API::Xport.new(xport_options)
end
|
#xport_options ⇒ Object
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
|