Skip to content

Commit

Permalink
Use EnumerationContext instead of cursor: keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
sambostock committed Jan 27, 2021
1 parent c5365a9 commit 27d1404
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
5 changes: 3 additions & 2 deletions app/jobs/maintenance_tasks/task_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ def retry_on(*, **)
private

def build_enumerator(_run, cursor:)
cursor ||= @run.cursor
@task.enumerator(cursor: cursor)
context = Task::EnumerationContext.new(cursor: cursor || @run.cursor)

@task.enumerator(context: context)
end

# Performs task iteration logic for the current input returned by the
Expand Down
4 changes: 2 additions & 2 deletions app/tasks/maintenance_tasks/active_record_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class ActiveRecordTask < Task

# TODO: specify abstract_class

def enumerator(cursor:)
def enumerator(context:)
collection = self.collection
assert_relation!(collection)

enumerator_builder.active_record_on_records(collection, cursor: cursor)
enumerator_builder.active_record_on_records(collection, cursor: context.cursor)
end

def collection
Expand Down
4 changes: 2 additions & 2 deletions app/tasks/maintenance_tasks/array_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class ActiveRecordTask < Task

# TODO: Specify abstract_class

def enumerator(cursor:)
def enumerator(context:)
collection = self.collection
assert_array!(collection)

enumerator_builder.array(collection, cursor: cursor)
enumerator_builder.array(collection, cursor: context.cursor)
end

def collection
Expand Down
4 changes: 2 additions & 2 deletions app/tasks/maintenance_tasks/csv_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class CsvTask < Task
# @return [String] the content of the CSV file to process.
attr_accessor :csv_content

def enumerator(cursor:)
JobIteration::CsvEnumerator.new(collection).rows(cursor: cursor)
def enumerator(context:)
JobIteration::CsvEnumerator.new(collection).rows(cursor: context.cursor)
end

# The number of rows to be processed. Excludes the header row from the count
Expand Down
3 changes: 2 additions & 1 deletion app/tasks/maintenance_tasks/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Task
# TODO: Might need to be abstract_class, even if just for consistency

class NotFoundError < NameError; end
EnumerationContext = Struct.new(:cursor, keyword_init: true)

class << self
# Finds a Task with the given name.
Expand Down Expand Up @@ -78,7 +79,7 @@ def load_constants
#
# @raise [NotImplementedError] with a message advising subclasses to
# implement an override for this method.
def enumerator(cursor:)
def enumerator(context:)
raise NotImplementedError,
"#{self.class.name} must implement `enumerator` or inherit from a class which does"
# TODO: Could make error string list available adapters
Expand Down
3 changes: 2 additions & 1 deletion example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ def process(row) BetaFlag[:whatever].enable(row.shop_id) end
class PaymentTask < MaintenanceTasks::Task
# Consumers implement .enumerator instead of .collection, without having to
# know that other tasks actually also use .enumerator internally
def enumerator(cursor:)
def enumerator(context:)
Enumerator.new do |yielder|
cursor = context.cursor
loop do
page = cursor ? InvoiceAPI.fetch(after: cursor) : InvoiceAPI.fetch_all
break if page.empty?
Expand Down

0 comments on commit 27d1404

Please sign in to comment.