Skip to content

Commit

Permalink
Merge pull request #44 from cul/queue-task-updates
Browse files Browse the repository at this point in the history
queue.rake refactoring so that atc:queue:perform_transfer task can read from an id list
  • Loading branch information
elohanlon authored Apr 8, 2024
2 parents 9c2264a + bf9a4c0 commit 76c1675
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions lib/tasks/queue.rake
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,41 @@ namespace :atc do
ENV['enqueue_successor'] == 'true'
end

# Yields one or more source_id integer values based on available ENV values.
def with_source_object_id_argument(&block)
with_id_argument('source_object', &block)
end

def with_pending_transfer_id_argument(&block)
with_id_argument('pending_transfer', &block)
end

# Yields one or more id integer values based on available ENV values.
# Reads from one of the following ENV values (using whichever one it finds first):
# source_object_id, source_object_path, source_object_id_file
# "#{prefix}_id", "#{prefix}_id_file", "source_object_path"
#
# ENV['source_object_id'] - A single SourceObject id
# ENV['source_object_path'] - A SourceObject path value
# ENV['source_object_id_file'] - A file that contains a list of SourceObject ids, with one id per line
# ENV["#{prefix}_id"] - A single id
# ENV["#{prefix}_id_file"] - A file that contains a list of ids, with one id per line
# ENV["source_object_path"] - A SourceObject path value that will be resolved to a SourceObject id via DB lookup.
#
# @return [Boolean] true if at least one source_id is available based on the ENV values,
# otherwise prints an error message to stdout and returns false.
def with_source_id_argument
if ENV['source_object_id'].present?
value = ENV['source_object_id']
def with_id_argument(prefix)
if ENV["#{prefix}_id"].present?
value = ENV["#{prefix}_id"]
unless value.match?(/[0-9]+/)
puts "Error: source_object_id must be an integer value"
puts "Error: #{prefix}_id must be an integer value"
return false
end
yield value.to_i
elsif ENV['source_object_path'].present?
id = SourceObject.for_path(ENV['source_object_path'])&.id
unless id.present?
puts "Error: could not find a SourceObject with the given source_object_path"
return false
end
yield id
elsif ENV['source_object_id_file'].present?
source_object_id_file = ENV['source_object_id_file']
unless File.exist?(source_object_id_file)
puts "Error: File not found at #{source_object_id_file}"
elsif ENV["#{prefix}_id_file"].present?
id_file = ENV["#{prefix}_id_file"]
unless File.exist?(id_file)
puts "Error: File not found at #{id_file}"
return false
end
# First, validate all of the ids (to make sure they're all numeric ids)
puts 'Validating source_object_id_file...'
File.foreach(source_object_id_file) do |line|
puts "Validating #{prefix}_id_file..."
File.foreach(id_file) do |line|
# Using String#strip because each line has a new line character at the end
line_content = line.strip
unless line_content.empty? || line.strip =~ /^\d+$/
Expand All @@ -62,15 +63,23 @@ namespace :atc do
end
puts 'Validation passed.'
# Then actually yield each id
File.foreach(source_object_id_file) do |line|
File.foreach(id_file) do |line|
# Using String#strip because each line has a new line character at the end
line_content = line.strip
next if line_content.empty?
yield line.strip.to_i
end
elsif ENV['source_object_path'].present?
source_object_id = SourceObject.for_path(ENV['source_object_path'])&.id

unless source_object_id.present?
puts 'Error: could not find a SourceObject with the given source_object_path'
return false
end
yield id
else
puts 'Please specify one or more source ids, using one of the following arguments: '\
'source_object_id, source_object_path, source_object_id_file'
"#{prefix}_id, #{prefix}_id_file"
return false
end

Expand All @@ -82,7 +91,7 @@ namespace :atc do
task create_fixity_checksum: :environment do
enqueue_successor = parse_enqueue_successor_argument()

next unless with_source_id_argument() do |source_object_id|
next unless with_source_object_id_argument() do |source_object_id|
puts "Queued source_object_id: #{source_object_id}"
CreateFixityChecksumJob.perform_later(source_object_id, enqueue_successor: enqueue_successor)
end
Expand All @@ -95,7 +104,7 @@ namespace :atc do
task prepare_transfer: :environment do
enqueue_successor = parse_enqueue_successor_argument()

next unless with_source_id_argument() do |source_object_id|
next unless with_source_object_id_argument() do |source_object_id|
puts "Queued source_object_id: #{source_object_id}"
PrepareTransferJob.perform_later(source_object_id, enqueue_successor: enqueue_successor)
end
Expand All @@ -106,10 +115,12 @@ namespace :atc do
desc "Queue a PerformTransferJob. "\
"This job converts a PendingTransfer to a StoredObject while transferring a file."
task perform_transfer: :environment do
pending_transfer_id = parse_integer_argument('pending_transfer_id')
next if pending_transfer_id.nil?
next unless with_pending_transfer_id_argument() do |pending_transfer_id|
puts "Queued pending_transfer_id: #{pending_transfer_id}"
PerformTransferJob.perform_later(pending_transfer_id)
end

PerformTransferJob.perform_later(pending_transfer_id.to_i)
puts 'Done'
end
end
end

0 comments on commit 76c1675

Please sign in to comment.