Files
threshold/internal/clientcache/cmd/cache/stop_windows.go
T
shadowdaoandClaude Sonnet 5.5 962d708fdd Rename Go module paths to repo.anhonesthost.net/CyberCoveLLC/threshold
Rename root, api and sdk module paths (imports, go.mod, go_package,
ldflags, build scripts, buf configs) and regenerate protobuf/gateway
code with the project's pinned toolchain. Wire identifiers unchanged.
api gets a local replace for sdk; root/api/sdk require v0.1.0.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 10:33:41 -07:00

81 lines
2.1 KiB
Go

// Copyright IBM Corp. 2020, 2026
// SPDX-License-Identifier: BUSL-1.1
//go:build windows
// +build windows
package cache
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"repo.anhonesthost.net/CyberCoveLLC/threshold/api"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/clientcache/internal/client"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/clientcache/internal/daemon"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/util"
)
// stop will send a term signal to the cache to shut down.
func (c *StopCommand) stop(ctx context.Context) error {
switch {
case util.IsNil(ctx):
return errors.New("Invalid parameter provided to stop: context is missing")
}
dotPath, err := DefaultDotDirectory(ctx)
if err != nil {
return fmt.Errorf("Error when getting default dot directory: %w", err)
}
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
}
c.UI.Warn(fmt.Sprintf("Failed stopping the cache through the handler: %q, now killing the process", errMsg))
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) {
return errors.New("Unable to stop the cache: pid file not found.")
}
return fmt.Errorf("Error when trying to stop the cache: %w", err)
}
if p == nil {
return errors.New("Cache process was not found.")
}
if err := p.Kill(); err != nil {
return fmt.Errorf("Error when killing the process: %w.", err)
}
return nil
}
func stopThroughHandler(ctx context.Context, dotPath string) (*api.Error, error) {
addr := daemon.SocketAddress(dotPath)
c, err := client.New(ctx, addr)
if err != nil {
return nil, err
}
resp, err := c.Post(ctx, "/v1/stop", nil)
if err != nil {
return nil, fmt.Errorf("Error when sending request to the cache: %w.", err)
}
apiErr, err := resp.Decode(nil)
return apiErr, err
}