Skip to content

Commit 3e1f0d8

Browse files
sciccostevecrozz
authored andcommitted
moving destroy of problems into background jobs (errbit#1151)
1 parent 4f35fa3 commit 3e1f0d8

14 files changed

+116
-6
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,7 @@ gem 'jquery-rails', '~> 2.1.4'
112112
gem 'pjax_rails'
113113
gem 'underscore-rails'
114114

115+
gem 'sucker_punch'
116+
115117
ENV['USER_GEMFILE'] ||= './UserGemfile'
116118
eval_gemfile ENV['USER_GEMFILE'] if File.exist?(ENV['USER_GEMFILE'])

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ GEM
388388
colorize (>= 0.7.0)
389389
net-scp (>= 1.1.2)
390390
net-ssh (>= 2.8.0)
391+
sucker_punch (2.0.2)
392+
concurrent-ruby (~> 1.0.0)
391393
term-ansicolor (1.3.2)
392394
tins (~> 1.0)
393395
therubyracer (0.12.2)
@@ -487,6 +489,7 @@ DEPENDENCIES
487489
rubocop
488490
rushover
489491
sass-rails
492+
sucker_punch
490493
therubyracer
491494
timecop
492495
uglifier

app/controllers/problems_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ def unmerge_several
114114
end
115115

116116
def destroy_several
117-
nb_problem_destroy = ProblemDestroy.execute(selected_problems)
118-
flash[:notice] = "#{I18n.t(:n_errs_have, count: nb_problem_destroy)} #{I18n.t('n_errs_have.been_deleted')}."
117+
DestroyProblemsByIdJob.perform_later(selected_problems_ids)
118+
flash[:notice] = "#{I18n.t(:n_errs, count: selected_problems.size)} #{I18n.t('n_errs.will_be_deleted')}."
119119
redirect_to :back
120120
end
121121

122122
def destroy_all
123-
nb_problem_destroy = ProblemDestroy.execute(app.problems)
124-
flash[:success] = "#{I18n.t(:n_errs_have, count: nb_problem_destroy)} #{I18n.t('n_errs_have.been_deleted')}."
123+
DestroyProblemsByAppJob.perform_later(app.id)
124+
flash[:success] = "#{I18n.t(:n_errs, count: app.problems.count)} #{I18n.t('n_errs.will_be_deleted')}."
125125
redirect_to :back
126126
rescue ActionController::RedirectBackError
127127
redirect_to app_path(app)

app/controllers/problems_searcher.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ module ProblemsSearcher
2525
Array(Problem.find(err_ids))
2626
end
2727

28+
expose(:selected_problems_ids) do
29+
selected_problems.map(&:id).map(&:to_s)
30+
end
31+
2832
expose(:err_ids) do
2933
(params[:problems] || []).compact
3034
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class DestroyProblemsByAppJob < ActiveJob::Base
2+
queue_as :default
3+
4+
def perform(app_id)
5+
app = App.find_by(id: app_id)
6+
::ProblemDestroy.execute(app.problems)
7+
end
8+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class DestroyProblemsByIdJob < ActiveJob::Base
2+
queue_as :default
3+
4+
def perform(problem_ids)
5+
bson_problem_ids = []
6+
problem_ids.each do |id|
7+
bson_problem_ids << BSON::ObjectId.from_string(id)
8+
end
9+
problems = Problem.find(bson_problem_ids).to_a
10+
::ProblemDestroy.execute(problems)
11+
end
12+
end

config/application.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,7 @@ class Application < Rails::Application
4848

4949
# Configure Devise mailer to use our mailer layout.
5050
config.to_prepare { Devise::Mailer.layout 'mailer' }
51+
52+
config.active_job.queue_adapter = :sucker_punch
5153
end
5254
end

config/initializers/sucker_punch.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'sucker_punch/async_syntax'

config/locales/en.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ en:
1010
success: "Good news everyone! '%{app_name}' was successfully updated."
1111
destroy:
1212
success: "'%{app_name}' was successfully destroyed."
13-
13+
n_errs:
14+
one: "%{count} error"
15+
other: "%{count} errors"
16+
will_be_deleted: will be deleted
1417
n_errs_have:
1518
one: "%{count} error has"
1619
other: "%{count} errors have"

config/locales/pt-BR.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ pt-BR:
1111
destroy:
1212
success: "'%{app_name}' foi destruído com sucesso."
1313

14+
n_errs:
15+
one: "%{count} erro"
16+
other: "%{count} erros"
17+
will_be_deleted: serà excluído(s)
1418
n_errs_have:
1519
one: "%{count} erro foi"
1620
other: "%{count} erros foram"

0 commit comments

Comments
 (0)