Skip to content

Commit

Permalink
Merge branch 'main' into cv/k8s-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omus authored Mar 8, 2024
2 parents 2e3763c + 50c9629 commit d2fd348
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# K8sDeputy.jl

[![CI](https://github.com/beacon-biosignals/K8sDeputy.jl/actions/workflows/CI.yml/badge.svg)](https://github.com/beacon-biosignals/K8sDeputy.jl/actions/workflows/CI.yml)
[![CI](https://github.com/beacon-biosignals/K8sDeputy.jl/actions/workflows/CI.yaml/badge.svg)](https://github.com/beacon-biosignals/K8sDeputy.jl/actions/workflows/CI.yaml)
[![Code Style: YASGuide](https://img.shields.io/badge/code%20style-yas-violet.svg)](https://github.com/jrevels/YASGuide)
[![Stable Documentation](https://img.shields.io/badge/docs-stable-blue.svg)](https://beacon-biosignals.github.io/K8sDeputy.jl/stable)
[![Dev Documentation](https://img.shields.io/badge/docs-dev-blue.svg)](https://beacon-biosignals.github.io/K8sDeputy.jl/dev)
Expand Down
4 changes: 3 additions & 1 deletion docs/src/health_checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ spec:
timeoutSeconds: 5
```
!!!note
!!! note
K8s probes require that applications must respond to the probe requests in under `timeoutSeconds` (defaults to 1 second). Since Julia's HTTP.jl server can be unresponsive we recommend using a `timeoutSeconds` of at least 5 seconds.

Expand Down Expand Up @@ -110,3 +110,5 @@ Once `shutdown!` is called the following occurs:
3. The Julia process is terminated

By default the `shutdown_handler` only has 5 seconds to complete. If your `shutdown_handler` requires more time to execute you can change the timeout by using the keyword `shutdown_handler_timeout`.

Depending on your application you may want to define multiple calls to `shutdown!`. For example you may want to call `shutdown!` from within `graceful_terminator` to enable [graceful termination support](./graceful_termination.md) for you application.
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# K8sDeputy.jl

[![CI](https://github.com/beacon-biosignals/K8sDeputy.jl/actions/workflows/CI.yml/badge.svg)](https://github.com/beacon-biosignals/K8sDeputy.jl/actions/workflows/CI.yml)
[![CI](https://github.com/beacon-biosignals/K8sDeputy.jl/actions/workflows/CI.yaml/badge.svg)](https://github.com/beacon-biosignals/K8sDeputy.jl/actions/workflows/CI.yaml)
[![Code Style: YASGuide](https://img.shields.io/badge/code%20style-yas-violet.svg)](https://github.com/jrevels/YASGuide)
[![Stable Documentation](https://img.shields.io/badge/docs-stable-blue.svg)](https://beacon-biosignals.github.io/K8sDeputy.jl/stable)
[![Dev Documentation](https://img.shields.io/badge/docs-dev-blue.svg)](https://beacon-biosignals.github.io/K8sDeputy.jl/dev)
Expand Down
91 changes: 67 additions & 24 deletions test/graceful_termination.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,74 @@
@testset "graceful_terminator / graceful_terminate" begin
code = quote
using K8sDeputy
atexit(() -> @info "SHUTDOWN COMPLETE")
graceful_terminator() do
@info "GRACEFUL TERMINATION HANDLER"
exit(2)
return nothing
@testset "graceful_terminator" begin
@testset "Julia entrypoint" begin
code = quote
using K8sDeputy
atexit(() -> @info "SHUTDOWN COMPLETE")
graceful_terminator() do
@info "GRACEFUL TERMINATION HANDLER"
exit(2)
return nothing
end
sleep(60)
end
sleep(60)

cmd = `$(Base.julia_cmd()) --color=no -e $code`
buffer = IOBuffer()
p = run(pipeline(cmd; stdout=buffer, stderr=buffer); wait=false)
@test timedwait(() -> process_running(p), Second(5)) === :ok

# Allow some time for Julia to startup and the graceful terminator to be registered.
sleep(3)

# When no PID is passed in the process ID is read from the Julia entrypoint file.
# Blocks untils the process terminates.
@test graceful_terminate() === nothing

@test process_exited(p)
@test p.exitcode == 2

output = String(take!(buffer))
expected = """
[ Info: GRACEFUL TERMINATION HANDLER
[ Info: SHUTDOWN COMPLETE
"""
@test output == expected
end

cmd = `$(Base.julia_cmd()) --color=no -e $code`
buffer = IOBuffer()
p = run(pipeline(cmd; stdout=buffer, stderr=buffer); wait=false)
@test timedwait(() -> process_running(p), Second(5)) === :ok
@testset "multiple Julia processes" begin
code = quote
using K8sDeputy
atexit(() -> @info "SHUTDOWN COMPLETE")
graceful_terminator(; set_entrypoint=false) do
@info "GRACEFUL TERMINATION HANDLER"
exit(2)
return nothing
end
sleep(60)
end

cmd = `$(Base.julia_cmd()) --color=no -e $code`
buffer1 = IOBuffer()
buffer2 = IOBuffer()
p1 = run(pipeline(cmd; stdout=buffer1, stderr=buffer1); wait=false)
p2 = run(pipeline(cmd; stdout=buffer2, stderr=buffer2); wait=false)
@test timedwait(() -> process_running(p1) && process_running(p2), Second(5)) === :ok

# Allow some time for Julia to startup and the graceful terminator to be registered.
sleep(3)
# Allow some time for Julia to startup and the graceful terminator to be registered.
sleep(3)

@test graceful_terminate(getpid(p)) === nothing # Blocks untils the HTTP server goes down
@test process_exited(p)
@test p.exitcode == 2
# Blocks untils the process terminates
@test graceful_terminate(getpid(p1)) === nothing
@test graceful_terminate(getpid(p2)) === nothing
@test process_exited(p1)
@test process_exited(p2)

output = String(take!(buffer))
expected = """
[ Info: GRACEFUL TERMINATION HANDLER
[ Info: SHUTDOWN COMPLETE
"""
@test output == expected
output1 = String(take!(buffer1))
output2 = String(take!(buffer2))
expected = """
[ Info: GRACEFUL TERMINATION HANDLER
[ Info: SHUTDOWN COMPLETE
"""
@test output1 == expected
@test output2 == expected
end
end
5 changes: 3 additions & 2 deletions test/health.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ end
atexit(() -> @info "SHUTDOWN COMPLETE")

deputy = Deputy(; shutdown_handler)
graceful_terminator() do
graceful_terminator(; set_entrypoint=false) do
@info "GRACEFUL TERMINATION HANDLER"
shutdown!(deputy)
return nothing
Expand All @@ -219,7 +219,8 @@ end
return r.status == 200
end === :ok

graceful_terminate(getpid(p)) # Blocks untils the HTTP server goes down
# Blocks untils the process terminates
graceful_terminate(getpid(p))
@test process_exited(p)
@test p.exitcode == 1

Expand Down

0 comments on commit d2fd348

Please sign in to comment.