-
Notifications
You must be signed in to change notification settings - Fork 72
THREESCALE-6077: job sync system zync #4307
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
Open
jlledom
wants to merge
6
commits into
master
Choose a base branch
from
THREESCALE-6077-job-sync-system-zync
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+141
−21
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5e5c85e
refactor: extract batch size constant in zync rake tasks
jlledom 9d8e945
feat: add full resync rake task for zync integration
jlledom bf92019
test(zync): add comprehensive coverage for full resync task
jlledom 914140d
refactor(zync): split full resync into flat per-type tasks
jlledom acf4a86
feat(zync): add ResyncEvent for rake task resync operations
jlledom 0cda2e6
refactor(zync): publish ZyncEvents directly in rake tasks
jlledom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,44 +1,69 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| BATCH_SIZE = 100 | ||
|
|
||
| namespace :zync do | ||
| namespace :resync do | ||
| def each_with_progress(scope) | ||
| def each_with_progress(label, scope) | ||
| puts "== Resyncing #{label} ==" | ||
| total_count = scope.count | ||
| batch_size = 100 | ||
| index = 0 | ||
|
|
||
| step = [(total_count / 10), 1].max | ||
| progress = -> do | ||
| break unless (index % batch_size) == 0 | ||
| break unless (index % step).zero? | ||
|
|
||
| percent = (index / total_count.to_f) * 100.0 | ||
| puts "#{percent.round(2)}% completed" | ||
| end | ||
|
|
||
| scope.find_each(batch_size: batch_size) do |object| | ||
| scope.find_each(batch_size: BATCH_SIZE) do |object| | ||
| index += 1 | ||
| yield object | ||
| progress.call | ||
| end | ||
| end | ||
|
|
||
| desc 'Resync provider domains with zync' | ||
| task provider_domains: :environment do | ||
| def active_providers | ||
| accounts = Account.providers_with_master | ||
| if (provider_id = ENV["PROVIDER_ID"]) | ||
| accounts = accounts.where(id: provider_id) | ||
| accounts.where(id: provider_id) | ||
| else | ||
| accounts.without_suspended.without_deleted | ||
| end | ||
| each_with_progress(accounts) { |account| Domains::ProviderDomainsChangedEvent.create_and_publish!(account) } | ||
| end | ||
|
|
||
| desc 'Resync provider domains with zync' | ||
| task providers: :environment do | ||
| each_with_progress('providers', active_providers) { |account| Domains::ProviderDomainsChangedEvent.create_and_publish!(account) } | ||
| end | ||
|
|
||
| task provider_domains: :providers | ||
|
|
||
| desc 'Resync services with zync' | ||
| task services: :environment do | ||
| services = Service.joins(:account).merge(active_providers) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, but you split out proxies I see now 🤔 ok, we go to the other extreme :) alright. |
||
| each_with_progress('services', services) { |service| OIDC::ServiceChangedEvent.create_and_publish!(service) } | ||
| end | ||
|
|
||
| desc 'Resync proxy domains with zync' | ||
| task proxy_domains: :environment do | ||
| services = Service.includes(:proxy) | ||
| if (provider_id = ENV["PROVIDER_ID"]) | ||
| services = services.where(account_id: provider_id) | ||
| end | ||
| each_with_progress(services) { |service| Domains::ProxyDomainsChangedEvent.create_and_publish!(service.proxy) } | ||
| task proxies: :environment do | ||
| proxies = Proxy.joins(service: :account).merge(active_providers) | ||
| each_with_progress('proxies', proxies) { |proxy| Domains::ProxyDomainsChangedEvent.create_and_publish!(proxy) } | ||
| end | ||
|
|
||
| task proxy_domains: :proxies | ||
|
|
||
| desc 'Resync applications with zync' | ||
| task applications: :environment do | ||
| cinstances = Cinstance.joins(service: :account).merge(active_providers) | ||
| each_with_progress('applications', cinstances) { |cinstance| Applications::ApplicationUpdatedEvent.create_and_publish!(cinstance) } | ||
| end | ||
|
|
||
| desc 'Resync all domains with zync' | ||
| task domains: [:provider_domains, :proxy_domains] | ||
| task domains: %i[provider_domains proxy_domains] | ||
|
|
||
| desc 'Full resync' | ||
| task full: %i[providers services proxies applications] | ||
| end | ||
| end | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
hmm, so in the past we didn't filter out inactive providers for this task? Wasteful in some environments.
Previously we had
.includes(:proxy), does it help avoid individual queries? Idk if you checked how many queries actually run. Also I'm not sure whether.includes(:account)will also help because likely we will query the account of the service anyway. But maybejoinsalready includes the data for the account. I haven't checked.btw if we loop over services, because that query already has the provider accounts, that may really reduce the SQL loop if we have a custom action like before, but I don't ask you to make a custom action 👼 just a FYI, don't think the marginal benefit is worth the additional code.
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.
wait, this is a new task. Bob said:
So we can have it if one needs to run independently but maybe redundant to run with the others? Idk, I was just asking 😬
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.
I wasn't aware of this dependency mechanism. I think we don't need to touch anything because it's not really redundant, it depends on the service existing or not. The mechanism is only triggered when zync returns 422, that wouldn't happen when running our
:fulltask because it runs:servicesbefore:proxies.If we run
:proxiesdirectly, then yes, it will sync services, but only after each failed attempt to sync a proxy, so that implies a couple of extra round trips which is suboptimal. I don't think we should remove the:servicestask, since it's the way to avoid the suboptimal path.It also works for applications, that is, if you try to sync an application, it will sync the
serviceandproxyfirst if they don't exist. But again, suboptimal.porta/app/events/zync_event.rb
Lines 42 to 54 in f377917