Loading
Cancel in-flight imports when a user is blocked or banned
What does this MR do and why?
When an admin blocks or bans a user, their in-flight imports keep running. The block/ban transitions only drop CI pipelines; nothing touches import state.
This MR adds Users::CancelImportsForBlockedUserWorker, enqueued after commit from the any => :blocked and active => :banned transitions in User, mirroring Users::DropPipelinesForBlockedUserWorker. It cancels:
ProjectImportStates inscheduled/startedfor projects created by the user (created_projects.each_batch+is_importing), viaimport_state.cancel. Every remaining import job then short-circuits onimport_state.completed?.- Direct Transfer
BulkImports increated/startedowned by the user, viabulk_import.cancel(which already propagates the cancel to its entities).
Notes:
- The ban hook enqueues the worker on all instances, not only inside the existing
Gitlab.com?branch. - The worker calls the state machine events directly instead of
Import::Github::CancelProjectImportService, because that service checkscan?(current_user, :cancel_project_import, project), which a blocked user fails. As a result the GitHub-specifictrack_canceled_importmetric does not fire; the genericcancel_project_importinternal event still fires from theProjectImportStatetransition. - Each cancellation is isolated: a failure (for example a race with the import finishing) is logged and the rest continue. Counts are logged with
log_extra_metadata_on_done.
Scope, as proposed in the issue:
- Only admin
blockandban. Notldap_blocked,deactivatedorblocked_pending_approval. - Unblocking does not resume imports.
- Half-imported projects are left in place.
References
Database
- Projects:
projects WHERE creator_id = ? AND idrange (each_batch) usesindex_projects_on_creator_id_and_id; theis_importingjoin reachesproject_mirror_datathrough its uniqueproject_idindex. - Bulk imports:
bulk_imports WHERE user_id = ? AND status IN (0, 1)usesindex_bulk_imports_on_user_id.
How to set up and validate locally
-
In a Rails console, start an import that stays in progress, or fake one:
user = User.find_by_username('<a non-admin user>') project = Project.find(<a project created by that user>) project.import_state || project.create_import_state! project.import_state.update_columns(status: 'started') bulk_import = BulkImport.create!(user: user, source_type: :gitlab, source_version: '17.0', status: 1) -
Block (or ban) the user:
user.block!(oruser.ban!). -
Once Sidekiq has run
Users::CancelImportsForBlockedUserWorker:project.import_state.reload.status # => "canceled" bulk_import.reload.status_name # => :canceled
Edited by Rodrigo Tomonari