-
-
Save g-gundam/cff4e8fd81740a60e95ba563cae37597 to your computer and use it in GitHub Desktop.
This is davinci's watchdog code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This file is machine-generated - editing it directly is not advised | |
| julia_version = "1.11.2" | |
| manifest_format = "2.0" | |
| project_hash = "f3e12818d79d35ab70781d89b14a3d00c4df0f7c" | |
| [[deps.Artifacts]] | |
| uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" | |
| version = "1.11.0" | |
| [[deps.Base64]] | |
| uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" | |
| version = "1.11.0" | |
| [[deps.BitFlags]] | |
| git-tree-sha1 = "0691e34b3bb8be9307330f88d1a3c3f25466c24d" | |
| uuid = "d1d4a3ce-64b1-5f1a-9ba4-7e7e69966f35" | |
| version = "0.1.9" | |
| [[deps.CodecZlib]] | |
| deps = ["TranscodingStreams", "Zlib_jll"] | |
| git-tree-sha1 = "bce6804e5e6044c6daab27bb533d1295e4a2e759" | |
| uuid = "944b1d66-785c-5afd-91f1-9de20f533193" | |
| version = "0.7.6" | |
| [[deps.ConcurrentUtilities]] | |
| deps = ["Serialization", "Sockets"] | |
| git-tree-sha1 = "f36e5e8fdffcb5646ea5da81495a5a7566005127" | |
| uuid = "f0e56b4a-5159-44fe-b623-3e5288b988bb" | |
| version = "2.4.3" | |
| [[deps.Dates]] | |
| deps = ["Printf"] | |
| uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" | |
| version = "1.11.0" | |
| [[deps.ExceptionUnwrapping]] | |
| deps = ["Test"] | |
| git-tree-sha1 = "d36f682e590a83d63d1c7dbd287573764682d12a" | |
| uuid = "460bff9d-24e4-43bc-9d9f-a8973cb893f4" | |
| version = "0.1.11" | |
| [[deps.HTTP]] | |
| deps = ["Base64", "CodecZlib", "ConcurrentUtilities", "Dates", "ExceptionUnwrapping", "Logging", "LoggingExtras", "MbedTLS", "NetworkOptions", "OpenSSL", "PrecompileTools", "Random", "SimpleBufferStream", "Sockets", "URIs", "UUIDs"] | |
| git-tree-sha1 = "c67b33b085f6e2faf8bf79a61962e7339a81129c" | |
| uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3" | |
| version = "1.10.15" | |
| [[deps.InteractiveUtils]] | |
| deps = ["Markdown"] | |
| uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" | |
| version = "1.11.0" | |
| [[deps.JLLWrappers]] | |
| deps = ["Artifacts", "Preferences"] | |
| git-tree-sha1 = "a007feb38b422fbdab534406aeca1b86823cb4d6" | |
| uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" | |
| version = "1.7.0" | |
| [[deps.Libdl]] | |
| uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" | |
| version = "1.11.0" | |
| [[deps.Logging]] | |
| uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" | |
| version = "1.11.0" | |
| [[deps.LoggingExtras]] | |
| deps = ["Dates", "Logging"] | |
| git-tree-sha1 = "f02b56007b064fbfddb4c9cd60161b6dd0f40df3" | |
| uuid = "e6f89c97-d47a-5376-807f-9c37f3926c36" | |
| version = "1.1.0" | |
| [[deps.Markdown]] | |
| deps = ["Base64"] | |
| uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" | |
| version = "1.11.0" | |
| [[deps.MbedTLS]] | |
| deps = ["Dates", "MbedTLS_jll", "MozillaCACerts_jll", "NetworkOptions", "Random", "Sockets"] | |
| git-tree-sha1 = "c067a280ddc25f196b5e7df3877c6b226d390aaf" | |
| uuid = "739be429-bea8-5141-9913-cc70e7f3736d" | |
| version = "1.1.9" | |
| [[deps.MbedTLS_jll]] | |
| deps = ["Artifacts", "Libdl"] | |
| uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" | |
| version = "2.28.6+0" | |
| [[deps.MozillaCACerts_jll]] | |
| uuid = "14a3606d-f60d-562e-9121-12d972cd8159" | |
| version = "2023.12.12" | |
| [[deps.NetworkOptions]] | |
| uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" | |
| version = "1.2.0" | |
| [[deps.OpenSSL]] | |
| deps = ["BitFlags", "Dates", "MozillaCACerts_jll", "OpenSSL_jll", "Sockets"] | |
| git-tree-sha1 = "38cb508d080d21dc1128f7fb04f20387ed4c0af4" | |
| uuid = "4d8831e6-92b7-49fb-bdf8-b643e874388c" | |
| version = "1.4.3" | |
| [[deps.OpenSSL_jll]] | |
| deps = ["Artifacts", "JLLWrappers", "Libdl"] | |
| git-tree-sha1 = "7493f61f55a6cce7325f197443aa80d32554ba10" | |
| uuid = "458c3c95-2e84-50aa-8efc-19380b2a3a95" | |
| version = "3.0.15+3" | |
| [[deps.PrecompileTools]] | |
| deps = ["Preferences"] | |
| git-tree-sha1 = "5aa36f7049a63a1528fe8f7c3f2113413ffd4e1f" | |
| uuid = "aea7be01-6a6a-4083-8856-8a6e6704d82a" | |
| version = "1.2.1" | |
| [[deps.Preferences]] | |
| deps = ["TOML"] | |
| git-tree-sha1 = "9306f6085165d270f7e3db02af26a400d580f5c6" | |
| uuid = "21216c6a-2e73-6563-6e65-726566657250" | |
| version = "1.4.3" | |
| [[deps.Printf]] | |
| deps = ["Unicode"] | |
| uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" | |
| version = "1.11.0" | |
| [[deps.Random]] | |
| deps = ["SHA"] | |
| uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | |
| version = "1.11.0" | |
| [[deps.SHA]] | |
| uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" | |
| version = "0.7.0" | |
| [[deps.Serialization]] | |
| uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" | |
| version = "1.11.0" | |
| [[deps.SimpleBufferStream]] | |
| git-tree-sha1 = "f305871d2f381d21527c770d4788c06c097c9bc1" | |
| uuid = "777ac1f9-54b0-4bf8-805c-2214025038e7" | |
| version = "1.2.0" | |
| [[deps.Sockets]] | |
| uuid = "6462fe0b-24de-5631-8697-dd941f90decc" | |
| version = "1.11.0" | |
| [[deps.TOML]] | |
| deps = ["Dates"] | |
| uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" | |
| version = "1.0.3" | |
| [[deps.Test]] | |
| deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] | |
| uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | |
| version = "1.11.0" | |
| [[deps.TranscodingStreams]] | |
| git-tree-sha1 = "0c45878dcfdcfa8480052b6ab162cdd138781742" | |
| uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa" | |
| version = "0.11.3" | |
| [[deps.URIs]] | |
| git-tree-sha1 = "67db6cc7b3821e19ebe75791a9dd19c9b1188f2b" | |
| uuid = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4" | |
| version = "1.5.1" | |
| [[deps.UUIDs]] | |
| deps = ["Random", "SHA"] | |
| uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" | |
| version = "1.11.0" | |
| [[deps.Unicode]] | |
| uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" | |
| version = "1.11.0" | |
| [[deps.Zlib_jll]] | |
| deps = ["Libdl"] | |
| uuid = "83775a58-1f1d-513f-b197-d71354ab007a" | |
| version = "1.2.13+1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [deps] | |
| HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://discourse.julialang.org/t/best-practices-for-managing-websocket-http-server-lifecycle/124434 | |
| using Base.Threads | |
| using HTTP | |
| using HTTP: WebSockets | |
| ws_watchdog = [] | |
| ws_array = [] | |
| ws_ip = "127.0.0.1" | |
| ws_port = 8080 | |
| function start_ws_watchdog() | |
| ## on first run | |
| # terminate any running watchdog(s) - by InterruptException | |
| terminate_active_watchdog!() | |
| # terminate any active websockets | |
| terminate_active_ws!() | |
| sleep(1) | |
| # start new watchdog | |
| watchdog_task = @spawn try | |
| #ws_array |> empty! | |
| while true | |
| try | |
| # start websocket server if there's none or they're all done/closed | |
| if isempty(ws_array) || all(ws -> istaskdone(ws.task), ws_array) || all(ws -> !isopen(ws.listener.server), ws_array) | |
| start_ws_server() | |
| end | |
| catch e | |
| if isa(e, Base.IOError) && occursin("EADDRINUSE", e.msg) | |
| # port already in use, re-start was too early | |
| @warn "WARNING - ws start, port already in use $e" | |
| sleep(1) | |
| terminate_active_ws!() | |
| else | |
| rethrow(e) | |
| end | |
| end | |
| sleep(5) | |
| end | |
| catch e | |
| if isa(e, InterruptException) | |
| @warn "LOG - Websocket watchdog terminated" | |
| else | |
| @error "ERROR - ws watchdog, $e" | |
| rethrow(e) | |
| end | |
| end | |
| @info "LOG - New watchdog started: $watchdog_task" | |
| # save watchdog task reference | |
| push!(ws_watchdog, watchdog_task) | |
| end | |
| function start_ws_server() | |
| # start new server (it spawns its on task) | |
| ws_server = WebSockets.listen!(ws_ip, ws_port; verbose = true) do ws | |
| _info = "LOG - New Websocket server started: $(ws_server.task)" | |
| @info _info | |
| for msg in ws | |
| @spawn try | |
| # save received messages as-is | |
| lock(msg_lock) | |
| push!(ws_msg_log_raw, msg) | |
| unlock(msg_lock) | |
| parsed_msg = JSON3.read(msg, Dict) |> dict_keys_to_sym | |
| # save parsed message | |
| setindex!(parsed_msg, "received", :type) | |
| lock(msg_lock) | |
| push!(ws_msg_log, parsed_msg) | |
| unlock(msg_lock) | |
| # pass msg on | |
| msg_handler(ws, parsed_msg) | |
| catch e | |
| @error "ERROR - Message handler error, $e" | |
| end | |
| end | |
| end | |
| # saves server handler & task for reference | |
| push!(ws_array, ws_server) | |
| end | |
| function terminate_active_ws!() | |
| for ws in filter(ws -> !istaskdone(ws.task) || !isempty(ws.connections) || isopen(ws.listener.server), ws_array) | |
| HTTP.forceclose(ws) | |
| _num_conn = ws.connections |> length | |
| @warn "WARNING - Terminating ws server: $(ws.task) - $_num_conn connections" | |
| end | |
| end | |
| function terminate_active_watchdog!() | |
| for task in ws_watchdog | |
| if !istaskdone(task) | |
| schedule(task, InterruptException(), error=true) | |
| @warn "WARNING - Terminating ws watchdog: $task" | |
| end | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instructions
In a terminal:
Then in the Julia REPL.
In another terminal:
Discussion
https://discourse.julialang.org/t/best-practices-for-managing-websocket-http-server-lifecycle/124434