Skip to content

Commit

Permalink
Add setsupervisor and setroot functions
Browse files Browse the repository at this point in the history
  • Loading branch information
attdona committed Dec 31, 2023
1 parent 028d5a6 commit a4408ba
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## 0.3.0

- `procs()`: get the supervised processes a s a nested ordered dict.
- Setup supervisors settings with `setsupervisor` and `setroot` functions.

- `procs()`: get the supervised processes as a nested ordered dict.

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

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.2.0"
version = "0.3.0"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
80 changes: 77 additions & 3 deletions src/Visor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ module Visor
using DataStructures
using UUIDs

period = 5

"""
Maximun numbers of restart in period seconds.
"""
intensity = 1

const ROOT_SUPERVISOR = "root"
const NODE_SEP = "."

Expand Down Expand Up @@ -509,7 +516,14 @@ foo process started
function startup(supervisor::Supervisor, proc::Supervised)
if !isdefined(supervisor, :task) || istaskdone(supervisor.task)
# start an empty supervisor
@async supervise(Supervised[])
@async supervise(
Supervised[],
intensity=supervisor.intensity,
period=supervisor.period,
strategy=supervisor.strategy,
terminateif=supervisor.terminateif,
handler=supervisor.evhandler,
)
yield()
end
if haskey(supervisor.processes, proc.id) &&
Expand Down Expand Up @@ -1303,10 +1317,70 @@ function evhandler(process, event)
end
end

supervise() = wait(__ROOT__)
function supervise()
return Base.wait(__ROOT__)
end

"""
setroot(;
intensity::Int=1,
period::Int=5,
strategy::Symbol=:one_for_one,
terminateif::Symbol=:empty,
handler::Union{Nothing,Function}=nothing,
)
Setup root supervisor settings.
"""
setroot(;
intensity::Int=1,
period::Int=5,
strategy::Symbol=:one_for_one,
terminateif::Symbol=:empty,
handler::Union{Nothing,Function}=nothing,
) = setsupervisor(
__ROOT__;
intensity=intensity,
period=period,
strategy=strategy,
terminateif=terminateif,
handler=handler,
)

"""
setsupervisor(sv::Supervisor;
intensity::Int=1,
period::Int=5,
strategy::Symbol=:one_for_one,
terminateif::Symbol=:empty,
handler::Union{Nothing,Function}=nothing,
)
Setup supervisor settings.
"""
function setsupervisor(
sv::Supervisor;
intensity::Int=1,
period::Int=5,
strategy::Symbol=:one_for_one,
terminateif::Symbol=:empty,
handler::Union{Nothing,Function}=nothing,
)
sv.intensity = intensity
sv.period = period
sv.strategy = strategy
sv.terminateif = terminateif
sv.evhandler = handler
return sv
end

const __ROOT__::Supervisor = Supervisor(
ROOT_SUPERVISOR, OrderedDict{String,Supervised}(), 1, 5, :one_for_one, :empty
ROOT_SUPERVISOR,
OrderedDict{String,Supervised}(),
intensity,
period,
:one_for_one,
:empty,
)

end

0 comments on commit a4408ba

Please sign in to comment.