From a4408ba6925ccd8c9038bf92fa88a994acd0f6f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attilio=20Don=C3=A0?= Date: Sun, 31 Dec 2023 18:39:36 +0100 Subject: [PATCH] Add setsupervisor and setroot functions --- CHANGELOG.md | 4 ++- Project.toml | 2 +- src/Visor.jl | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 81 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ee3a89..60397b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/Project.toml b/Project.toml index 62d0870..d5b5dcc 100755 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/Visor.jl b/src/Visor.jl index 56db3d2..ff37305 100755 --- a/src/Visor.jl +++ b/src/Visor.jl @@ -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 = "." @@ -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) && @@ -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