forked from GoogleCloudPlatform/compute-image-windows
/
service.go
110 lines (99 loc) · 2.58 KB
/
service.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"github.com/kardianos/service"
)
type program struct {
run func(context.Context)
ctx context.Context
cancel context.CancelFunc
done chan struct{}
timeout time.Duration
}
func (p *program) Start(s service.Service) error {
go func() {
p.run(p.ctx)
close(p.done)
}()
return nil
}
func (p *program) Stop(s service.Service) error {
p.cancel()
select {
case <-p.done:
return nil
case <-time.After(p.timeout):
return fmt.Errorf("failed to shutdown within timeout %s", p.timeout)
}
}
func usage(name string) {
fmt.Printf(
"Usage:\n"+
" %[1]s install: install the %[2]s service\n"+
" %[1]s remove: remove the %[2]s service\n"+
" %[1]s start: start the %[2]s service\n"+
" %[1]s stop: stop the %[2]s service\n", filepath.Base(os.Args[0]), name)
}
func register(ctx context.Context, name, displayName, desc string, run func(context.Context), action string) error {
svcConfig := &service.Config{
Name: name,
DisplayName: displayName,
Description: desc,
}
ctx, cancel := context.WithCancel(ctx)
done := make(chan struct{})
prg := &program{
run: run,
ctx: ctx,
cancel: cancel,
done: done,
timeout: 15 * time.Second,
}
svc, err := service.New(prg, svcConfig)
if err != nil {
return err
}
switch action {
case "run":
return svc.Run()
case "install":
if err := svc.Install(); err != nil {
return fmt.Errorf("failed to install service %s: %s", name, err)
}
case "remove":
if err := svc.Uninstall(); err != nil {
return fmt.Errorf("failed to remove service %s: %s", name, err)
}
case "start":
if err := svc.Start(); err != nil {
return fmt.Errorf("failed to start service %s: %s", name, err)
}
case "stop":
if err := svc.Stop(); err != nil {
return fmt.Errorf("failed to stop service %s: %s", name, err)
}
case "help":
usage(name)
default:
fmt.Printf("%q is not a valid argument.\n", action)
usage(name)
}
return nil
}