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 783f021
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 27 deletions.
18 changes: 7 additions & 11 deletions app/jobs/async_batch_destroy_job.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
# 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

def initialize(model_class, sql_query, who_is)
@model_class = model_class
@sql_query = sql_query
@who_is = who_is
end

def perform
def perform(model_class, sql_query, _who_is)
@model_class = model_class.constantize
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 Down
20 changes: 8 additions & 12 deletions app/jobs/async_batch_update_job.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
# 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

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 Down
2 changes: 1 addition & 1 deletion lib/resource_dsl/acts_as_async_destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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,
Delayed::Job.enqueue AsyncBatchDestroyJob.perform_later(model_class,
scoped_collection_records.except(:eager_load).to_sql,
@paper_trail_info),
queue: 'batch_actions'
Expand Down
2 changes: 1 addition & 1 deletion lib/resource_dsl/acts_as_async_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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,
Delayed::Job.enqueue AsyncBatchUpdateJob.perform_later(model_class,
scoped_collection_records.except(:eager_load).to_sql,
params[:changes].permit!,
@paper_trail_info),
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
2 changes: 1 addition & 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

0 comments on commit 783f021

Please sign in to comment.