Skip to content

Commit

Permalink
Improved handling of create API call (#1050)
Browse files Browse the repository at this point in the history
* in create call, now check whether meeting is already running before finding available servers

* even more streamlining of the create call handling

* now preliminarily increment server load in join call (too)

* add test for load increment on join call

* add RSpec version of test for load increment on join
  • Loading branch information
Ithanil authored Apr 22, 2024
1 parent 4fb8a7f commit 6d52025
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 27 deletions.
62 changes: 35 additions & 27 deletions app/controllers/bigbluebutton_api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,49 +160,54 @@ def create
params.require(:meetingID)

begin
server = Server.find_available(params[:'meta_server-tag'])
rescue ApplicationRedisRecord::RecordNotFound => e
raise InternalError, e.message
# Check if meeting is already running
meeting = Meeting.find(params[:meetingID], @tenant&.id)
server = meeting.server
logger.debug("Found existing meeting #{params[:meetingID]} on BigBlueButton server #{server.id}.")
rescue ApplicationRedisRecord::RecordNotFound
begin
# Find available server and create meeting on it
server = Server.find_available(params[:'meta_server-tag'])

# Create meeting in database
logger.debug("Creating meeting #{params[:meetingID]} in database.")
moderator_pwd = params[:moderatorPW].presence || SecureRandom.alphanumeric(8)
meeting = Meeting.find_or_create_with_server!(
params[:meetingID],
server,
moderator_pwd,
params[:voiceBridge],
@tenant&.id
)

# Update server if meeting (unexpectedly) already existed on a different server
server = meeting.server

logger.debug("Incrementing server #{server.id} load by 1")
server.increment_load(1)
rescue ApplicationRedisRecord::RecordNotFound => e
raise InternalError, e.message
end
end

# Create meeting in database
logger.debug("Creating meeting #{params[:meetingID]} in database.")

moderator_pwd = params[:moderatorPW].presence || SecureRandom.alphanumeric(8)
params[:moderatorPW] = moderator_pwd

meeting = Meeting.find_or_create_with_server!(
params[:meetingID],
server,
moderator_pwd,
params[:voiceBridge],
@tenant&.id
)

# Update with old server if meeting already existed in database
server = meeting.server

logger.debug("Incrementing server #{server.id} load by 1")
server.increment_load(1)

duration = params[:duration].to_i

params[:moderatorPW] = meeting.moderator_pw
params[:voiceBridge] = meeting.voice_bridge
params[:'meta_tenant-id'] = @tenant.id if @tenant.present?
if server.tag.present?
params[:'meta_server-tag'] = server.tag
else
params.delete(:'meta_server-tag')
end

duration = params[:duration].to_i

# Set/Overite duration if MAX_MEETING_DURATION is set and it's greater than params[:duration] (if passed)
if !Rails.configuration.x.max_meeting_duration.zero? &&
(duration.zero? || duration > Rails.configuration.x.max_meeting_duration)
logger.debug("Setting duration to #{Rails.configuration.x.max_meeting_duration}")
params[:duration] = Rails.configuration.x.max_meeting_duration
end

params[:voiceBridge] = meeting.voice_bridge

if @tenant&.lrs_endpoint.present?
lrs_payload = LrsPayloadService.new(tenant: @tenant, secret: server.secret).call
params[:'meta_secret-lrs-payload'] = lrs_payload if lrs_payload.present?
Expand Down Expand Up @@ -294,6 +299,9 @@ def join
logger.info("The requested meeting #{params[:meetingID]} does not exist")
raise MeetingNotFoundError
end
logger.debug("Incrementing server #{server.id} load by 1")
server.increment_load(1)

# Get list of params that should not be modified by join API call
excluded_params = Rails.configuration.x.join_exclude_params

Expand Down
19 changes: 19 additions & 0 deletions spec/requests/bigbluebutton_api_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,25 @@
expect(response).to redirect_to(encode_bbb_uri("join", server.url, server.secret, params).to_s)
end

it "increments the server load by the value of load_multiplier" do
server.load_multiplier = 7.0
server.save!
meeting = create(:meeting, server: server)

# Reload 1
new_server = Server.find(server.id)
load_before_join = new_server.load

# Join
params = { meetingID: meeting.id, password: "test-password", fullName: "test-name" }
get bigbluebutton_api_join_url, params: params

# Reload 2
new_server = Server.find(server.id)
expected_load = load_before_join + 7.0
expect(new_server.load).to eq(expected_load)
end

it "redirects user to the current join url with only permitted params for join" do
meeting = create(:meeting, server: server)
params = { meetingID: meeting.id, password: "test-password", fullName: "test-name", test1: "", test2: "" }
Expand Down
20 changes: 20 additions & 0 deletions test/controllers/bigbluebutton_api_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,26 @@ class BigBlueButtonApiControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to encode_bbb_uri('join', server1.url, server1.secret, params).to_s
end

test 'join increments the server load by the value of load_multiplier' do
server1 = Server.create(url: 'https://test-1.example.com/bigbluebutton/api/',
secret: 'test-1-secret', enabled: true, online: true, load: 0, load_multiplier: 7.0)
meeting = Meeting.find_or_create_with_server('test-meeting-1', server1, 'mp')

# Reload 1
server1 = Server.find(server1.id)
load_before_join = server1.load

params = { meetingID: meeting.id, moderatorPW: 'mp', fullName: 'test-name' }
BigBlueButtonApiController.stub_any_instance(:verify_checksum, nil) do
get bigbluebutton_api_join_url, params: params
end

# Reload 2
server1 = Server.find(server1.id)
expected_load = load_before_join + 7.0
assert_equal expected_load, server1.load
end

test 'join redirects user to the current join url with only permitted params for join' do
server1 = Server.create(url: 'https://test-1.example.com/bigbluebutton/api/',
secret: 'test-1-secret', enabled: true, load: 0, online: true)
Expand Down

0 comments on commit 6d52025

Please sign in to comment.