Skip to content

Commit

Permalink
Rake task to run a FileInventoryJob from the command line (#1335)
Browse files Browse the repository at this point in the history
* Temporary hack to use a non-CAS session when running the File Inventory Job and test if that causes the performance to slow down

* Allow file inventory to be run from a rake task. Allow user to pass the MF credentials to use

* Rubocop nitpicking

* Better prompt
  • Loading branch information
hectorcorrea authored Mar 6, 2025
1 parent 6d0016b commit efd5ca5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/jobs/file_inventory_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def perform(user_id:, project_id:, mediaflux_session:)

# Queries Mediaflux for the file list and saves it to a CSV file.
filename = filename_for_export
Rails.logger.info "Exporting file list to #{filename} for project #{project_id}"
Rails.logger.info "Exporting file list to #{filename} for project #{project_id} (session: #{mediaflux_session})"
project.file_list_to_file(session_id: mediaflux_session, filename: filename)
Rails.logger.info "Export file generated #{filename} for project #{project_id}"
Rails.logger.info "Export file generated #{filename} for project #{project_id} (session: #{mediaflux_session})"

# Update the job record as completed
update_inventory_request(user_id: user.id, project: project, job_id: @job_id, filename: filename)
Expand Down
12 changes: 11 additions & 1 deletion app/models/mediaflux/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def build_http_request_body(name:)
def build_http_request(name:, form_file: nil)
request = self.class.build_post_request

Rails.logger.debug(xml_payload)
log_xml_request(xml_payload)
set_authentication_headers(request)

if form_file.nil?
Expand All @@ -172,6 +172,16 @@ def build_http_request(name:, form_file: nil)
end
# rubocop:enable Metrics/MethodLength

def log_xml_request(xml_payload)
password_element = xml_payload.match(/\<password\>.*\<\/password\>/)
if password_element.nil?
Rails.logger.debug(xml_payload)
else
# Don't log the password
Rails.logger.debug(xml_payload.gsub(password_element.to_s, "<password>***</password>"))
end
end

# Authentication code to push a few custom HTTP headers to Mediaflux
# Eventually the `session_user` will need to be an object that provides the timeout value.
def set_authentication_headers(request)
Expand Down
32 changes: 32 additions & 0 deletions lib/tasks/file_inventory.rake
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,36 @@ namespace :file_inventory do
request.request_details = { file_size: File.size(filename), output_file: filename, project_title: request.project.title }
request.save!
end

desc "Runs a file inventory job (asks for the MediaFlux credentials to use)"
task :run, [:project_id, :netid] => [:environment] do |_, args|
project_id = args[:project_id]
netid = args[:netid]
project = Project.find(project_id)
user = User.where(uid: netid).first

# Lets the user enter the credentials to use (domain, username, password)
# notice that the password is not displayed
puts "Enter the credentials to connect to MediaFlux"
puts "domain: "
mf_domain = STDIN.gets.chomp

puts "user: "
mf_user = STDIN.gets.chomp

puts "password: "
mf_password = STDIN.getpass.chomp

# Get a MediaFlux session for the credentials entered by the user
logon_request = Mediaflux::LogonRequest.new(domain: mf_domain, user: mf_user, password: mf_password, identity_token: nil, token_type: nil)
mediaflux_session = logon_request.session_token
if logon_request.error?
raise logon_request.response_error[:message]
end

# Schedule the file inventory job using the MediaFlux session we just adquired
puts "Scheduling file inventory using credentials for: #{mf_domain}:#{mf_user}, session: #{mediaflux_session}"
job_request = FileInventoryJob.perform_later(user_id: user.id, project_id: project.id, mediaflux_session: mediaflux_session)
puts "Scheduled file inventory #{job_request.job_id} for project #{project.title} (#{project.id}) user: #{user.uid} (#{user.id}), session: #{mediaflux_session}"
end
end

0 comments on commit efd5ca5

Please sign in to comment.