From 4bcc0977500af7af070296e2faf817eed10aafb9 Mon Sep 17 00:00:00 2001
From: Zeger-Jan van de Weg <zegerjan@gitlab.com>
Date: Wed, 17 Feb 2016 14:41:33 +0100
Subject: [PATCH] A worker deletes a user, so the request doesn't time out

Fixes #13261
---
 CHANGELOG                                 |  1 +
 app/controllers/admin/users_controller.rb |  2 +-
 app/workers/delete_user_worker.rb         | 10 ++++++++++
 spec/workers/delete_user_worker_spec.rb   | 22 ++++++++++++++++++++++
 4 files changed, 34 insertions(+), 1 deletion(-)
 create mode 100644 app/workers/delete_user_worker.rb
 create mode 100644 spec/workers/delete_user_worker_spec.rb

diff --git a/CHANGELOG b/CHANGELOG
index 7f076f70c7c..6de17a4a098 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -178,6 +178,7 @@ v 8.5.0
 
 v 8.4.5
   - No CE-specific changes
+  - User deletion is now done in the background so the request can not time out
 
 v 8.4.4
   - Update omniauth-saml gem to 1.4.2
diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb
index 87f4fb455b8..3fe33018d73 100644
--- a/app/controllers/admin/users_controller.rb
+++ b/app/controllers/admin/users_controller.rb
@@ -122,7 +122,7 @@ class Admin::UsersController < Admin::ApplicationController
     DeleteUserService.new(current_user).execute(user)
 
     respond_to do |format|
-      format.html { redirect_to admin_users_path }
+      format.html { redirect_to admin_users_path, notice: "The user is being deleted." }
       format.json { head :ok }
     end
   end
diff --git a/app/workers/delete_user_worker.rb b/app/workers/delete_user_worker.rb
new file mode 100644
index 00000000000..bcbb6d58b4f
--- /dev/null
+++ b/app/workers/delete_user_worker.rb
@@ -0,0 +1,10 @@
+class DeleteUserWorker
+  include Sidekiq::Worker
+
+  def perform(current_user_id, delete_user_id)
+    delete_user  = User.find(delete_user_id)
+    current_user = User.find(current_user_id)
+
+    DeleteUserService.new(current_user).execute(delete_user)
+  end
+end
diff --git a/spec/workers/delete_user_worker_spec.rb b/spec/workers/delete_user_worker_spec.rb
new file mode 100644
index 00000000000..73005c507c6
--- /dev/null
+++ b/spec/workers/delete_user_worker_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe DeleteUserWorker do
+  describe "Deletes a user and all their personal projects" do
+    let!(:user)         { create(:user) }
+    let!(:current_user) { create(:user) }
+    let!(:namespace)    { create(:namespace, owner: user) }
+    let!(:project)      { create(:project, namespace: namespace) }
+
+    before do
+      DeleteUserWorker.new.perform(current_user.id, user.id)
+    end
+
+    it 'deletes all personal projects' do
+      expect { Project.find(project.id) }.to raise_error(ActiveRecord::RecordNotFound)
+    end
+
+    it 'deletes the user' do
+      expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
+    end
+  end
+end
-- 
GitLab