Skip to content

Commit

Permalink
refactoring classes AsyncBatchDestroyJob and AsyncBatchUpdateJob
Browse files Browse the repository at this point in the history
refactoring classes according to the interface ActiveJob
  • Loading branch information
Ivanov-Anton committed Jun 30, 2020
1 parent 4d0aad6 commit b9385af
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 45 deletions.
34 changes: 24 additions & 10 deletions app/jobs/async_batch_destroy_job.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
# frozen_string_literal: true

class AsyncBatchDestroyJob
class AsyncBatchDestroyJob < ApplicationJob
include BatchJobsLog
BATCH_SIZE = 1000
queue_as 'batch_actions'

attr_reader :model_class, :sql_query, :who_is
attr_reader :model_class, :who_is

def initialize(model_class, sql_query, who_is)
@model_class = model_class
@sql_query = sql_query
def perform(model_class, sql_query, who_is)
@model_class = model_class.constantize
@who_is = who_is
end

def perform
set_audit_log_data
begin
scoped_records = model_class.constantize.find_by_sql(sql_query + " LIMIT #{BATCH_SIZE}")
model_class.constantize.transaction do
scoped_records = @model_class.find_by_sql(sql_query + " LIMIT #{BATCH_SIZE}")
@model_class.transaction do
scoped_records.each(&:destroy!)
end
end until scoped_records.empty?
Expand All @@ -29,4 +26,21 @@ def reschedule_at(_time, _attempts)
def max_attempts
3
end

after_perform do |job|
LogicLog.create!(
source: "#{self.class} #{job.job_id}",
level: 0,
msg: 'Success'
)
end

discard_on(Exception) do |job, error|
LogicLog.create!(
source: "#{job.class} #{job.job_id}",
level: 0,
msg: "error message: #{error.message}\nclass: #{job.arguments[0]}\nsql: #{job.arguments[1]}\nqueue name: #{job.queue_name}"
)
raise error.class
end
end
36 changes: 25 additions & 11 deletions app/jobs/async_batch_update_job.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
# frozen_string_literal: true

class AsyncBatchUpdateJob
class AsyncBatchUpdateJob < ApplicationJob
include BatchJobsLog
BATCH_SIZE = 1000
queue_as 'batch_actions'

attr_reader :model_class, :sql_query, :changes, :who_is
attr_reader :model_class, :sql_query, :who_is

def initialize(model_class, sql_query, changes, who_is)
@model_class = model_class
def perform(model_class, sql_query, changes, who_is)
@model_class = model_class.constantize
@sql_query = sql_query
@changes = changes
@who_is = who_is
end

def perform
set_audit_log_data
model_class.constantize.transaction do
total_count = model_class.constantize.count_by_sql count_sql_query
@model_class.transaction do
total_count = @model_class.count_by_sql count_sql_query

(total_count.to_f / BATCH_SIZE).ceil.times do |batch_number|
offset = batch_number * BATCH_SIZE
scoped_records = model_class.constantize.find_by_sql(order_by_id_sql + " OFFSET #{offset} LIMIT #{BATCH_SIZE}")
scoped_records = @model_class.find_by_sql(order_by_id_sql + " OFFSET #{offset} LIMIT #{BATCH_SIZE}")
scoped_records.each { |record| record.update!(changes) }
end
end
Expand All @@ -47,4 +44,21 @@ def reschedule_at(_time, _attempts)
def max_attempts
3
end

after_perform do |job|
LogicLog.create!(
source: "#{self.class} #{job.job_id}",
level: 0,
msg: "Success, details: queue_name: #{job.queue_name}, class_name: #{job.arguments[0]}"
)
end

discard_on(Exception) do |job, error|
LogicLog.create!(
source: "#{job.class} #{job.job_id}",
level: 0,
msg: "error message: #{error.message}\nclass: #{job.arguments[0]}\nchanges: #{job.arguments[2]}\nsql: #{job.arguments[1]}\nqueue name: #{job.queue_name}"
)
raise error.class
end
end
16 changes: 0 additions & 16 deletions lib/batch_jobs_log.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
# frozen_string_literal: true

module BatchJobsLog
def success(job)
LogicLog.create!(
source: "#{self.class} #{job.id}",
level: 0,
msg: 'Success'
)
end

def failure(job)
LogicLog.create!(
source: "#{self.class} #{job.id}",
level: 0,
msg: job.last_error
)
end

def set_audit_log_data
::PaperTrail.request.whodunnit = who_is[:whodunnit]
::PaperTrail.request.controller_info = who_is[:controller_info]
Expand Down
6 changes: 3 additions & 3 deletions lib/resource_dsl/acts_as_async_destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def acts_as_async_destroy(model_class)
scoped_collection_action :async_destroy,
title: 'Delete batch',
if: proc { authorized?(:batch_destroy, resource_klass) } do
Delayed::Job.enqueue AsyncBatchDestroyJob.new(model_class,
AsyncBatchDestroyJob.set(queue: 'batch_actions').perform_later(model_class,
scoped_collection_records.except(:eager_load).to_sql,
@paper_trail_info),
queue: 'batch_actions'
@paper_trail_info)

flash[:notice] = I18n.t('flash.actions.batch_actions.batch_destroy.job_scheduled')
head :ok
end
Expand Down
5 changes: 2 additions & 3 deletions lib/resource_dsl/acts_as_async_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ def acts_as_async_update(model_class, attrs_to_update)
class: 'scoped_collection_action_button ui',
form: attrs_to_update,
if: proc { authorized?(:batch_destroy, resource_klass) } do
Delayed::Job.enqueue AsyncBatchUpdateJob.new(model_class,
AsyncBatchUpdateJob.set(queue: 'batch_actions').perform_later(model_class,
scoped_collection_records.except(:eager_load).to_sql,
params[:changes].permit!,
@paper_trail_info),
queue: 'batch_actions'
@paper_trail_info)
flash[:notice] = I18n.t('flash.actions.batch_actions.batch_update.job_scheduled')
head :ok
end
Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/async_batch_destroy_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
include_context :init_destination, id: 2, initial_rate: 0.5
include_context :init_destination, id: 3, initial_rate: 0.7

subject { described_class.new(model_class, sql_query, who_is).perform }
subject { described_class.perform_now(model_class, sql_query, who_is) }

before :each do
stub_const('AsyncBatchDestroyJob::BATCH_SIZE', 2)
Expand Down
11 changes: 10 additions & 1 deletion spec/jobs/async_batch_update_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
include_context :init_destination, id: 2, initial_rate: 0.5
include_context :init_destination, id: 3, initial_rate: 0.7

subject { described_class.new(model_class, sql_query, changes, who_is).perform }
subject { described_class.perform_now(model_class, sql_query, changes, who_is) }

before :each do
stub_const('AsyncBatchUpdateJob::BATCH_SIZE', 2)
Expand Down Expand Up @@ -69,6 +69,15 @@
it { expect { subject }.to change(Routing::Destination.where(id: 1, prefix: 300), :count).by(1) }
end
end

context 'logic log' do
let(:changes) { { next_interval: '4' } }
let(:sql_query) { Routing::Destination.all.to_sql }

it 'should ' do
expect { subject }.to change(LogicLog, :count).by 1
end
end
end
end
end

0 comments on commit b9385af

Please sign in to comment.