Skip to content

Commit

Permalink
Add from_name api
Browse files Browse the repository at this point in the history
  • Loading branch information
attdona committed Nov 17, 2024
1 parent 2f1076b commit a952844
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 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.7.3 (17 November, 2024)

- Added from_name() API method. It may be useful if the process name contains dots.

## 0.7.2 (15 October, 2024)

- Event handler callback fro reporting process lifetime events revisited.
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.7.2"
version = "0.7.3"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
27 changes: 26 additions & 1 deletion src/Visor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const STOP_WAITING_AFTER = Inf
export application
export call
export cast
export from
export from, from_name
export getphase
export ifrestart
export hassupervised
Expand Down Expand Up @@ -1101,6 +1101,31 @@ function from_path(start_node::Supervised, path)
return nothing
end

"""
from_name(container::Supervised, name::AbstractString)
Return the process identified by `name` that is supervised by `container` or
nothing if such process does not exists.
It may be useful in case the process name contains dots because it does not lookup the
supervision hierarchy using the dot as separator between nodes.
"""
function from_name(container::Supervised, name::AbstractString)
if haskey(container.processes, name)
return container.processes[name]
else
return nothing
end
end

"""
from_name(name::AbstractString)
Return the process supervised by the root supervisor that is identified by `name` or
nothing if such process does not exists.
"""
from_name(name::AbstractString) = from_name(__ROOT__, name)

"""
function receive(fn::Function, pd::Process)
Expand Down
19 changes: 13 additions & 6 deletions test/test_hierarchy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ include("utils.jl")
# root
# /\
# / \
# sv1 sv2
# sv1 sv2
# / \
# sv1-3 w3
# /\
# w1 w2
# /\
# w1 w2 127.0.0.1
#

sv2_specs = [process("w3", worker; namedargs=(steps=10, check_interrupt_every=2))]

sv3_specs = [
process("w1", worker; namedargs=(steps=5, check_interrupt_every=1)),
process("w2", worker; namedargs=(steps=10, check_interrupt_every=3)),
process("127.0.0.1", worker; namedargs=(steps=10, check_interrupt_every=3)),
]

sv1_specs = [supervisor("sv1-3", sv3_specs; intensity=1)]
Expand All @@ -28,7 +29,7 @@ function getrunning(handle)
sv = Visor.from_supervisor(handle, "sv1.sv1-3")
nprocs = length(Visor.running_nodes(sv))
@info "supervisor [$(sv.id)] running processes: $nprocs"
return ttrace["getrunning"] = nprocs == 1
return ttrace["getrunning"] = nprocs == 2
end

function shutdown_request(start_node, target_id::String)
Expand All @@ -44,6 +45,12 @@ handle = supervise(specs; wait=false)

sv = from("sv1.sv1-3")

proc = from_name(sv, "127.0.0.1")
@test proc.id == "127.0.0.1"

proc = from_name("127.0.0.1")
@test proc === nothing

# test internal method from_path
# from_path is not thread safe, use from instead
node = Visor.from_path(sv, ".")
Expand All @@ -58,12 +65,12 @@ t1 = Timer((tim) -> shutdown_request(handle, "sv1.sv1-3.w1"), 2)
t2 = Timer((tim) -> getrunning(handle), 3)

n = Visor.nproc(sv)
@test n == 2
@test n == 3

wait(handle)

for (tst, result) in ttrace
@test result
end

shutdown()
shutdown()

2 comments on commit a952844

@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/119626

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

Please sign in to comment.