Skip to content

Commit

Permalink
supervise() does not throw if already active
Browse files Browse the repository at this point in the history
  • Loading branch information
attdona committed May 2, 2024
1 parent 43ee286 commit bcba7c1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0.6.1 (2 May, 2024)

- If already active supervise() add processes instead of throwing an exception.

## 0.6.0 (4 March, 2024)

- Add 'one_terminate_all' supervisor strategy.
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Visor"
uuid = "cf786855-3531-4b86-ba6e-3e33dce7dcdb"
authors = ["Attilio Donà"]
version = "0.6.0"
version = "0.6.1"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
44 changes: 25 additions & 19 deletions src/Visor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ struct ProcessFatal <: ExitReason
process::Supervised
end

# Scheduled by supervisor when forcing process node shutdown.
# Scheduled by supervisor when forcing process node shutdown.
struct ProcessInterrupt <: Exception
id::String
end
Expand Down Expand Up @@ -657,7 +657,7 @@ function add_processes(
end

# Terminate all child processes in startup reverse order.
# If invoked with a `failed_proc` argument it terminates the processes as far as `failed_proc`.
# If invoked with a `failed_proc` argument it terminates the processes as far as `failed_proc`.
function supervisor_shutdown(
supervisor, failed_proc::Union{Supervised,Nothing}=nothing, reset::Bool=false
)
Expand Down Expand Up @@ -859,7 +859,7 @@ end
Call the no-argument function `fn` if the `process` restarted.
The function `fn` is not executed at the first start of `process`.
The function `fn` is not executed at the first start of `process`.
"""
function ifrestart(fn::Function, process::Process)
if process.isrestart
Expand Down Expand Up @@ -894,7 +894,7 @@ end
"""
shutdown(node)
Try to shutdown a process or a supervisor.
Try to shutdown a process or a supervisor.
If `node` is a supervisor it first attempts to shutdown all children nodes and then it stop the supervisor.
If some process refuse to shutdown the `node` supervisor is not stopped.
Expand Down Expand Up @@ -959,7 +959,7 @@ shutdown() = shutdown(__ROOT__)
"""
isshutdown(msg)
Returns `true` if message `msg` is a shutdown command.
Returns `true` if message `msg` is a shutdown command.
"""
isshutdown(msg) = isa(msg, Shutdown)

Expand Down Expand Up @@ -992,7 +992,7 @@ end
"""
hassupervised(name::String)
Determine whether the supervised identified by `name` exists.
Determine whether the supervised identified by `name` exists.
"""
function hassupervised(name::String)
try
Expand All @@ -1004,12 +1004,12 @@ function hassupervised(name::String)
end

# from_supervisor(start_node::Supervisor, name::String)::Supervised
#
#
# Return the supervised node identified by relative or full qualified name `name`.
#
#
# If `name` start with a dot, for example `.foo.bar`, then the process search starts
# from the root supervisor.
#
#
# If using a relative qualified name, for example `foo.bar`, the search starts
# from `start_node` supervisor.
function from_supervisor(sv::Supervisor, name::String)
Expand All @@ -1025,7 +1025,7 @@ Given for example the process `mytask` supervised by `mysupervisor`:
supervisor("mysupervisor", [process(mytask)])
then the full name of `mytask` process is `mysupervisor.mytask`.
then the full name of `mytask` process is `mysupervisor.mytask`.
"""
from(name::String) = from_supervisor(__ROOT__, name)

Expand Down Expand Up @@ -1081,10 +1081,10 @@ end
Send a `request` to the process identified by full `name` and wait for a response.
If `timeout` is equal to -1 then waits forever, otherwise if a response is not received
in `timeout` seconds an `ErrorException` is raised.
in `timeout` seconds an `ErrorException` is raised.
The message sent to the target task is a `Request` struct that contains the request and a channel for sending back the response.
```julia
using Visor
Expand Down Expand Up @@ -1233,7 +1233,7 @@ end
The root supervisor start a family of supervised `processes`.
Return the root supervisor or wait for supervisor termination if `wait` is true.
Return the root supervisor or wait for supervisor termination if `wait` is true.
# Arguments
Expand All @@ -1243,7 +1243,7 @@ Return the root supervisor or wait for supervisor termination if `wait` is true.
- `:one_for_one`: only the terminated task is restarted.
- `:one_for_all`: if a child task terminates, all other tasks are terminated, and then all children,
including the terminated one, are restarted.
- `:rest_for_one`: if a child task terminates, the rest of the children tasks (that is, the child tasks
- `:rest_for_one`: if a child task terminates, the rest of the children tasks (that is, the child tasks
after the terminated process in start order) are terminated. Then the terminated
child task and the rest of the child tasks are restarted.
- `:one_terminate_all`: if a child task terminates then the remaining tasks will be concurrently terminated
Expand Down Expand Up @@ -1279,7 +1279,13 @@ function supervise(
end

if isdefined(__ROOT__, :inbox) && isopen(__ROOT__.inbox)
throw(ErrorException("supervise already active"))
for p in processes
startup(p)
end
if wait
Base.wait(__ROOT__)
end
return __ROOT__
else
__ROOT__.inbox = Channel(INBOX_CAPACITY)
__ROOT__.evhandler = handler
Expand Down Expand Up @@ -1332,13 +1338,13 @@ function supervise(wait::Bool=true)
wait && return Base.wait(__ROOT__)
end

#
#
# wait(sv::Supervisor)
#
#
# Wait for supervisor `sv` termination.
#
#
# A supervisor terminates when all of its supervised tasks terminate.
#
#
function Base.wait(sv::Supervisor)
wait(sv.task)
try
Expand Down

2 comments on commit bcba7c1

@attdona
Copy link
Member Author

@attdona attdona commented on bcba7c1 May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/106031

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.1 -m "<description of version>" bcba7c1901b11d33d0b6832093d824e8e12e338a
git push origin v0.6.1

Please sign in to comment.