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 in scheduled/started for projects created by the user (created_projects.each_batch + is_importing), via import_state.cancel. Every remaining import job then short-circuits on import_state.completed?.
  • Direct Transfer BulkImports in created/started owned by the user, via bulk_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 checks can?(current_user, :cancel_project_import, project), which a blocked user fails. As a result the GitHub-specific track_canceled_import metric does not fire; the generic cancel_project_import internal event still fires from the ProjectImportState transition.
  • 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 block and ban. Not ldap_blocked, deactivated or blocked_pending_approval.
  • Unblocking does not resume imports.
  • Half-imported projects are left in place.

References

#631166 (closed)

Database

  • Projects: projects WHERE creator_id = ? AND id range (each_batch) uses index_projects_on_creator_id_and_id; the is_importing join reaches project_mirror_data through its unique project_id index.
  • Bulk imports: bulk_imports WHERE user_id = ? AND status IN (0, 1) uses index_bulk_imports_on_user_id.

How to set up and validate locally

  1. 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)
  2. Block (or ban) the user: user.block! (or user.ban!).

  3. Once Sidekiq has run Users::CancelImportsForBlockedUserWorker:

    project.import_state.reload.status # => "canceled"
    bulk_import.reload.status_name     # => :canceled
Edited by Rodrigo Tomonari

Merge request reports

Loading
Loading