Class: SpreeCmCommissioner::WaitingRoomSystemMetadataFetcher

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(firestore: nil) ⇒ WaitingRoomSystemMetadataFetcher

Returns a new instance of WaitingRoomSystemMetadataFetcher.



7
8
9
# File 'app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb', line 7

def initialize(firestore: nil)
  @firestore = firestore if firestore.present?
end

Instance Attribute Details

#document_dataObject (readonly)

Returns the value of attribute document_data.



5
6
7
# File 'app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb', line 5

def document_data
  @document_data
end

Class Method Details

.compute_max_sessions_count_with_min(server_running_count:, max_thread_count:, multiplier:, cap:) ⇒ Object



19
20
21
22
23
24
25
# File 'app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb', line 19

def self.compute_max_sessions_count_with_min(server_running_count:, max_thread_count:, multiplier:, cap:)
  min = min_sessions_count
  max = server_running_count * max_thread_count * multiplier / 100
  # Cap the auto-scaled value so the system does not scale beyond the configured ceiling,
  # while ensuring the minimum always wins even if the cap is set below it.
  max.clamp(min, [cap, min].max)
end

.min_sessions_countObject



15
16
17
# File 'app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb', line 15

def self.min_sessions_count
  ENV.fetch('WAITING_ROOM_MIN_SESSIONS_COUNT', '5').to_i
end

Instance Method Details

#documentObject



56
57
58
# File 'app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb', line 56

def document
  @document ||= firestore.col('metadata').doc('system')
end

#firestoreObject



60
61
62
# File 'app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb', line 60

def firestore
  @firestore ||= Google::Cloud::Firestore.new(project_id: [:project_id], credentials: )
end

#load_document_dataObject



11
12
13
# File 'app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb', line 11

def load_document_data
  @document_data = document.get.data
end

#max_sessions_count_capObject

hard ceiling for max_sessions_count to avoid auto-scaling the system too much



52
53
54
# File 'app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb', line 52

def max_sessions_count_cap
  document_data[:max_sessions_count_cap]&.to_i || ENV.fetch('WAITING_ROOM_MAX_SESSIONS_COUNT_CAP', '1000').to_i
end

#max_sessions_count_with_minObject



27
28
29
30
31
32
33
34
# File 'app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb', line 27

def max_sessions_count_with_min
  @max_sessions_count_with_min ||= self.class.compute_max_sessions_count_with_min(
    server_running_count: server_running_count,
    max_thread_count: max_thread_count,
    multiplier: multiplier,
    cap: max_sessions_count_cap
  )
end

#max_thread_countObject



42
43
44
# File 'app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb', line 42

def max_thread_count
  document_data[:max_thread_count]&.to_i || ENV.fetch('WAITING_ROOM_MAX_THREAD_COUNT', '10').to_i
end

#multiplierObject

percentage



47
48
49
# File 'app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb', line 47

def multiplier
  document_data[:multiplier]&.to_i || ENV.fetch('WAITING_ROOM_MULTIPLIER', '150').to_i
end

#server_running_countObject

firebase metadata



38
39
40
# File 'app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb', line 38

def server_running_count
  document_data[:server_running_count]&.to_i || ENV.fetch('WAITING_ROOM_SERVERS_COUNT', '2').to_i
end

#service_accountObject



64
65
66
# File 'app/services/spree_cm_commissioner/waiting_room_system_metadata_fetcher.rb', line 64

def 
  @service_account ||= Rails.application.credentials.
end