Skip to content

Commit

Permalink
5 strategies (#6)
Browse files Browse the repository at this point in the history
* Implement #5

* Bumped version to 0.5.0
  • Loading branch information
attdona authored Feb 29, 2024
1 parent ffd5055 commit dd76161
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 6 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Change Log

## 0.4.0
## 0.5.0 (29 February, 2024)

- Fix `one_for_all` and `rest_for_all` strategies [#5](https://github.com/cardo-org/Visor.jl/issues/5)

## 0.4.0 (1 February, 2024)

- Update supervisor status at process termination [#3](https://github.com/cardo-org/Visor.jl/issues/3)

## 0.3.0
## 0.3.0 (24 January, 2024)

- Setup supervisors settings with `setsupervisor` and `setroot` functions.

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.4.0"
version = "0.5.0"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
18 changes: 16 additions & 2 deletions src/Visor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,18 @@ function restart_policy(supervisor, process)
@debug "[$process] process retry startstamps: $(process.startstamps)"
if length(process.startstamps) > supervisor.intensity
@warn "[$process]: reached max of $(supervisor.intensity) restarts in $(supervisor.period) secs period"

# removing failed processes make possible to manually startup again
stopped = [process]
if supervisor.strategy === :one_for_all
stopped = supervisor_shutdown(supervisor)
elseif supervisor.strategy === :rest_for_one
stopped = supervisor_shutdown(supervisor, process)
end

for p in stopped
delete!(supervisor.processes, p.id)
end
put!(supervisor.inbox, ProcessFatal(process))
else
process.isrestart = true
Expand Down Expand Up @@ -899,8 +911,10 @@ end
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)
if isopen(sv.inbox)
put!(sv.inbox, Shutdown(; reset=reset))
close(sv.inbox)
end
wait(sv.task)
sv.status = idle
end
Expand Down
8 changes: 7 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ logging(; debug=DEBUG == "0" ? [] : [Visor])
@time @safetestset "chain_restart" begin
include("test_chain_restart.jl")
end
@time @safetestset "one_for_all_terminate" begin
include("test_one_for_all_terminate.jl")
end
@time @safetestset "one_for_all" begin
include("test_one_for_all.jl")
end
@time @safetestset "rest_for_one_terminate" begin
include("test_rest_for_one_terminate.jl")
end
@time @safetestset "rest_for_one" begin
include("test_rest_for_one.jl")
end
Expand All @@ -55,4 +61,4 @@ logging(; debug=DEBUG == "0" ? [] : [Visor])
end
end

@info "expected tests: 43"
@info "expected tests: 49"
2 changes: 2 additions & 0 deletions test/test_chain_restart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ p1_specs = [process("good", myworker), process("bad", faulty)]
handle = supervise([supervisor("sv", p1_specs; intensity=2)])

@test restarts === 2

shutdown()
75 changes: 75 additions & 0 deletions test/test_one_for_all_terminate.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Visor
using Test

#
#
# root
# /
# /
# s1
# / \
# s11 w3
# / \
# w1 w2
#
# startup order: w1, w2, w3
# if strategy is :one_for_all and the failed process does not restart terminate
# all sibling processes.
#

#ENV["JULIA_DEBUG"] = Visor

starts = 0

stopped = []

function myworker(self)
global starts

@info "[$self]: starting"
starts += 1
if self.id === "w3"
sleep(2)
#if starts < 4
@info "[$self]: THROW EXCEPTION"
error("bang")
#end
else
sleep(20)
end
@info "[$self]: terminate"
# for msg in self.inbox
# @debug "[$(self.id)] recv: $msg"
# if isshutdown(msg)
# push!(stopped, self.id)
# break
# end
# end
end

s11_specs = [
process("w1", myworker; thread=true), process("w2", myworker; force_interrupt_after=1)
]

s1_specs = [
supervisor("s11", s11_specs; intensity=1, terminateif=:shutdown),
process("w3", myworker; force_interrupt_after=1),
]

specs = [supervisor("s1", s1_specs; strategy=:one_for_all)]

handle = Visor.supervise(specs; wait=false)

timer_not_triggered = true
timer = Timer((tim) -> begin
global timer_not_triggered = false
shutdown(handle)
end, 10)

@test wait(handle) === nothing

# if all processes terminate the timer that shutdown thw system
# is not triggered
@test timer_not_triggered
@test starts === 6
close(timer)
66 changes: 66 additions & 0 deletions test/test_rest_for_one_terminate.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
include("utils.jl")

NAME = "Alfred"

EXPECTED_RESTARTS = 7
restart_count = 0

function p2(self)
global restart_count += 1
sleep(1)
return error("simulate a fault")
end

function myworker(self)
global restart_count

@info "(re)starting [$(self.id)]"
restart_count += 1

try
for msg in self.inbox
@info "[$(self.id)] recv: $msg"
if isshutdown(msg)
break
elseif msg.request === :get_name
put!(msg.inbox, NAME)
end
end
catch e
@error "worker: $e"
rethrow()
end
end

workerspecs = [
process("p1", myworker)
process("p2", p2)
process("p3", myworker)
process("p4", myworker)
]

specs = [supervisor("boss", workerspecs; strategy=:rest_for_one)]

@info "starting ..."
handle = Visor.supervise(specs; wait=false)

only_p1_running = false
timer = Timer((tim) -> begin

# check that only p1 is running
boss = from("boss")
procs = boss.processes
if (length(procs) === 1)
global only_p1_running = true
end
@info "boss running processes: $(boss.processes)"
shutdown(handle)
end, 8)

@test wait(handle) === nothing

# if all processes terminate the timer that shutdown thw system
# is not triggered
@test only_p1_running
@test EXPECTED_RESTARTS === restart_count
close(timer)

2 comments on commit dd76161

@attdona
Copy link
Member Author

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/101973

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.5.0 -m "<description of version>" dd761616cdd6205049079de4326110846bc7ce85
git push origin v0.5.0

Please sign in to comment.