From 39bd5f07f5baeee8e42c3dc79fe7713023937dae Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Sun, 23 Feb 2025 13:06:50 -0500 Subject: [PATCH] Speed up sailors log polling --- app/jobs/sailors_log_poll_for_changes_job.rb | 42 ++++++++++++-------- config/initializers/good_job.rb | 2 +- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/app/jobs/sailors_log_poll_for_changes_job.rb b/app/jobs/sailors_log_poll_for_changes_job.rb index 2b19a85..519900c 100644 --- a/app/jobs/sailors_log_poll_for_changes_job.rb +++ b/app/jobs/sailors_log_poll_for_changes_job.rb @@ -2,34 +2,44 @@ class SailorsLogPollForChangesJob < ApplicationJob queue_as :default def perform - # Get all users with enabled preferences - slack_ids = SailorsLogNotificationPreference.where(enabled: true).distinct.pluck(:slack_uid) + puts "performing SailorsLogPollForChangesJob" + # get all users who've coded in the last minute + users_who_coded = Heartbeat.where("created_at > ?", 1.minutes.ago).distinct.pluck(:user_id) + + puts "users_who_coded: #{users_who_coded}" + + # Get all of those with enabled preferences + enabled_users = SailorsLogNotificationPreference.where(enabled: true, slack_uid: users_who_coded).distinct.pluck(:slack_uid) + + puts "enabled_users: #{enabled_users}" + + logs = SailorsLog.where(slack_uid: enabled_users) + + puts "logs: #{logs}" - # for each user, check if their logs have changed - logs = SailorsLog.where(slack_uid: slack_ids).includes(:notification_preferences) logs.each do |log| - # get all projects for the user with time spent in the last 24 hours - projects = Heartbeat.today.where(user_id: log.slack_uid).distinct.pluck(:project) - - new_project_times = Heartbeat.where(user_id: log.slack_uid, project: projects).group(:project).duration_seconds - - projects.each do |project| - new_project_time = new_project_times[project] - if new_project_time > (log.projects_summary[project] || 0) + 1.hour + # get all projects for the user with duration + new_project_times = Heartbeat.where(user_id: log.slack_uid).group(:project).duration_seconds + new_project_times.each do |project, new_project_duration| + if new_project_duration > (log.projects_summary[project] || 0) + 1.hour # create a new SailorsLogSlackNotification log.notification_preferences.each do |preference| log.notifications << SailorsLogSlackNotification.new( slack_uid: log.slack_uid, slack_channel_id: preference.slack_channel_id, project_name: project, - project_duration: new_project_time + project_duration: new_project_duration ) end - log.projects_summary[project] = new_project_time + log.projects_summary[project] = new_project_duration end end - ActiveRecord::Base.transaction do - log.save! if log.changed? + log.save! if log.changed? + + GoodJob::Bulk.enqueue do + log.notifications.where(sent: false).each do |notification| + notification.notify_user! + end end end end diff --git a/config/initializers/good_job.rb b/config/initializers/good_job.rb index 00fe01e..55c1a3b 100644 --- a/config/initializers/good_job.rb +++ b/config/initializers/good_job.rb @@ -13,7 +13,7 @@ Rails.application.configure do class: "LeaderboardUpdateJob" }, sailors_log_poll: { - cron: "*/15 * * * *", + cron: "* * * * *", class: "SailorsLogPollForChangesJob" } }