Skip to content

Commit

Permalink
Wrap syscall.Kill to a wrapper to avoid error in Windows
Browse files Browse the repository at this point in the history
syscall.Kill is a system library call not available in Windows making it difficult to compile the code as is in Windows and for proper IDE support. So wrapped syscall.Kill to a system specific build constrained implementation to avoid the compile error.
  • Loading branch information
imyousuf committed Sep 22, 2024
1 parent 2b8bf58 commit a09d3b1
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ADD controllers ./controllers
ADD migration ./migration
ADD storage ./storage
ADD dispatcher ./dispatcher
ADD utils ./utils

RUN make build
RUN make test
Expand Down
3 changes: 2 additions & 1 deletion controllers/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/newscred/webhook-broker/storage/data"
storagemocks "github.com/newscred/webhook-broker/storage/mocks"
"github.com/newscred/webhook-broker/utils"
"github.com/stretchr/testify/mock"
)

Expand Down Expand Up @@ -85,7 +86,7 @@ func TestNotifyOnInterrupt(t *testing.T) {
<-stop
wg.Done()
}()
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
utils.NewProcessKiller().Kill(syscall.Getpid(), syscall.SIGTERM)
if waitTimeout(&wg, 100*time.Millisecond) {
t.Fail()
}
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/newscred/webhook-broker/dispatcher"
"github.com/newscred/webhook-broker/storage"
"github.com/newscred/webhook-broker/storage/data"
"github.com/newscred/webhook-broker/utils"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)

Expand Down Expand Up @@ -229,7 +230,7 @@ func startWebhookBroker(inConfig *config.CLIConfig) {
log.Print("Restarting")
setHasConfigChange(true)
}
syscall.Kill(pid, syscall.SIGINT)
utils.NewProcessKiller().Kill(pid, syscall.SIGINT)
})
for hasConfigChange {
setHasConfigChange(false)
Expand Down
12 changes: 12 additions & 0 deletions utils/process_killer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package utils

import "syscall"

// ProcessKiller interface for killing a process
type ProcessKiller interface {
Kill(PID int, signal syscall.Signal) error
}

func NewProcessKiller() ProcessKiller {
return &DefaultProcessKiller{}
}
25 changes: 25 additions & 0 deletions utils/process_killer_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build linux || darwin

package utils

import (
"os"
"syscall"
)

// DefaultProcessKiller is a default implementation of ProcessKiller interface
type DefaultProcessKiller struct{}

func (pk *DefaultProcessKiller) Kill(pid int, signal syscall.Signal) error {
_, err := os.FindProcess(pid)
if err != nil {
return err
}

err = syscall.Kill(pid, syscall.SIGINT)
if err != nil {
return err
}

return nil
}
15 changes: 15 additions & 0 deletions utils/process_killer_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build windows

package utils

import (
"syscall"
)

// DefaultProcessKiller is a default implementation of ProcessKiller interface
type DefaultProcessKiller struct {
}

func (pk *DefaultProcessKiller) Kill(pid int, signal syscall.Signal) error {
return nil
}

0 comments on commit a09d3b1

Please sign in to comment.