Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ builds:
goos:
- linux
- darwin
- windows
ignore:
- goos: linux
goarch: '386'
- goos: windows
goarch: '386'

archives:
- name_template: >-
Expand All @@ -27,6 +30,9 @@ archives:
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
format_overrides:
- goos: windows
formats: [ zip ]
files:
- LICENSE
- completions/*
Expand Down
20 changes: 1 addition & 19 deletions cmd/project/project_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,7 @@ var projectWorkerCmd = &cobra.Command{
cmd.Env = append(os.Environ(), fmt.Sprintf("MESSENGER_CONSUMER_NAME=%s-%d", baseName, index))
cmd.WaitDelay = time.Second
cmd.Cancel = func() error {
if gracefulStopLimit > 0 {
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
return err
}

now := time.Now()

for time.Since(now) < time.Second*time.Duration(gracefulStopLimit) {
if isProcessStopped(cmd.Process) {
return os.ErrProcessDone
}
time.Sleep(time.Millisecond * 250)
}
}
return cmd.Process.Kill()
return gracefulStop(cmd, gracefulStopLimit)
}

if err := cmd.Run(); err != nil {
Expand Down Expand Up @@ -157,7 +143,3 @@ func cancelOnTermination(ctx context.Context, cancel context.CancelFunc) {
cancel()
}()
}

func isProcessStopped(p *os.Process) bool {
return errors.Is(p.Signal(syscall.Signal(0)), os.ErrProcessDone)
}
34 changes: 34 additions & 0 deletions cmd/project/project_worker_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build !windows

package project

import (
"errors"
"os"
"os/exec"
"syscall"
"time"
)

// gracefulStop sends SIGTERM and waits up to gracefulStopLimit seconds for the process to exit before killing it. A limit of 0 kills immediately.
func gracefulStop(cmd *exec.Cmd, gracefulStopLimit uint) error {
if gracefulStopLimit > 0 {
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
return err
}

deadline := time.Now().Add(time.Second * time.Duration(gracefulStopLimit))
for time.Now().Before(deadline) {
if isProcessStopped(cmd.Process) {
return os.ErrProcessDone
}
time.Sleep(time.Millisecond * 250)
}
}

return cmd.Process.Kill()
}

func isProcessStopped(p *os.Process) bool {
return errors.Is(p.Signal(syscall.Signal(0)), os.ErrProcessDone)
}
10 changes: 10 additions & 0 deletions cmd/project/project_worker_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build windows

package project

import "os/exec"

// gracefulStop kills the process immediately. Windows console child processes cannot receive SIGTERM, so graceful termination is not possible;
func gracefulStop(cmd *exec.Cmd, _ uint) error {
return cmd.Process.Kill()
}