diff --git a/README.md b/README.md index 1552f28..4c30173 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/docs/src/health_checks.md b/docs/src/health_checks.md index 31d775b..a2228cd 100644 --- a/docs/src/health_checks.md +++ b/docs/src/health_checks.md @@ -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. @@ -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. diff --git a/docs/src/index.md b/docs/src/index.md index 1552f28..4c30173 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -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) diff --git a/test/graceful_termination.jl b/test/graceful_termination.jl index fdb46e6..075ae91 100644 --- a/test/graceful_termination.jl +++ b/test/graceful_termination.jl @@ -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 diff --git a/test/health.jl b/test/health.jl index 36227a1..8d45143 100644 --- a/test/health.jl +++ b/test/health.jl @@ -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 @@ -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