• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / tigerdata-app / 1f9ee909-20b2-467d-b493-b5be4964430d

22 Oct 2025 04:57PM UTC coverage: 91.212%. Remained the same
1f9ee909-20b2-467d-b493-b5be4964430d

push

circleci

web-flow
Fixing flaky tests (#2080)

10 times out of 10 one of these tests would fail for me locally.

Really we just have to look at the page for something new so that we are
sure the controller action has finished before we check for something
that is not waiting.

2740 of 3004 relevant lines covered (91.21%)

755.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.83
/app/jobs/file_inventory_job.rb
1
# frozen_string_literal: true
2
class FileInventoryJob < ApplicationJob
5✔
3
  # Create the FileInventoryRequest as soon as the job is created, not when it is performed
4
  before_enqueue do |job|
5✔
5
    project = Project.find(job.arguments.first[:project_id])
8✔
6
    FileInventoryRequest.create(user_id: job.arguments.first[:user_id], project_id: job.arguments.first[:project_id], job_id: @job_id, state: UserRequest::PENDING,
8✔
7
                                request_details: { project_title: project.title })
8
  end
9

10
  # This is required because the before_enqueue does not get called when perform_now is run on a job
11
  # We include both before_perform and before_enqueue so perform_now and perform_later will work as desired
12
  before_perform do |job|
5✔
13
    project = Project.find(job.arguments.first[:project_id])
30✔
14
    inventory_request = FileInventoryRequest.find_by(user_id: job.arguments.first[:user_id], project_id: job.arguments.first[:project_id], job_id: @job_id)
30✔
15
    if inventory_request.blank?
30✔
16
      FileInventoryRequest.create(user_id: job.arguments.first[:user_id], project_id: job.arguments.first[:project_id], job_id: @job_id, state: UserRequest::PENDING,
28✔
17
                                  request_details: { project_title: project.title })
18
    end
19
  end
20

21
  def perform(user_id:, project_id:, mediaflux_session:)
5✔
22
    project = Project.find(project_id)
30✔
23
    raise "Invalid project id #{project_id} for job #{job_id}" if project.nil?
30✔
24
    user = User.find(user_id)
30✔
25
    raise "Invalid user id #{user_id} for job #{job_id}" if user.nil?
28✔
26
    Rails.logger.debug inspect
28✔
27

28
    # set the mediaflux session to the one the user created, do not just utilize the system one
29
    user.mediaflux_from_session({ mediaflux_session: })
28✔
30

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

37
    # Update the job record as completed
38
    update_inventory_request(user_id: user.id, project: project, job_id: @job_id, filename: filename)
24✔
39
  rescue Mediaflux::SessionExpired => e
40
    # do not retry we can not continue if the session is expired
41
    fail_inventory_request(user_id: user.id, project: project, job_id: @job_id)
4✔
42
  end
43

44
  private
5✔
45

46
    def mediaflux_session
5✔
47
      logon_request = Mediaflux::LogonRequest.new
×
48
      logon_request.session_token
×
49
    end
50

51
    def filename_for_export
5✔
52
      raise "Shared location is not configured" if Rails.configuration.mediaflux["shared_files_location"].blank?
28✔
53
      pathname = Pathname.new(Rails.configuration.mediaflux["shared_files_location"])
28✔
54
      pathname.join("#{job_id}.csv").to_s
28✔
55
    end
56

57
    def fail_inventory_request(user_id:, project:, job_id: )
5✔
58
      inventory_request = FileInventoryRequest.find_by(user_id: user_id, project_id: project.id, job_id: job_id)
6✔
59
      request_details = { project_title: project.title, error: "Mediaflux session expired" }
6✔
60
      inventory_request.update(state: UserRequest::FAILED, request_details: request_details,
6✔
61
                                completion_time: Time.current.in_time_zone("America/New_York"))
62
      inventory_request
4✔
63
    rescue ActiveRecord::StatementInvalid
64
      Rails.logger.info "Export - failing inventory request details for project #{project.id} job_id: #{job_id} failed, about to retry"
2✔
65
      ActiveRecord::Base.connection_pool.release_connection
2✔
66
      retry
2✔
67
    end
68

69
    def update_inventory_request(user_id:, project:, job_id:, filename: )
5✔
70
      Rails.logger.info "Export - updating inventory request details for project #{project.id}"
26✔
71
      inventory_request = FileInventoryRequest.find_by(user_id: user_id, project_id: project.id, job_id: job_id)
26✔
72
      request_details = { output_file: filename, project_title: project.title, file_size: File.size(filename) }
26✔
73
      inventory_request.update(state: UserRequest::COMPLETED, request_details: request_details,
26✔
74
                                completion_time: Time.current.in_time_zone("America/New_York"))
75
      Rails.logger.info "Export - updated inventory request details for project #{project.id}"
24✔
76
      inventory_request
24✔
77
    rescue ActiveRecord::StatementInvalid
78
      Rails.logger.info "Export - updating inventory request details for project #{project.id} failed, about to retry"
2✔
79
      ActiveRecord::Base.connection_pool.release_connection
2✔
80
      retry
2✔
81
    end
82
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc