dawarich/app/jobs/tracks/cleanup_job.rb

30 lines
845 B
Ruby
Raw Normal View History

2025-07-16 16:22:33 -04:00
# 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|
# 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
2025-07-16 16:22:33 -04:00
.group(:id)
end
end