Class: LimitPointsAdjustmentWorker

Inherits:
ApplicationWorker show all
Includes:
Sidekiq::Status::Worker
Defined in:
app/workers/limit_points_adjustment_worker.rb

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#perform(points_adjustment_id) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/workers/limit_points_adjustment_worker.rb', line 6

def perform(points_adjustment_id)
  success = false
  ActiveRecord::Base.transaction do
    points_adjustment = LimitPointsAdjustment.find(points_adjustment_id)
    raise "Points adjustment campaign status is not 'on_queue'" if points_adjustment.status != 'on_queue'

    # (1) set current active campaign status to expired
    limit_points_adjustment_active = LimitPointsAdjustment.find_by(status: 'active')
    if limit_points_adjustment_active.present?
      limit_points_adjustment_active.update!(status: 'expired', ended_at: Time.zone.now)
    end

    # (2) set on_queue campaign status to active
    points_adjustment.update!(status: 'active')

    # (3) Reset remaining limit points for all users
    LimitPointsUsage.unscoped.in_batches(of: 1000).delete_all
    success = true
  end
  raise 'Something went wrong' if !success
rescue StandardError => e
  APMErrorHandler.report(e, limit_points_adjustment_id: points_adjustment_id)
end