dawarich/app/jobs/tracks/cleanup_job.rb
2025-08-21 22:32:29 +02:00

31 lines
917 B
Ruby

# frozen_string_literal: true
# Lightweight cleanup job that runs weekly to catch any missed track generation.
#
# This provides a safety net while avoiding the overhead of daily bulk processing.
class Tracks::CleanupJob < ApplicationJob
queue_as :tracks
sidekiq_options retry: false
def perform(older_than: 1.day.ago)
users_with_old_untracked_points(older_than).find_each do |user|
Rails.logger.info "Processing missed tracks for user #{user.id}"
# Process only the old untracked points
Tracks::Generator.new(
user,
end_at: older_than,
mode: :incremental
).call
end
end
private
def users_with_old_untracked_points(older_than)
User.active.joins(:points)
.where(points: { track_id: nil, timestamp: ..older_than.to_i })
.having('COUNT(points.id) >= 2') # Only users with enough points for tracks
.group(:id)
end
end