-
Notifications
You must be signed in to change notification settings - Fork 0
/
live_server.jl
84 lines (75 loc) · 2.47 KB
/
live_server.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Revise
using Sockets
# Some async helper utils
macro async_logged(exs...)
if length(exs) == 2
taskname, body = exs
elseif length(exs) == 1
taskname = "Task"
body = only(exs)
end
quote
@async try
$(esc(body))
catch exc
@error string($(esc(taskname)), " failed") exception=(exc,catch_backtrace())
rethrow()
end
end
end
struct CancelToken
cancelled::Ref{Bool}
cond::Threads.Condition
end
CancelToken() = CancelToken(Ref(false), Threads.Condition())
function Base.close(token::CancelToken)
lock(token.cond) do
token.cancelled[] = true
notify(token.cond)
end
end
Base.isopen(token::CancelToken) = lock(()->!token.cancelled[], token.cond)
Base.wait(token::CancelToken) = lock(()->wait(token.cond), token.cond)
#-------------------------------------------------------------------------------
# The server function
function run_server(serve, token::CancelToken, host=ip"127.0.0.1", port=8081)
addr = Sockets.InetAddr(host, port)
server_sockets = Channel(1)
@sync begin
@async_logged "Server" begin
while isopen(token)
@info "Starting server... press ENTER to stop."
socket = Sockets.listen(addr)
try
put!(server_sockets, socket)
Base.invokelatest(serve, socket)
catch exc
if exc isa Base.IOError && !isopen(socket)
# Ok - server restarted
continue
end
close(socket)
rethrow()
end
end
@info "Exited server loop"
end
@async_logged "Revision loop" begin
# This is like Revise.entr but we control the event loop. This is
# necessary because we need to exit this loop cleanly when the user
# cancels the server, regardless of any revision event.
while isopen(token)
@info "Revision event"
wait(Revise.revision_event)
Revise.revise(throw=true)
# Restart the server's listen loop.
close(take!(server_sockets))
end
@info "Exited revise loop"
end
wait(token)
@assert !isopen(token)
notify(Revise.revision_event) # Trigger revise loop one last time.
@info "Server done"
end
end