-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
59 lines (50 loc) · 1.11 KB
/
main.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
package main
import (
"fmt"
"github.com/kardianos/service"
"sync"
"time"
)
const serviceName = "Medium service"
const serviceDescription = "Simple service, just for fun"
type program struct{}
func (p program) Start(s service.Service) error {
fmt.Println(s.String() + " started")
go p.run()
return nil
}
func (p program) Stop(s service.Service) error {
fmt.Println(s.String() + " stopped")
return nil
}
func (p program) run() {
for serviceIsRunning {
writingSync.Lock()
programIsRunning = true
writingSync.Unlock()
fmt.Println("Service is running")
time.Sleep(2 * time.Second)
writingSync.Lock()
programIsRunning = false
writingSync.Unlock()
for {
fmt.Println("Service is running")
time.Sleep(1 * time.Second)
}
}
func main() {
serviceConfig := &service.Config{
Name: serviceName,
DisplayName: serviceName,
Description: serviceDescription,
}
prg := &program{}
s, err := service.New(prg, serviceConfig)
if err != nil {
fmt.Println("Cannot create the service: " + err.Error())
}
err = s.Run()
if err != nil {
fmt.Println("Cannot start the service: " + err.Error())
}
}