Class: ScheduleWorkers::ExpirationPointsWorker

Inherits:
ApplicationWorker show all
Defined in:
app/workers/schedule_workers/expiration_points_worker.rb

Overview

this worker is used to send notification to user about their points expiration and points expired this worker schedule to run at 08 AM bangkok time

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#performObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/workers/schedule_workers/expiration_points_worker.rb', line 5

def perform
  current_time = Time.zone.now
  batch_size = 100

  # Query user benefit expirations for the current day up to end of day
  user_benefit_expirations = UserBenefitExpiry.where(
    'DATE(reminder_d7) = ? OR DATE(expires_at) = ?',
    current_time.to_date, current_time.to_date
  )

  # Process in batches
  user_benefit_expirations.find_in_batches(batch_size: batch_size) do |batch|
    batch.each do |user_benefit_expiry|
      user = user_benefit_expiry.user
      reminder_d7 = user_benefit_expiry.reminder_d7
      expires_at = user_benefit_expiry.expires_at

      # Check for reminders
      if reminder_d7 && reminder_d7.to_date == current_time.to_date
        # we need to run this in anytime within next 24 hours randomly
        # to prevent sidekiq queue to be full since we have a lot of users
        # so we will run this in 24 hours randomly
        next_x_time_in_seconds = rand(1..24.hours.to_i)
        RewardWorkers::PointsExpirationReminder.perform_at(next_x_time_in_seconds.seconds.from_now, user.id)

      end

      if expires_at && expires_at.to_date == current_time.to_date
        # Perform the points expired worker at the expiry date end of day
        expires_at = Time.use_zone(user.time_zone) { user_benefit_expiry.expires_at.end_of_day }
        RewardWorkers::PointsExpired.perform_at(expires_at, user.id)
      end
    end
  end
end