Skip to content

Commit

Permalink
Refactor BatchUpdateJob and allow overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
awead committed Nov 5, 2015
1 parent dcdc35f commit 4846691
Showing 1 changed file with 78 additions and 41 deletions.
119 changes: 78 additions & 41 deletions sufia-models/app/jobs/batch_update_job.rb
@@ -1,3 +1,4 @@
# Resque job that updates files uploaded via the BatchController
class BatchUpdateJob
include Hydra::PermissionsQuery
include Sufia::Messages
Expand All @@ -6,67 +7,103 @@ def queue_name
:batch_update
end

attr_accessor :login, :title, :file_attributes, :batch_id, :visibility, :saved, :denied
attr_reader :login, :batch_id, :title, :file_attributes, :visibility
attr_writer :saved, :denied

# Called from BatchController
# @param [String] login of the current user
# @param [String] batch_id for the Batch object containing the files
# @param [Hash] title contains the filename of each file
# @param [Hash] file_attributes applied to every file in the batch
# @param [String] visibility
def initialize(login, batch_id, title, file_attributes, visibility)
self.login = login
self.title = title || {}
self.file_attributes = file_attributes
self.visibility = visibility
self.batch_id = batch_id
self.saved = []
self.denied = []
@login = login
@batch_id = batch_id
@title = title || {}
@file_attributes = file_attributes
@visibility = visibility
end

def run
batch = Batch.find_or_create(batch_id)
user = User.find_by_user_key(login)

batch.generic_files.each do |gf|
update_file(gf, user)
end

batch.generic_files.each { |gf| update_file(gf) }
batch.update(status: ["Complete"])

if denied.empty?
send_user_success_message(user, batch) unless saved.empty?
else
send_user_failure_message(user, batch)
end
send_user_message
end

def update_file(gf, user)
unless user.can? :edit, gf
ActiveFedora::Base.logger.error "User #{user.user_key} DENIED access to #{gf.id}!"
denied << gf
return
end
# Updates the metadata for one file in the batch. Override this method if you wish to perform
# additional operations to these files.
# @param [GenericFile] gf
def apply_metadata(gf)
gf.title = title[gf.id] if title[gf.id]
gf.attributes = file_attributes
gf.visibility = visibility
end

save_tries = 0
begin
gf.save!
rescue RSolr::Error::Http => error
save_tries += 1
ActiveFedora::Base.logger.warn "BatchUpdateJob caught RSOLR error on #{gf.id}: #{error.inspect}"
# fail for good if the tries is greater than 3
raise error if save_tries >= 3
sleep 0.01
retry
end #
# Queues jobs to run on each file. By default, this includes ContentUpdateEventJob, but
# can be augmented with additional custom jobs
# @param [GenericFile] gf
def queue_additional_jobs(gf)
Sufia.queue.push(ContentUpdateEventJob.new(gf.id, login))
saved << gf
end

def send_user_success_message(user, batch)
def send_user_success_message
message = saved.count > 1 ? multiple_success(batch.id, saved) : single_success(batch.id, saved.first)
User.batchuser.send_message(user, message, success_subject, false)
end

def send_user_failure_message(user, batch)
def send_user_failure_message
message = denied.count > 1 ? multiple_failure(batch.id, denied) : single_failure(batch.id, denied.first)
User.batchuser.send_message(user, message, failure_subject, false)
end

private

def update_file(gf, you = nil)
you ||= user
unless you.can? :edit, gf
ActiveFedora::Base.logger.error "User #{you.user_key} DENIED access to #{gf.id}!"
denied << gf
return
end

apply_metadata(gf)

save_tries = 0
begin
gf.save!
rescue RSolr::Error::Http => error
save_tries += 1
ActiveFedora::Base.logger.warn "BatchUpdateJob caught RSOLR error on #{gf.id}: #{error.inspect}"
# fail for good if the tries is greater than 3
raise error if save_tries >= 3
sleep 0.01
retry
end
queue_additional_jobs(gf)
saved << gf
end

def send_user_message
if denied.empty?
send_user_success_message unless saved.empty?
else
send_user_failure_message
end
end

def batch
@batch ||= Batch.find_or_create(batch_id)
end

def user
@user ||= User.find_by_user_key(login)
end

def saved
@saved ||= []
end

def denied
@denied ||= []
end
end

0 comments on commit 4846691

Please sign in to comment.