-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perform most subscription filtering in memory for speed
- Loading branch information
Showing
7 changed files
with
100 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,56 @@ | ||
class AlertEmail::Base < ApplicationJob | ||
def perform | ||
MAXIMUM_RESULTS_PER_RUN = 500 | ||
|
||
FILTERS = { | ||
teaching_job_roles: ->(vacancy, value) { (vacancy.job_roles & value).any? }, | ||
support_job_roles: ->(vacancy, value) { (vacancy.job_roles & value).any? }, | ||
visa_sponsorship_availability: ->(vacancy, value) { value.include? vacancy.visa_sponsorship_available.to_s }, | ||
ect_statuses: ->(vacancy, value) { value.include?(vacancy.ect_status) }, | ||
subjects: ->(vacancy, value) { (vacancy.subjects & value).any? }, | ||
phases: ->(vacancy, value) { (vacancy.phases & value).any? }, | ||
working_patterns: ->(vacancy, value) { (vacancy.working_patterns & value).any? }, | ||
organisation_slug: ->(vacancy, value) { vacancy.organisations.map(&:slug).include?(value) }, | ||
}.freeze | ||
|
||
def perform # rubocop:disable Metrics/AbcSize | ||
return if DisableExpensiveJobs.enabled? | ||
|
||
subscriptions.find_each do |subscription| | ||
next if subscription.alert_run_today? | ||
# The intent here is that if we don't have keyword or location searches, then this operation can all be done in memory | ||
# really fast (1 week's worth of vacancies is around 2000, so not worth leaving on disk for each of 100k daily subscriptions | ||
default_scope = Vacancy.includes(:organisations).live.order(publish_on: :desc).search_by_filter(from_date: from_date, to_date: Date.current) | ||
|
||
already_run_ids = AlertRun.for_today.map(&:subscription_id) | ||
|
||
vacancies = vacancies_for_subscription(subscription) | ||
next if vacancies.blank? | ||
subscriptions.find_each.reject { |sub| already_run_ids.include?(sub.id) }.each do |subscription| | ||
scope = default_scope | ||
criteria = subscription.search_criteria.symbolize_keys | ||
scope, criteria = handle_keywords(scope, criteria) | ||
scope, criteria = handle_location(scope, criteria) | ||
|
||
Jobseekers::AlertMailer.alert(subscription.id, vacancies.pluck(:id)).deliver_later | ||
vacancies = scope.select do |vacancy| | ||
criteria.all? { |criterion, value| FILTERS.fetch(criterion).call(vacancy, value) } | ||
end | ||
|
||
Jobseekers::AlertMailer.alert(subscription.id, vacancies.pluck(:id)).deliver_later if vacancies.any? | ||
end | ||
Sentry.capture_message("#{self.class.name} run successfully", level: :info) | ||
end | ||
|
||
def vacancies_for_subscription(subscription) | ||
subscription.vacancies_for_range(from_date, Date.current) | ||
private | ||
|
||
def handle_keywords(scope, criteria) | ||
if criteria.key?(:keyword) | ||
[scope.search_by_full_text(criteria[:keyword]), criteria.except(:keyword)] | ||
else | ||
[scope, criteria] | ||
end | ||
end | ||
|
||
def handle_location(scope, criteria) | ||
if criteria.key?(:location) | ||
[scope.search_by_location(criteria[:location], criteria[:radius]), criteria.except(:location, :radius)] | ||
else | ||
[scope, criteria] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
class AlertRun < ApplicationRecord | ||
enum :status, { pending: 0, sent: 1 } | ||
belongs_to :subscription | ||
|
||
scope :for_today, -> { where(run_on: Date.current) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters