Debian Codegraph @

server/service_windows.go

raw
1 // Copyright 2012-2025 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package server
15
16 import (
17 "os"
18 "time"
19
20 "golang.org/x/sys/windows/svc"
21 )
22
23 const (
24 reopenLogCode = 128
25 reopenLogCmd = svc.Cmd(reopenLogCode)
26 ldmCode = 129
27 ldmCmd = svc.Cmd(ldmCode)
28 acceptReopenLog = svc.Accepted(reopenLogCode)
29 )
30
31 var serviceName = "nats-server"
32
33 // SetServiceName allows setting a different service name
34 func SetServiceName(name string) {
35 serviceName = name
36 }
37
38 // winServiceWrapper implements the svc.Handler interface for implementing
39 // nats-server as a Windows service.
40 type winServiceWrapper struct {
41 server *Server
42 }
43
44 var dockerized = false
45
46 func init() {
47 if v, exists := os.LookupEnv("NATS_DOCKERIZED"); exists && v == "1" {
48 dockerized = true
49 }
50 }
51
52 // Execute will be called by the package code at the start of
53 // the service, and the service will exit once Execute completes.
54 // Inside Execute you must read service change requests from r and
55 // act accordingly. You must keep service control manager up to date
56 // about state of your service by writing into s as required.
57 // args contains service name followed by argument strings passed
58 // to the service.
59 // You can provide service exit code in exitCode return parameter,
60 // with 0 being "no error". You can also indicate if exit code,
61 // if any, is service specific or not by using svcSpecificEC
62 // parameter.
63 func (w *winServiceWrapper) Execute(args []string, changes <-chan svc.ChangeRequest,
64 status chan<- svc.Status) (bool, uint32) {
65
66 status <- svc.Status{State: svc.StartPending}
67 go w.server.Start()
68
69 var startupDelay = 10 * time.Second
70 if v, exists := os.LookupEnv("NATS_STARTUP_DELAY"); exists {
71 if delay, err := time.ParseDuration(v); err == nil {
72 startupDelay = delay
73 } else {
74 w.server.Errorf("Failed to parse \"%v\" as a duration for startup: %s", v, err)
75 }
76 }
77 // Wait for accept loop(s) to be started
78 if !w.server.ReadyForConnections(startupDelay) {
79 // Failed to start.
80 return false, 1
81 }
82
83 status <- svc.Status{
84 State: svc.Running,
85 Accepts: svc.AcceptStop | svc.AcceptShutdown | svc.AcceptParamChange | acceptReopenLog,
86 }
87
88 loop:
89 for {
90 select {
91 case change, ok := <-changes:
92 if !ok {
93 break loop
94 }
95 switch change.Cmd {
96 case svc.Interrogate:
97 status <- change.CurrentStatus
98 case svc.Stop, svc.Shutdown:
99 w.server.Shutdown()
100 break loop
101 case reopenLogCmd:
102 // File log re-open for rotating file logs.
103 w.server.ReOpenLogFile()
104 case ldmCmd:
105 go w.server.lameDuckMode()
106 case svc.ParamChange:
107 if err := w.server.Reload(); err != nil {
108 w.server.Errorf("Failed to reload server configuration: %s", err)
109 }
110 default:
111 w.server.Debugf("Unexpected control request: %v", change.Cmd)
112 }
113 case <-w.server.quitCh:
114 break loop
115 }
116 }
117
118 status <- svc.Status{State: svc.StopPending}
119 return false, 0
120 }
121
122 // Run starts the NATS server as a Windows service.
123 func Run(server *Server) error {
124 if dockerized {
125 server.Start()
126 return nil
127 }
128 isWindowsService, err := svc.IsWindowsService()
129 if err != nil {
130 return err
131 }
132 if !isWindowsService {
133 server.Start()
134 return nil
135 }
136 return svc.Run(serviceName, &winServiceWrapper{server})
137 }
138
139 // isWindowsService indicates if NATS is running as a Windows service.
140 func isWindowsService() bool {
141 if dockerized {
142 return false
143 }
144 isWindowsService, _ := svc.IsWindowsService()
145 return isWindowsService
146 }