-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_windows.go
67 lines (59 loc) · 1.55 KB
/
main_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"context"
"fmt"
"log/slog"
"os"
"runtime/debug"
"time"
"github.com/myopenfactory/edi-connector/config"
"github.com/myopenfactory/edi-connector/connector"
"golang.org/x/sys/windows/svc"
)
func init() {
run := windowsRun
serviceRun = &run
}
func windowsRun(ctx context.Context, logger *slog.Logger, cfg config.Config, serviceName string) error {
cl, err := connector.New(logger, cfg)
if err != nil {
return fmt.Errorf("failed to create edi-connector: %w", err)
}
if err := svc.Run(serviceName, &service{connector: cl}); err != nil {
return fmt.Errorf("service %q failed to run: %w", serviceName, err)
}
return nil
}
type service struct {
logger *slog.Logger
connector *connector.Connector
}
func (s *service) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
defer func() {
if r := recover(); r != nil {
s.logger.Error("client panic", "value", r, "stack", debug.Stack())
}
}()
if err := s.connector.Run(ctx); err != nil {
s.logger.Error("error while running client", "error", err)
os.Exit(1)
}
}()
deadline := 5 * time.Second
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted, WaitHint: uint32(deadline.Seconds()) * 1000}
for {
select {
case c := <-r:
switch c.Cmd {
case svc.Stop, svc.Shutdown:
changes <- svc.Status{State: svc.StopPending}
cancel()
return false, 0
}
}
}
}