mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-16 10:12:03 +00:00
feat: add allocated server readiness control
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
extends SceneTree
|
||||
|
||||
const ServerControlScript = preload("res://scripts/server_control.gd")
|
||||
const PORT := 18080
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
var control = ServerControlScript.new()
|
||||
root.add_child(control)
|
||||
if control.start(PORT, "drain-secret") != OK:
|
||||
printerr("server control failed to bind")
|
||||
quit(1)
|
||||
return
|
||||
control.set_process_ready(true)
|
||||
await process_frame
|
||||
var ready_response := await _request("GET", "/ready", [])
|
||||
if ready_response != 200:
|
||||
printerr("ready response was %d" % ready_response)
|
||||
quit(1)
|
||||
return
|
||||
var unauthorized := await _request("POST", "/drain", ["Authorization: Bearer wrong"])
|
||||
if unauthorized != 401:
|
||||
printerr("unauthorized drain response was %d" % unauthorized)
|
||||
quit(1)
|
||||
return
|
||||
var drained := await _request("POST", "/drain", ["Authorization: Bearer drain-secret"])
|
||||
if drained != 202 or not control.is_draining():
|
||||
printerr("authorized drain response/state was %d/%s" % [drained, control.is_draining()])
|
||||
quit(1)
|
||||
return
|
||||
var not_ready := await _request("GET", "/ready", [])
|
||||
if not_ready != 503:
|
||||
printerr("draining ready response was %d" % not_ready)
|
||||
quit(1)
|
||||
return
|
||||
control.stop()
|
||||
print("server control smoke passed")
|
||||
quit(0)
|
||||
|
||||
|
||||
func _request(method: String, path: String, headers: PackedStringArray) -> int:
|
||||
var request := HTTPRequest.new()
|
||||
root.add_child(request)
|
||||
var http_method := HTTPClient.METHOD_GET if method == "GET" else HTTPClient.METHOD_POST
|
||||
var err := request.request("http://127.0.0.1:%d%s" % [PORT, path], headers, http_method)
|
||||
if err != OK:
|
||||
request.queue_free()
|
||||
return -1
|
||||
var result = await request.request_completed
|
||||
request.queue_free()
|
||||
return int(result[1])
|
||||
Reference in New Issue
Block a user