-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #773 from ripienaar/772
(#772) support running as a windows service
- Loading branch information
Showing
6 changed files
with
101 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ provisioning_target_provider.go | |
plugin_*.go | ||
.idea/ | ||
*.msi | ||
*.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// +build !windows | ||
|
||
package cmd | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
func (r *serverRunCommand) Run(wg *sync.WaitGroup) (err error) { | ||
defer wg.Done() | ||
|
||
instance, err := r.prepareInstance() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
wg.Add(1) | ||
return instance.Run(ctx, wg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package cmd | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/choria-io/go-choria/server" | ||
log "github.com/sirupsen/logrus" | ||
"golang.org/x/sys/windows/svc" | ||
) | ||
|
||
type winServiceWrapper struct { | ||
instance *server.Instance | ||
cmd *serverRunCommand | ||
} | ||
|
||
func (w *winServiceWrapper) Execute(args []string, changes <-chan svc.ChangeRequest, status chan<- svc.Status) (bool, uint32) { | ||
status <- svc.Status{State: svc.StartPending} | ||
|
||
var err error | ||
|
||
w.instance, err = w.cmd.prepareInstance() | ||
if err != nil { | ||
log.Errorf("failed to create instance of the server: %q", err) | ||
return false, 1 | ||
} | ||
|
||
wg.Add(1) | ||
go w.instance.Run(ctx, wg) | ||
|
||
status <- svc.Status{ | ||
State: svc.Running, | ||
Accepts: svc.AcceptStop | svc.AcceptShutdown, | ||
} | ||
|
||
loop: | ||
for change := range changes { | ||
switch change.Cmd { | ||
case svc.Interrogate: | ||
status <- change.CurrentStatus | ||
|
||
case svc.Stop, svc.Shutdown: | ||
cancel() | ||
|
||
break loop | ||
default: | ||
log.Warnf("Unexpected service control sequence received: %q", change.Cmd) | ||
} | ||
} | ||
|
||
status <- svc.Status{State: svc.StopPending} | ||
|
||
return false, 0 | ||
} | ||
|
||
func (r *serverRunCommand) Run(wg *sync.WaitGroup) (err error) { | ||
defer wg.Done() | ||
|
||
interactive, err := svc.IsAnInteractiveSession() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if interactive { | ||
instance, err := r.prepareInstance() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
wg.Add(1) | ||
return instance.Run(ctx, wg) | ||
} | ||
|
||
return svc.Run("choria-server", &winServiceWrapper{cmd: r}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters