Skip to content

Refactor job refresh_user_assignments_worker

What does this MR do and why?

The code changes uses the new service, GitlabSubscriptions::AddOnPurchases::ReconcileSeatOverageService, which is responsible for reconciling seat overage for add-on purchases. This service is used in two worker classes, BulkRefreshUserAssignmentsWorker and RefreshUserAssignmentsWorker, to delete ineligible user assignments and then delete any overage seat assignments. Additionally, the code refactors the way the add_on_purchase instance variable is retrieved, making it work when namespace_id is nil.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

How to set up and validate locally

  1. Check out this branch
  2. Create a new root group namespace
  3. Setup some seed records
namespace = Namespace.last
add_on = GitlabSubscriptions::AddOn.find_or_create_by!(name: "code_suggestions") {|e| e.description = "Test"}

# create new add_on_purchase 
add_on_purchase = GitlabSubscriptions::AddOnPurchase.create!(
  add_on: add_on, namespace: namespace, expires_on: 1.month.from_now, quantity: 5, purchase_xid: 'A-S0001', created_at: 1.week.ago
)

# get any 3 users that is not owner/member of the add_on_purchase.namespace
user_1, user_2, user_3 = User.last(3)

# add users as developer to namespace
add_on_purchase.namespace.add_developer(user_1)
add_on_purchase.namespace.add_developer(user_2)

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

add_on_purchase.assigned_users.count # 3
add_on_purchase.update!(quantity: 1) # create overage

# removes user_3 as it is not eligible because it is not a member of namespace
# removes user_2 because of overage seat and is is recently added
GitlabSubscriptions::AddOnPurchases::RefreshUserAssignmentsWorker.new.perform(add_on_purchase.namespace_id)
add_on_purchase.reload.assigned_users.where(user: user_1).count # 1

# As another option, we can call the UpdateService which will enqueue the job and perform reconciliation
# However, make sure that job is processed by checking the gdk
# $ gdk restart rails-background-jobs
# 

GitlabSubscriptions::AddOnPurchases::UpdateService.new(add_on_purchase.namespace, add_on_purchase.add_on, {quantity: 1}).execute
add_on_purchase.reload.assigned_users.where(user: user_1).count
  1. We can repeat the above steps (1-3) again to create an overage for an add_on_purchase
  2. Call the worker: GitlabSubscriptions::AddOnPurchases::BulkRefreshUserAssignmentsWorker.perform_with_capacity
  3. It will remove any overage seat

Related to #456823

Edited by Bishwa Hang Rai

Merge request reports