Use a named event for dapr stop on Windows (#631) (#638)

This commit is contained in:
Charlie Stanley 2021-03-22 15:46:41 -07:00 committed by GitHub
parent 8e183b25db
commit 4ad42fffbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 95 additions and 10 deletions

View File

@ -9,10 +9,8 @@ import (
"bufio"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/dapr/cli/pkg/metadata"
@ -90,7 +88,7 @@ var RunCmd = &cobra.Command{
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
setupShutdownNotify(sigCh)
daprRunning := make(chan bool, 1)
appRunning := make(chan bool, 1)

18
cmd/shutdown.go Normal file
View File

@ -0,0 +1,18 @@
// +build !windows
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation and Dapr Contributors.
// Licensed under the MIT License.
// ------------------------------------------------------------
package cmd
import (
"os"
"os/signal"
"syscall"
)
func setupShutdownNotify(sigCh chan os.Signal) {
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
}

34
cmd/shutdown_windows.go Normal file
View File

@ -0,0 +1,34 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation and Dapr Contributors.
// Licensed under the MIT License.
// ------------------------------------------------------------
package cmd
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/dapr/cli/pkg/print"
"golang.org/x/sys/windows"
)
func setupShutdownNotify(sigCh chan os.Signal){
//This will catch Ctrl-C
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
// Unlike Linux/Mac, you can't just send a SIGTERM from another process
// In order for 'dapr stop' to be able to signal gracefully we must use a named event in Windows
go func() {
eventName, _ := syscall.UTF16FromString(fmt.Sprintf("dapr_cli_%v", os.Getpid()))
eventHandle, _ := windows.CreateEvent(nil, 0, 0, &eventName[0])
_, err := windows.WaitForSingleObject(eventHandle, windows.INFINITE)
if err != nil {
print.WarningStatusEvent(os.Stdout, "Unable to wait for shutdown event. 'dapr stop' will not work. Error: %s", err.Error())
return
}
sigCh <- os.Interrupt
}()
}

1
go.mod
View File

@ -23,6 +23,7 @@ require (
github.com/spf13/cobra v1.1.1
github.com/spf13/viper v1.7.0
github.com/stretchr/testify v1.6.1
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d // indirect
gopkg.in/yaml.v2 v2.3.0
helm.sh/helm/v3 v3.4.0
k8s.io/api v0.20.0

View File

@ -1,3 +1,5 @@
// +build !windows
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation and Dapr Contributors.
// Licensed under the MIT License.
@ -7,7 +9,6 @@ package standalone
import (
"fmt"
"runtime"
"github.com/dapr/cli/utils"
)
@ -23,12 +24,7 @@ func Stop(appID string) error {
if a.AppID == appID {
pid := fmt.Sprintf("%v", a.PID)
var err error
if runtime.GOOS == "windows" {
_, err = utils.RunCmdAndWait("taskkill", "/F", "/T", "/PID", pid)
} else {
_, err = utils.RunCmdAndWait("kill", pid)
}
_, err := utils.RunCmdAndWait("kill", pid)
return err
}

View File

@ -0,0 +1,38 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation and Dapr Contributors.
// Licensed under the MIT License.
// ------------------------------------------------------------
package standalone
import (
"fmt"
"syscall"
"golang.org/x/sys/windows"
)
// Stop terminates the application process.
func Stop(appID string) error {
apps, err := List()
if err != nil {
return err
}
for _, a := range apps {
if a.AppID == appID {
eventName, _ := syscall.UTF16FromString(fmt.Sprintf("dapr_cli_%v", a.PID))
eventHandle, err := windows.OpenEvent(windows.EVENT_MODIFY_STATE, false, &eventName[0])
if err != nil {
return err
}
err = windows.SetEvent(eventHandle)
return err
}
}
return fmt.Errorf("couldn't find app id %s", appID)
}