service: installation and running of AGH as a service with CLI args

This commit is contained in:
David Sheets 2020-09-07 10:26:40 +01:00
parent d3428ca46c
commit 90ef204d04
2 changed files with 10 additions and 5 deletions

View File

@ -126,7 +126,7 @@ func Main(version string, channel string, armVer string) {
}() }()
if args.serviceControlAction != "" { if args.serviceControlAction != "" {
handleServiceControlAction(args.serviceControlAction) handleServiceControlAction(args)
return return
} }

View File

@ -24,12 +24,14 @@ const (
// Represents the program that will be launched by a service or daemon // Represents the program that will be launched by a service or daemon
type program struct { type program struct {
opts options
} }
// Start should quickly start the program // Start should quickly start the program
func (p *program) Start(s service.Service) error { func (p *program) Start(s service.Service) error {
// Start should not block. Do the actual work async. // Start should not block. Do the actual work async.
args := options{runningAsService: true} args := p.opts
args.runningAsService = true
go run(args) go run(args)
return nil return nil
} }
@ -125,7 +127,8 @@ func sendSigReload() {
// run - this is a special command that is not supposed to be used directly // run - this is a special command that is not supposed to be used directly
// it is specified when we register a service, and it indicates to the app // it is specified when we register a service, and it indicates to the app
// that it is being run as a service/daemon. // that it is being run as a service/daemon.
func handleServiceControlAction(action string) { func handleServiceControlAction(opts options) {
action := opts.serviceControlAction
log.Printf("Service control action: %s", action) log.Printf("Service control action: %s", action)
if action == "reload" { if action == "reload" {
@ -137,15 +140,17 @@ func handleServiceControlAction(action string) {
if err != nil { if err != nil {
log.Fatal("Unable to find the path to the current directory") log.Fatal("Unable to find the path to the current directory")
} }
runOpts := opts
runOpts.serviceControlAction = "run"
svcConfig := &service.Config{ svcConfig := &service.Config{
Name: serviceName, Name: serviceName,
DisplayName: serviceDisplayName, DisplayName: serviceDisplayName,
Description: serviceDescription, Description: serviceDescription,
WorkingDirectory: pwd, WorkingDirectory: pwd,
Arguments: []string{"-s", "run"}, Arguments: serialize(runOpts),
} }
configureService(svcConfig) configureService(svcConfig)
prg := &program{} prg := &program{runOpts}
s, err := service.New(prg, svcConfig) s, err := service.New(prg, svcConfig)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)