Skip to content

Commit

Permalink
Define #count automatically for batch collections using the enumerator
Browse files Browse the repository at this point in the history
Co-authored-by: Étienne Barrié <etienne.barrie@shopify.com>
  • Loading branch information
adrianna-chang-shopify and etiennebarrie committed Jun 3, 2021
1 parent e59b27b commit d7b7dc5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
12 changes: 7 additions & 5 deletions app/jobs/concerns/maintenance_tasks/task_job_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def retry_on(*, **)
def build_enumerator(_run, cursor:)
cursor ||= @run.cursor
collection = @task.collection
@enumerator = nil

collection_enum = case collection
when ActiveRecord::Relation
Expand All @@ -43,13 +44,13 @@ def build_enumerator(_run, cursor:)
a batch enumerator with the "start" or "finish" options.
MSG
end
enumerator_builder.active_record_on_batch_relations(
# For now, only support automatic count based on the enumerator for
# batches
@enumerator = enumerator_builder.active_record_on_batch_relations(
collection.relation,
cursor: cursor,
batch_size: collection.batch_size,
)
@run.update!(tick_total: enumerator.size)
enumerator
when Array
enumerator_builder.build_array_enumerator(collection, cursor: cursor)
when CSV
Expand Down Expand Up @@ -102,8 +103,9 @@ def before_perform
end

def on_start
@run.tick_total = @task.count unless @run.tick_total
@run.update!(started_at: Time.now)
count = @task.count
count = @enumerator&.size if count == :no_count
@run.update!(started_at: Time.now, tick_total: count)
end

def on_complete
Expand Down
1 change: 1 addition & 0 deletions app/tasks/maintenance_tasks/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def process(_item)
#
# @return [Integer, nil]
def count
:no_count
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ def collection
Post.in_batches(of: 5)
end

def count
Post.count
end

def process(batch_of_posts)
batch_of_posts.update_all(content: "New content added on #{Time.now.utc}")
end
Expand Down
4 changes: 2 additions & 2 deletions test/tasks/maintenance_tasks/task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class TaskTest < ActiveSupport::TestCase
assert_equal 2, Maintenance::TestTask.count
end

test "#count is nil by default" do
test "#count is :no_count by default" do
task = Task.new
assert_nil task.count
assert_equal(:no_count, task.count)
end

test "#collection raises NoMethodError" do
Expand Down

0 comments on commit d7b7dc5

Please sign in to comment.