@@ -7,60 +7,58 @@ class StuckImportJobsWorker
7
7
IMPORT_JOBS_EXPIRATION = 15 . hours . to_i
8
8
9
9
def perform
10
- projects_without_jid_count = mark_projects_without_jid_as_failed !
11
- projects_with_jid_count = mark_projects_with_jid_as_failed !
10
+ import_state_without_jid_count = mark_import_states_without_jid_as_failed !
11
+ import_state_with_jid_count = mark_import_states_with_jid_as_failed !
12
12
13
13
Gitlab ::Metrics . add_event ( :stuck_import_jobs ,
14
- projects_without_jid_count : projects_without_jid_count ,
15
- projects_with_jid_count : projects_with_jid_count )
14
+ projects_without_jid_count : import_state_without_jid_count ,
15
+ projects_with_jid_count : import_state_with_jid_count )
16
16
end
17
17
18
18
private
19
19
20
- def mark_projects_without_jid_as_failed !
21
- enqueued_projects_without_jid . each do |project |
22
- project . mark_import_as_failed ( error_message )
20
+ def mark_import_states_without_jid_as_failed !
21
+ enqueued_import_states_without_jid . each do |import_state |
22
+ import_state . mark_as_failed ( error_message )
23
23
end . count
24
24
end
25
25
26
26
# rubocop: disable CodeReuse/ActiveRecord
27
- def mark_projects_with_jid_as_failed!
28
- # TODO: Rollback this change to use SQL through #pluck
29
- jids_and_ids = enqueued_projects_with_jid . map { |project | [ project . import_jid , project . id ] } . to_h
27
+ def mark_import_states_with_jid_as_failed!
28
+ jids_and_ids = enqueued_import_states_with_jid . pluck ( :jid , :id ) . to_h
30
29
31
30
# Find the jobs that aren't currently running or that exceeded the threshold.
32
31
completed_jids = Gitlab ::SidekiqStatus . completed_jids ( jids_and_ids . keys )
33
32
return unless completed_jids . any?
34
33
35
- completed_project_ids = jids_and_ids . values_at ( *completed_jids )
34
+ completed_import_state_ids = jids_and_ids . values_at ( *completed_jids )
36
35
37
- # We select the projects again, because they may have transitioned from
36
+ # We select the import states again, because they may have transitioned from
38
37
# scheduled/started to finished/failed while we were looking up their Sidekiq status.
39
- completed_projects = enqueued_projects_with_jid . where ( id : completed_project_ids )
38
+ completed_import_states = enqueued_import_states_with_jid . where ( id : completed_import_state_ids )
40
39
41
- Rails . logger . info ( "Marked stuck import jobs as failed. JIDs: #{ completed_projects . map ( &:import_jid ) . join ( ', ' ) } " )
40
+ completed_import_state_jids = completed_import_states . map { |import_state | import_state . jid } . join ( ', ' )
41
+ Rails . logger . info ( "Marked stuck import jobs as failed. JIDs: #{ completed_import_state_jids } " )
42
42
43
- completed_projects . each do |project |
44
- project . mark_import_as_failed ( error_message )
43
+ completed_import_states . each do |import_state |
44
+ import_state . mark_as_failed ( error_message )
45
45
end . count
46
46
end
47
47
# rubocop: enable CodeReuse/ActiveRecord
48
48
49
- # rubocop: disable CodeReuse/ActiveRecord
50
- def enqueued_projects
51
- Project . joins_import_state . where ( "(import_state.status = 'scheduled' OR import_state.status = 'started') OR (projects.import_status = 'scheduled' OR projects.import_status = 'started')" )
49
+ def enqueued_import_states
50
+ ProjectImportState . with_status ( [ :scheduled , :started ] )
52
51
end
53
- # rubocop: enable CodeReuse/ActiveRecord
54
52
55
53
# rubocop: disable CodeReuse/ActiveRecord
56
- def enqueued_projects_with_jid
57
- enqueued_projects . where . not ( "import_state. jid IS NULL AND projects.import_jid IS NULL" )
54
+ def enqueued_import_states_with_jid
55
+ enqueued_import_states . where . not ( jid : nil )
58
56
end
59
57
# rubocop: enable CodeReuse/ActiveRecord
60
58
61
59
# rubocop: disable CodeReuse/ActiveRecord
62
- def enqueued_projects_without_jid
63
- enqueued_projects . where ( "import_state. jid IS NULL AND projects.import_jid IS NULL" )
60
+ def enqueued_import_states_without_jid
61
+ enqueued_import_states . where ( jid : nil )
64
62
end
65
63
# rubocop: enable CodeReuse/ActiveRecord
66
64
0 commit comments