Module: MassSearchWorker::InstanceMethods

Defined in:
app/workers/concerns/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:) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/workers/concerns/mass_search_worker.rb', line 38

def build_enumerator(args, cursor:)
  args = args.with_indifferent_access

  @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) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/workers/concerns/mass_search_worker.rb', line 59

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_completeObject



82
83
84
85
86
87
88
89
90
91
# File 'app/workers/concerns/mass_search_worker.rb', line 82

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