debian-mirror-gitlab/app/workers/stuck_import_jobs_worker.rb

62 lines
1.7 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
class StuckImportJobsWorker
2018-03-17 18:26:18 +05:30
include ApplicationWorker
2017-08-17 22:00:37 +05:30
include CronjobQueue
2018-03-17 18:26:18 +05:30
IMPORT_JOBS_EXPIRATION = 15.hours.to_i
2017-08-17 22:00:37 +05:30
def perform
2018-03-17 18:26:18 +05:30
projects_without_jid_count = mark_projects_without_jid_as_failed!
projects_with_jid_count = mark_projects_with_jid_as_failed!
Gitlab::Metrics.add_event(:stuck_import_jobs,
projects_without_jid_count: projects_without_jid_count,
projects_with_jid_count: projects_with_jid_count)
end
private
def mark_projects_without_jid_as_failed!
started_projects_without_jid.each do |project|
project.mark_import_as_failed(error_message)
end.count
end
def mark_projects_with_jid_as_failed!
completed_jids_count = 0
started_projects_with_jid.find_in_batches(batch_size: 500) do |group|
2017-08-17 22:00:37 +05:30
jids = group.map(&:import_jid)
# Find the jobs that aren't currently running or that exceeded the threshold.
2018-03-17 18:26:18 +05:30
completed_jids = Gitlab::SidekiqStatus.completed_jids(jids).to_set
2017-08-17 22:00:37 +05:30
if completed_jids.any?
2018-03-17 18:26:18 +05:30
completed_jids_count += completed_jids.count
group.each do |project|
project.mark_import_as_failed(error_message) if completed_jids.include?(project.import_jid)
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
Rails.logger.info("Marked stuck import jobs as failed. JIDs: #{completed_jids.to_a.join(', ')}")
2017-08-17 22:00:37 +05:30
end
end
2018-03-17 18:26:18 +05:30
completed_jids_count
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
def started_projects
Project.with_import_status(:started)
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
def started_projects_with_jid
started_projects.where.not(import_jid: nil)
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
def started_projects_without_jid
started_projects.where(import_jid: nil)
2017-08-17 22:00:37 +05:30
end
def error_message
2018-03-17 18:26:18 +05:30
"Import timed out. Import took longer than #{IMPORT_JOBS_EXPIRATION} seconds"
2017-08-17 22:00:37 +05:30
end
end