Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed alert emails #7318

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Speed alert emails #7318

wants to merge 10 commits into from

Conversation

starswan
Copy link
Contributor

@starswan starswan commented Nov 28, 2024

Trello card URL

https://trello.com/c/w1KM928B/1414-refactor-daily-weeklyalerts-job-for-a-high-volume-of-subscriptions

Changes in this PR:

Currently each (of 100k) subscriptions triggers an SQL query for the same (small) subset of recent vacancies. This change turns that on its head - if there are no keyword or location filters, the filtering is now all done in memory on the 'recent' subset (around 2000 for the 'weekly' email).

Screenshots of UI changes:

Before

After

Next steps:

  • Terraform deployment required?

  • New development configuration to be shared?

@starswan starswan marked this pull request as ready for review November 28, 2024 10:31
@starswan starswan marked this pull request as draft November 28, 2024 10:36
@starswan starswan marked this pull request as ready for review November 28, 2024 12:02
@starswan starswan marked this pull request as draft November 28, 2024 16:07
@starswan starswan marked this pull request as ready for review December 2, 2024 12:17
Comment on lines +116 to 118
config.before(:each, disable_expensive_jobs: true) do
allow(DisableExpensiveJobs).to receive(:enabled?).and_return(true)
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice 👏

@@ -1,19 +1,20 @@
class AlertEmail::Base < ApplicationJob
MAXIMUM_RESULTS_PER_RUN = 500
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed? I only see this removed call

Copy link
Contributor Author

@starswan starswan Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was - I've put the usage back.

app/jobs/alert_email/base.rb Outdated Show resolved Hide resolved
Comment on lines 13 to 14
subscriptions.find_each.reject { |sub| already_run_ids.include?(sub.id) }.each do |subscription|
vacancies = subscription.vacancies_matching default_scope
Copy link
Collaborator

@scruti scruti Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be looping twice over the subscriptions batch: once for the reject, and a second for finding/alerting.
We could avoid the first loop:

Suggested change
subscriptions.find_each.reject { |sub| already_run_ids.include?(sub.id) }.each do |subscription|
vacancies = subscription.vacancies_matching default_scope
subscriptions.find_each.each do |subscription|
next if already_run_ids.include?(subscription.id)
vacancies = subscription.vacancies_matching default_scope

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think it does. find_each is a lazy ierator IIRC so this pattern (which is pretty standard) works efficiently.

Comment on lines +138 to +140
YAML.unsafe_load_file(Rails.root.join("spec/fixtures/polygons.yml")).map(&:attributes).each { |s| LocationPolygon.create!(s) }
YAML.unsafe_load_file(Rails.root.join("spec/fixtures/liverpool_schools.yml")).map(&:attributes).each { |s| School.create!(s) }
create(:vacancy, :published_slugged, slug: "liv", contact_number: "0", organisations: [liverpool_school], job_roles: %w[headteacher], visa_sponsorship_available: false, ect_status: :ect_unsuitable, subjects: %w[German], phases: %w[secondary], working_patterns: %w[full_time])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice test setup for a complex case! 👏

Copy link
Collaborator

@scruti scruti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Left a few comments for possible optimizations, but nothing blocking!

@starswan starswan added the deploy label Dec 4, 2024
Copy link

github-actions bot commented Dec 4, 2024

Review app deployed to https://teaching-vacancies-review-pr-7318.test.teacherservices.cloud on AKS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants