diff --git a/app/jobs/async_batch_destroy_job.rb b/app/jobs/async_batch_destroy_job.rb index 0896389cd..f111b9e3d 100644 --- a/app/jobs/async_batch_destroy_job.rb +++ b/app/jobs/async_batch_destroy_job.rb @@ -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? diff --git a/app/jobs/async_batch_update_job.rb b/app/jobs/async_batch_update_job.rb index a285920e2..c8015d9d1 100644 --- a/app/jobs/async_batch_update_job.rb +++ b/app/jobs/async_batch_update_job.rb @@ -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 diff --git a/lib/resource_dsl/acts_as_async_destroy.rb b/lib/resource_dsl/acts_as_async_destroy.rb index d85295e5d..3039e23be 100644 --- a/lib/resource_dsl/acts_as_async_destroy.rb +++ b/lib/resource_dsl/acts_as_async_destroy.rb @@ -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' diff --git a/lib/resource_dsl/acts_as_async_update.rb b/lib/resource_dsl/acts_as_async_update.rb index b46db0a5a..f07be7e28 100644 --- a/lib/resource_dsl/acts_as_async_update.rb +++ b/lib/resource_dsl/acts_as_async_update.rb @@ -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), diff --git a/spec/jobs/async_batch_destroy_job_spec.rb b/spec/jobs/async_batch_destroy_job_spec.rb index 976cdb9b5..01f126a2e 100644 --- a/spec/jobs/async_batch_destroy_job_spec.rb +++ b/spec/jobs/async_batch_destroy_job_spec.rb @@ -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) diff --git a/spec/jobs/async_batch_update_job_spec.rb b/spec/jobs/async_batch_update_job_spec.rb index fb1cae05c..6fdc26b4f 100644 --- a/spec/jobs/async_batch_update_job_spec.rb +++ b/spec/jobs/async_batch_update_job_spec.rb @@ -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)