mirror of
https://github.com/apricote/releaser-pleaser.git
synced 2026-01-13 13:21:00 +00:00
feat: graceful shutdown when CI job is cancelled (#196)
By listening on SIGINT and SIGTERM signals we can stop executing as soon as reasonably possible. This helps to avoid uncessary work and stop the job earlier. Right now we have no manual checks for cancelled contexts, and rely on the http client to check for it while making requests.
This commit is contained in:
parent
eae0045359
commit
08d35f2f57
1 changed files with 35 additions and 5 deletions
|
|
@ -1,9 +1,12 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/lmittmann/tint"
|
"github.com/lmittmann/tint"
|
||||||
|
|
@ -13,10 +16,12 @@ import (
|
||||||
var logger *slog.Logger
|
var logger *slog.Logger
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "rp",
|
Use: "rp",
|
||||||
Short: "",
|
Short: "",
|
||||||
Long: ``,
|
Long: ``,
|
||||||
Version: version(),
|
Version: version(),
|
||||||
|
SilenceUsage: true, // Makes it harder to find the actual error
|
||||||
|
SilenceErrors: true, // We log manually with slog
|
||||||
}
|
}
|
||||||
|
|
||||||
func version() string {
|
func version() string {
|
||||||
|
|
@ -41,8 +46,33 @@ func version() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Execute() {
|
func Execute() {
|
||||||
err := rootCmd.Execute()
|
// Behaviour when cancelling jobs:
|
||||||
|
//
|
||||||
|
// GitHub Actions: https://docs.github.com/en/actions/managing-workflow-runs-and-deployments/managing-workflow-runs/canceling-a-workflow#steps-github-takes-to-cancel-a-workflow-run
|
||||||
|
// 1. SIGINT
|
||||||
|
// 2. Wait 7500ms
|
||||||
|
// 3. SIGTERM
|
||||||
|
// 4. Wait 2500ms
|
||||||
|
// 5. SIGKILL
|
||||||
|
//
|
||||||
|
// GitLab CI/CD: https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/4446
|
||||||
|
// 1. SIGTERM
|
||||||
|
// 2. Wait ???
|
||||||
|
// 3. SIGKILL
|
||||||
|
//
|
||||||
|
// We therefore need to listen on SIGINT and SIGTERM
|
||||||
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
|
go func() {
|
||||||
|
// Make sure to stop listening on signals after receiving the first signal to hand control of the signal back
|
||||||
|
// to the runtime. The Go runtime implements a "force shutdown" if the signal is received again.
|
||||||
|
<-ctx.Done()
|
||||||
|
logger.InfoContext(ctx, "Received shutdown signal, stopping...")
|
||||||
|
stop()
|
||||||
|
}()
|
||||||
|
|
||||||
|
err := rootCmd.ExecuteContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.ErrorContext(ctx, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue