Use cached monthly commitment for credits cart abandonment
What does this MR do and why?
POST /-/gitlab_subscriptions/hand_raise_leads/track_cart_abandonment with a credits param schedules CartAbandonmentCreditsPurchaseWorker 3 hours out. credits is the namespace's current monthly commitment, used as the baseline to detect a purchase. It comes straight from the request, and the worker's dedup key includes it, so sending a different value on each request schedules a new job every time. Each job also calls CustomersDot when it runs.
This MR stops trusting the request value:
FetchMonthlyCommitmentService: it takes a newcache:option. The default,nil, doesn't touch the cache.:readreads the cached value for the namespace and fetches from CustomersDot on a miss.:refreshalways fetches and overwrites the cached value. Any other value raisesArgumentError. Only successful fetches are cached (1 hour), and client errors are raised in every mode. An unsuccessful CustomersDot response is now returned as an error, still withtotal_credits: 0in the payload, instead of a success with 0 that looks like a real 0. The worker and the two components that already call the service keep the default, so their behavior doesn't change.- Billing page: it already fetches the monthly commitment from CustomersDot on every load. It now uses
cache: :refresh, so each load caches a fresh value for the namespace. - Tracking request: it ignores the
creditsvalue and calls the service withcache: :read. If CustomersDot fails or the client raises, the tracking service handles the error, nothing is cached, and no job is scheduled.
The worker and the dedup key (user_id, namespace_id, credits) are unchanged. Since credits now comes from the server, one user can only schedule one job per namespace per real commitment value, and the tracking endpoint makes at most about one CustomersDot call per namespace per hour.
Everything is behind the cart_abandonment_server_side_credits gitlab_com_derisk flag, checked with the loaded namespace record. With the flag off, nothing changes.
Why not the earlier approaches?
- Validate
creditsagainst allowed amounts:creditsisn't a purchase amount. It's the namespace's running total, which can be any number, and checking the exact value means a CustomersDot call on every request. - Drop
creditsfrom the dedup key (this MR's first version): it stopped the flood, but a user who bought and then abandoned a second purchase within the same 3-hour window lost that lead. It would also have deduplicated serial test runs. Discussed in https://gitlab.com/gitlab-org/gitlab/-/work_items/637422#note_4169999116.
References
- Related to https://gitlab.com/gitlab-org/gitlab/-/work_items/637422
- Follow-up to https://gitlab.com/gitlab-org/gitlab/-/work_items/635543
- Rollout issue: #638615
- Team issue: https://gitlab.com/gitlab-org/gitlab/-/work_items/634743
- Leader epic: https://gitlab.com/groups/gitlab-org/-/epics/24125
Screenshots or screen recordings
Not applicable: backend-only change with no UI.
How to set up and validate locally
-
Run GDK in SaaS mode (
GITLAB_SIMULATE_SAAS=1) and use a free or trial top-level group you own. -
In the Rails console, opt in and enable the flag for the group:
user = User.find_by_username('root') user.update!(onboarding_status_email_opt_in: true) group = Group.find_by_full_path('<your-group>') Feature.enable(:cart_abandonment_server_side_credits, group) -
Visit the group's Settings > Billing page, then check that the commitment was cached:
Rails.cache.read("gitlab_subscriptions:monthly_commitment:#{group.id}") -
In the browser console on any GitLab page, send tracking requests with different
creditsvalues:const t = document.querySelector('meta[name=csrf-token]').content; for (let i = 1; i <= 5; i++) fetch(`/-/gitlab_subscriptions/hand_raise_leads/track_cart_abandonment?namespace_id=<group-id>&credits=${i}`, { method: 'POST', headers: { 'X-CSRF-Token': t } }); -
Count the scheduled jobs. Expect 1 with the flag enabled, and 5 after
Feature.disable(:cart_abandonment_server_side_credits)and a cleanup:w = GitlabSubscriptions::CartAbandonmentCreditsPurchaseWorker Sidekiq::ScheduledSet.new.count { |j| j.klass == w.name } # cleanup Sidekiq::ScheduledSet.new.select { |j| j.klass == w.name }.each(&:delete)
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.