Files
threshold/internal/clientcache/cmd/cache/stop_windows.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
2.0 KiB
Go
Raw Normal View History

2023-06-11 20:40:47 -04:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
//go:build windows
// +build windows
2024-05-22 11:11:48 -07:00
package cache
2023-06-11 20:40:47 -04:00
import (
"context"
"errors"
2023-06-11 20:40:47 -04:00
"fmt"
"os"
"path/filepath"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/internal/clientcache/internal/client"
2023-06-11 20:40:47 -04:00
"github.com/hashicorp/boundary/internal/clientcache/internal/daemon"
"github.com/hashicorp/boundary/internal/util"
)
2024-05-22 11:11:48 -07:00
// stop will send a term signal to the cache to shut down.
2024-01-04 12:51:21 -08:00
func (c *StopCommand) stop(ctx context.Context) error {
2023-06-11 20:40:47 -04:00
switch {
case util.IsNil(ctx):
return errors.New("Invalid parameter provided to stop: context is missing")
2023-06-11 20:40:47 -04:00
}
dotPath, err := DefaultDotDirectory(ctx)
if err != nil {
return fmt.Errorf("Error when getting default dot directory: %w", err)
2023-06-11 20:40:47 -04:00
}
apiErr, err := stopThroughHandler(ctx, dotPath)
switch {
case err != nil, apiErr != nil:
var errMsg string
if err != nil {
errMsg = err.Error()
} else if apiErr != nil {
errMsg = apiErr.Message
}
2024-05-22 11:11:48 -07:00
c.UI.Warn(fmt.Sprintf("Failed stopping the cache through the handler: %q, now killing the process", errMsg))
2023-06-11 20:40:47 -04:00
default:
// there wasn't an error stopping it through the handler. No need to
// force kill the process
return nil
}
pidPath := filepath.Join(dotPath, pidFileName)
p, err := pidFileInUse(ctx, pidPath)
if err != nil {
if os.IsNotExist(err) {
2024-05-22 11:11:48 -07:00
return errors.New("Unable to stop the cache: pid file not found.")
2023-06-11 20:40:47 -04:00
}
2024-05-22 11:11:48 -07:00
return fmt.Errorf("Error when trying to stop the cache: %w", err)
2023-06-11 20:40:47 -04:00
}
if p == nil {
2024-05-22 11:11:48 -07:00
return errors.New("Cache process was not found.")
2023-06-11 20:40:47 -04:00
}
if err := p.Kill(); err != nil {
return fmt.Errorf("Error when killing the process: %w.", err)
2023-06-11 20:40:47 -04:00
}
return nil
}
func stopThroughHandler(ctx context.Context, dotPath string) (*api.Error, error) {
2024-01-02 11:15:45 -08:00
addr := daemon.SocketAddress(dotPath)
c, err := client.New(ctx, addr)
2023-06-11 20:40:47 -04:00
if err != nil {
return nil, err
}
resp, err := c.Post(ctx, "/v1/stop", nil)
2023-06-11 20:40:47 -04:00
if err != nil {
2024-05-22 11:11:48 -07:00
return nil, fmt.Errorf("Error when sending request to the cache: %w.", err)
2023-06-11 20:40:47 -04:00
}
apiErr, err := resp.Decode(nil)
return apiErr, err
2023-06-11 20:40:47 -04:00
}