-
Notifications
You must be signed in to change notification settings - Fork 5
Add batching parameter to salesforce_sync:* tasks #797
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,24 +1,75 @@ | ||||||||||||||||||||||||||
| # frozen_string_literal: true | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| namespace :salesforce_sync do | ||||||||||||||||||||||||||
| # Sync Schools to Salesforce that don't already exist there. | ||||||||||||||||||||||||||
| # Usage: | ||||||||||||||||||||||||||
| # rake salesforce_sync:school # Default: sync up to 25 records | ||||||||||||||||||||||||||
| # LIMIT=100 rake salesforce_sync:school # Sync up to 100 records | ||||||||||||||||||||||||||
| # LIMIT=0 rake salesforce_sync:school # Sync all missing records (unlimited) | ||||||||||||||||||||||||||
| desc 'Sync all Schools to Salesforce' | ||||||||||||||||||||||||||
| task school: :environment do | ||||||||||||||||||||||||||
| limit = ENV['LIMIT'] ? ENV['LIMIT'].to_i : 25 | ||||||||||||||||||||||||||
| limit = nil if limit.zero? | ||||||||||||||||||||||||||
| enqueued = 0 | ||||||||||||||||||||||||||
|
Comment on lines
+11
to
+13
|
||||||||||||||||||||||||||
| checked = 0 | ||||||||||||||||||||||||||
|
Comment on lines
10
to
+14
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| School.find_each do |school| | ||||||||||||||||||||||||||
| Salesforce::SchoolSyncJob.perform_later(school_id: school.id) | ||||||||||||||||||||||||||
| checked += 1 | ||||||||||||||||||||||||||
| unless Salesforce::School.exists?(editoruuid__c: school.id) | ||||||||||||||||||||||||||
| Salesforce::SchoolSyncJob.perform_later(school_id: school.id) | ||||||||||||||||||||||||||
| enqueued += 1 | ||||||||||||||||||||||||||
| break if limit && enqueued >= limit | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
Comment on lines
+18
to
+22
|
||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| puts "Checked #{checked} schools, enqueued #{enqueued} jobs" | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Sync non-student Roles to Salesforce that don't already exist there. | ||||||||||||||||||||||||||
| # Usage: | ||||||||||||||||||||||||||
| # rake salesforce_sync:role # Default: sync up to 25 records | ||||||||||||||||||||||||||
| # LIMIT=100 rake salesforce_sync:role # Sync up to 100 records | ||||||||||||||||||||||||||
| # LIMIT=0 rake salesforce_sync:role # Sync all missing records (unlimited) | ||||||||||||||||||||||||||
| desc 'Sync all non-student Roles to Salesforce' | ||||||||||||||||||||||||||
|
Comment on lines
+28
to
33
|
||||||||||||||||||||||||||
| # Sync non-student Roles to Salesforce that don't already exist there. | |
| # Usage: | |
| # rake salesforce_sync:role # Default: sync up to 25 records | |
| # LIMIT=100 rake salesforce_sync:role # Sync up to 100 records | |
| # LIMIT=0 rake salesforce_sync:role # Sync all missing records (unlimited) | |
| desc 'Sync all non-student Roles to Salesforce' | |
| # Backfill non-student Roles to Salesforce when they don't already exist there. | |
| # Usage: | |
| # rake salesforce_sync:role # Default: sync up to 25 missing records | |
| # LIMIT=100 rake salesforce_sync:role # Sync up to 100 missing records | |
| # LIMIT=0 rake salesforce_sync:role # Sync all missing records (unlimited) | |
| desc 'Backfill missing non-student Roles to Salesforce' |
Copilot
AI
Apr 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ENV['LIMIT'].to_i has the same “invalid string becomes 0 => unlimited” issue here as in the school task. Please validate that LIMIT is numeric and >= 0, and fail fast on invalid values to avoid accidentally enqueueing an unbounded backfill.
Copilot
AI
Apr 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This loop performs Salesforce::Role.exists? per Role, creating an N+1 query pattern against the Salesforce DB. Consider batching existence checks for each find_each batch (query existing affiliation_id__c for the batch’s role IDs) before enqueueing jobs.
Copilot
AI
Apr 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The usage comment says LIMIT=0 will “Sync all records (unlimited)”, but the code only enqueues jobs for Schools whose Contact already exists in Salesforce. Consider updating this wording (and potentially the desc) so the scope/behavior is clear.
| # Sync creator_agree_to_ux_contact for Schools where the Contact exists in Salesforce. | |
| # Usage: | |
| # rake salesforce_sync:contact # Default: sync up to 25 records | |
| # LIMIT=100 rake salesforce_sync:contact # Sync up to 100 records | |
| # LIMIT=0 rake salesforce_sync:contact # Sync all records (unlimited) | |
| desc 'Sync creator_agree_to_ux_contact for all Schools to Salesforce Contact' | |
| # Sync creator_agree_to_ux_contact for Schools whose Contact already exists in Salesforce. | |
| # Usage: | |
| # rake salesforce_sync:contact # Default: enqueue up to 25 eligible schools | |
| # LIMIT=100 rake salesforce_sync:contact # Enqueue up to 100 eligible schools | |
| # LIMIT=0 rake salesforce_sync:contact # Enqueue all eligible schools (unlimited) | |
| desc 'Sync creator_agree_to_ux_contact to Salesforce Contact for Schools whose Contact already exists' |
Copilot
AI
Apr 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ENV['LIMIT'].to_i again turns invalid values into 0 (unlimited). Since this task can enqueue many jobs, it’s safer to validate LIMIT (numeric, >= 0) and abort with a clear message when it’s invalid.
Copilot
AI
Apr 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does Salesforce::Contact.exists? for every School, which is an N+1 query pattern. Consider batching by collecting creator_ids for each find_each batch and querying existing Contacts in one call, then enqueue only for those creator_ids that exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The usage comments indicate this task syncs only missing schools and is LIMITed, but the
descstill reads “Sync all Schools to Salesforce”. Consider updating thedesc/wording to reflect the new behavior so operators don’t assume it will update existing Salesforce records.