Skip to content

Commit

Permalink
api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
attdona committed Dec 21, 2023
1 parent 84c1ed6 commit 028d5a6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Fixed @warn message in case of `ProcessFatal`.

- Add `hassupervised(name)` function.

- `from(path)` return `nothing` instead of throwing an exception if process identified by `path` is not found.

## 0.2.0 (3 December, 2023)

- Renamed functions:
Expand Down
28 changes: 24 additions & 4 deletions src/Visor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export cast
export from
export ifrestart
export process
export hassupervised
export isprocstarted
export isrequest
export isshutdown
Expand Down Expand Up @@ -717,6 +718,9 @@ function manage(supervisor)
reply(msg, add_node(supervisor, msg.request))
elseif isa(msg.request, String)
process = from_path(supervisor, msg.request)
if process === nothing
throw(UnknownProcess(msg.request))
end
put!(msg.inbox, process)
else
unknown_message(supervisor, msg.request)
Expand Down Expand Up @@ -840,7 +844,6 @@ end
# Stops all managed children processes and terminate supervisor `process`.
function shutdown(sv::Supervisor, reset::Bool=true)
@debug "[$sv] supervisor: shutdown request (reset=$reset)"

if isdefined(sv, :task) && !istaskdone(sv.task)
put!(sv.inbox, Shutdown(; reset=reset))
close(sv.inbox)
Expand Down Expand Up @@ -900,6 +903,20 @@ function wait_response(resp_cond, ch)
end
end

"""
hassupervised(name::String)
Determine whether the supervised identified by `name` exists.
"""
function hassupervised(name::String)
try
from(name)
return true
catch
return false
end
end

# from_supervisor(start_node::Supervisor, name::String)::Supervised
#
# Return the supervised node identified by relative or full qualified name `name`.
Expand All @@ -909,7 +926,7 @@ end
#
# If using a relative qualified name, for example `foo.bar`, the search starts
# from `start_node` supervisor.
function from_supervisor(sv::Supervisor, name::String)::Supervised
function from_supervisor(sv::Supervisor, name::String)
return from_path(sv, name)
end

Expand All @@ -926,7 +943,7 @@ then the full name of `mytask` process is `mysupervisor.mytask`.
"""
from(name::String) = from_supervisor(__ROOT__, name)

function from_path(start_node::Supervised, path)::Supervised
function from_path(start_node::Supervised, path)
if path == NODE_SEP
return root_supervisor(start_node)
elseif startswith(path, NODE_SEP)
Expand All @@ -953,7 +970,7 @@ function from_path(start_node::Supervised, path)::Supervised
return from_path(child, join(tokens[2:end], NODE_SEP))
end
end
throw(UnknownProcess(path))
return nothing
end

"""
Expand Down Expand Up @@ -1005,6 +1022,9 @@ supervise([process(server), process(requestor)])
"""
function call(name::String, request::Any; timeout::Real=3)
target_process = from_supervisor(__ROOT__, name)
if target_process === nothing
throw(UnknownProcess(name))
end
return call(target_process, request; timeout=timeout)
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_errors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ running = Visor.nproc(sv)

@test_throws Visor.UnknownProcess call("invalid-process", :some_data)

@test_throws Visor.UnknownProcess getrunning(sv)
@test_throws MethodError getrunning(sv)

@test_throws ErrorException process(worker, restart=:invalid_restart)

Expand Down

0 comments on commit 028d5a6

Please sign in to comment.