Module: Workers::MassSearchWorker::InstanceMethods

Defined in:
app/concerns/workers/mass_search_worker.rb

Overview

Core iteration behaviour — included AFTER Sidekiq::IterableJob so these methods
take precedence over Sidekiq's default raise-NotImplementedError stubs.

Instance Method Summary collapse

Instance Method Details

#build_enumerator(args, cursor:) ⇒ ActiveRecord::Relation, Enumerator

Loads the search, user, and locale from the job arguments and builds the
enumerator over the search's results, initialising progress counters.

Parameters:

  • args (Hash)

    string/symbol-keyed job arguments (+search_id+,
    +action_params+, +user_id+, +locale+)

  • cursor (Object, nil)

    resumption cursor supplied by Sidekiq::IterableJob

Returns:

  • (ActiveRecord::Relation, Enumerator)

    enumerator over the search results

See Also:

  • Sidekiq::IterableJob


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/concerns/workers/mass_search_worker.rb', line 46

def build_enumerator(args, cursor:)
  @search_id     = args[:search_id].to_i
  @search        = Search.find(@search_id)
  @action_params = (args[:action_params] || {}).with_indifferent_access
  @user          = Employee.find_by(id: args[:user_id])
  @locale        = args[:locale].presence || 'en'

  CurrentScope.locale = @locale.to_sym

  @updated_count = 0
  @error_count   = 0
  @total_count   = @search.search_results.count

  total @total_count
  at 0, "0 of #{@total_count} processed"

  active_record_records_enumerator(@search.search_results, cursor: cursor)
end

#each_iteration(search_result, _args) ⇒ void

This method returns an undefined value.

Processes a single search result: calls +process_record+ on the underlying
resource, tracks updated/error counts, deletes successful results, and
broadcasts a progress message.

Parameters:

  • search_result (SearchResult)

    the search result whose resource is processed

  • _args (Hash)

    job arguments (unused; state is held on the worker instance)

Raises:

  • (StandardError)

    re-raises after reporting to ErrorReporting



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/concerns/workers/mass_search_worker.rb', line 73

def each_iteration(search_result, _args)
  resource = search_result.resource
  unless resource
    search_result.delete
    return
  end

  if process_record(resource, @action_params, @user)
    @updated_count += 1
    search_result.delete
  else
    @error_count += 1
  end

  current = @updated_count + @error_count
  msg = "#{current} of #{@total_count} processed"
  msg += " (#{@error_count} #{'error'.pluralize(@error_count)})" if @error_count > 0
  at current, msg
rescue StandardError => e
  ErrorReporting.error(e, search_id: @search_id)
  raise
end

#on_completevoid

This method returns an undefined value.

Stores the completion summary message and redirects the user back to the
search page once the job finishes.



100
101
102
103
104
105
106
107
108
109
# File 'app/concerns/workers/mass_search_worker.rb', line 100

def on_complete
  updated = @updated_count || 0
  errors  = @error_count   || 0

  msg = "#{updated} #{'record'.pluralize(updated)} updated."
  msg += " #{errors} could not be updated." if errors > 0

  store info_message: msg,
        redirect_to:  Rails.application.routes.url_helpers.search_path(@search_id)
end