Class: Projects::DestroyService

Inherits:
BaseService show all
Includes:
Gitlab::ShellAdapter
Defined in:
app/services/projects/destroy_service.rb

Constant Summary collapse

DestroyError =
Class.new(StandardError)
BATCH_SIZE =
100

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from Gitlab::ShellAdapter

#gitlab_shell

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?, #can_all?, #can_any?

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#async_executeObject



10
11
12
13
14
15
# File 'app/services/projects/destroy_service.rb', line 10

def async_execute
  project.update_attribute(:pending_delete, true)

  job_id = ProjectDestroyWorker.perform_async(project.id, current_user.id, params)
  log_info("User #{current_user.id} scheduled destruction of project #{project.full_path} with job ID #{job_id}")
end

#executeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/services/projects/destroy_service.rb', line 17

def execute
  unless can?(current_user, :remove_project, project)
    project.update_attribute(:pending_delete, false) if project.pending_delete?
    return false
  end

  project.update_attribute(:pending_delete, true)

  # There is a possibility of active repository move processes for
  # project and snippets. An attempt to delete the project at the same time
  # can lead to race condition and an inconsistent state.
  #
  # This validation stops the project delete process if it detects active
  # repository move schedules for it.
  validate_active_repositories_move!

  # Flush the cache for both repositories. This has to be done _before_
  # removing the physical repositories as some expiration code depends on
  # Git data (e.g. a list of branch names).
  flush_caches(project)

  ::Ci::AbortPipelinesService.new.execute(all_pipelines, :project_deleted)

  Projects::UnlinkForkService.new(project, current_user).execute(refresh_statistics: false)

  attempt_destroy(project)

  execute_hooks(project)

  Gitlab::AppLogger.info(
    class: self.class.name,
    message: "Project \"#{project.full_path}\" was deleted",
    project_id: project.id,
    full_path: project.full_path
  )

  publish_project_deleted_event_for(project)

  project.invalidate_personal_projects_count_of_owner

  true
rescue StandardError => error
  context = Gitlab::ApplicationContext.current.merge(project_id: project.id)
  Gitlab::ErrorTracking.track_exception(error, **context)
  attempt_rollback(project, error.message)
  false
rescue Exception => error # rubocop:disable Lint/RescueException
  # Project.transaction can raise Exception
  attempt_rollback(project, error.message)
  raise
end