@@ -286,24 +286,36 @@ def self.find(id)
286
286
end
287
287
288
288
# Find the server with the lowest load (for creating a new meeting)
289
- def self . find_available ( tag_arg = nil )
290
- # Check if tag is required
291
- tag = tag_arg . presence
292
- tag_required = false
293
- if !tag . nil? && tag [ -1 ] == '!'
294
- tag = tag [ 0 ..-2 ] . presence # always returns String, if tag is String
295
- tag_required = true
289
+ def self . find_available ( tags_arg = nil )
290
+ # Check if passed tags are required
291
+ tags_arg = tags_arg . presence
292
+ tags_required = false
293
+ if !tags_arg . nil? && tags_arg [ -1 ] == '!'
294
+ tags_arg = tags_arg [ 0 ..-2 ] . presence # always returns String, if tag is String
295
+ tags_required = true
296
296
end
297
+ tags = tags_arg &.split ( ',' )
297
298
298
299
# Find available&matching server with the lowest load
299
300
with_connection do |redis |
300
301
ids_loads = redis . zrange ( 'server_load' , 0 , -1 , with_scores : true )
301
302
raise RecordNotFound . new ( "Could not find any available servers." , name , nil ) if ids_loads . blank?
302
- if !tag . nil? && ids_loads . none? { |myid , _ | redis . hget ( key ( myid ) , 'tag' ) == tag }
303
- raise BBBErrors ::ServerTagUnavailableError , tag if tag_required
304
- tag = nil # fall back to servers without tag
303
+
304
+ # build a list of matching tagged servers, otherwise fall back to untagged
305
+ if !tags . nil?
306
+ ids_loads_tagged = [ ]
307
+ tags . each do |tag |
308
+ ids_loads_tagged . concat ids_loads . select { |myid , _ | redis . hget ( key ( myid ) , 'tag' ) == tag }
309
+ end
310
+ if ids_loads_tagged . blank?
311
+ raise BBBErrors ::ServerTagUnavailableError , tags_arg if tags_required
312
+ tags = nil # fall back to servers without tag
313
+ ids_loads_tagged = ids_loads . select { |myid , _ | redis . hget ( key ( myid ) , 'tag' ) == nil }
314
+ end
315
+ ids_loads = ids_loads_tagged
305
316
end
306
- ids_loads = ids_loads . select { |myid , _ | redis . hget ( key ( myid ) , 'tag' ) == tag }
317
+
318
+ # try to select the server with lowest load
307
319
id , load , hash = ids_loads . each do |id , load |
308
320
hash = redis . hgetall ( key ( id ) )
309
321
break id , load , hash if hash . present?
0 commit comments