Skip to content

Refactor scheduler for SM

Bishwa Hang Rai requested to merge 432214-sm-schedule-refresh-worker into master

What does this MR do and why?

Refactors scheduler worker to consider correct Feature flag. The scheduler ScheduleBulkRefreshUserAssignmentsWorker now checks feature flag based on context (SaaS or Self-Managed), and schedules the BulkRefreshUserAssignmentsWorker .

Removed the feature flag guards from BulkRefreshUserAssignmentsWorker itself, as it is redundant, because it only gets called from ScheduleBulkRefreshUserAssignmentsWorker . Less code.

Lastly, adds safe navigation for namespace&. as BulkRefreshUserAssignmentsWorker will also get called in the context of Self-Managed instance, where namespace will be nil .

Screenshots or screen recordings

Sidekiq Admin Panel for cron jobs: link here

Screenshot

Screenshot 2023-12-06 at 14.59.36.png

How to set up and validate locally

  1. Make sure that GDK is run as self-managed instance, i.e., GITLAB_SIMULATE_SAAS=0 gdk restart
  2. Open up a rails console GITLAB_SIMULATE_SAAS=0 gdk rails c
  3. Create a new AddOnPurchase record for testing:
# cleanup any existing old test records
::GitlabSubscriptions::AddOnPurchase.destroy_all
# create an add_on record if not already created
add_on = GitlabSubscriptions::AddOn.find_or_create_by!(name: "code_suggestions") {|e| e.description = "Test"}
# create add_on_purchase record without the namespace, mimicking SM instance
add_on_purchase = GitlabSubscriptions::AddOnPurchase.create!(add_on: add_on, expires_on: 1.year.from_now, quantity: 10, purchase_xid: 'A-12345')

user_1 = User.find 69 # John Doe6
user_2 = User.find 70 # John Doe7

# assign seat to the user
add_on_purchase.assigned_users.create(user: user_1)
add_on_purchase.assigned_users.create(user: user_2)

user_2.block! # user_2 is ineligible

# check the current assigned_users count
add_on_purchase.assigned_users.count # 2

# enable the feature flag for SM
Feature.enable(:self_managed_code_suggestions)

# restart GDK background-job server, just in case
GITLAB_SIMULATE_SAAS=0 gdk restart rails-background-jobs

# Schedule the limited capacity worker via scheduler, it should log multiple jobs
# You can run `gdk tail -F` rails-background-jobs in separate console to follow logs
GitlabSubscriptions::AddOnPurchases::BulkRefreshUserAssignmentsWorker.new.perform

# alternatively, you could also use GUI to do so with:
# http://gdk.test:3000/admin/sidekiq/cron/gitlab_subscriptions_add_on_purchases_schedule_bulk_refresh_user_assignments_worker

# check the job was executed correctly: user_1 should still have its seat as it is eligible
add_on_purchase.assigned_users.count # 1
add_on_purchase.last_assigned_users_refreshed_at # not null

All passing specs in CI tests that SaaS context is still working fine. It can also be validated by following this steps: !132419 (merged)

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #432214

Edited by Bishwa Hang Rai

Merge request reports