-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathservice.go
More file actions
76 lines (62 loc) · 1.4 KB
/
service.go
File metadata and controls
76 lines (62 loc) · 1.4 KB
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
package goutils
import (
"fmt"
"github.com/kardianos/service"
"sync"
)
type program struct {
// Use a wait group to track active goroutines
wg sync.WaitGroup
countWg int
}
var programInstance *program
var ServiceInstance service.Service
func (p *program) Start(s service.Service) error {
fmt.Println(s.String() + " Rodando!")
return nil
}
func (p *program) Stop(s service.Service) error {
fmt.Println(fmt.Sprintf("%s Parando com %d threads ainda rodando...", s.String(), p.countWg))
p.DoneWaitGroup()
p.wg.Wait()
fmt.Println(s.String() + " Parado!")
return nil
}
func (p *program) AddWaitGroup() {
p.wg.Add(1)
p.countWg++
}
func (p *program) DoneWaitGroup() {
fmt.Println("Finishing Thread")
p.wg.Done()
p.countWg--
}
func InitService() {
programInstance = &program{
wg: sync.WaitGroup{},
}
programInstance.AddWaitGroup()
}
func ExecuteService(serviceConfig *service.Config) {
if programInstance == nil {
InitService()
}
var err error
ServiceInstance, err = service.New(programInstance, serviceConfig)
if err != nil {
fmt.Println("Não pode criar o serviço: " + err.Error())
}
err = ServiceInstance.Run()
if err != nil {
fmt.Println("Não é possível iniciar o serviço: " + err.Error())
}
}
func AddThreadForShutdown() {
if programInstance == nil {
InitService()
}
programInstance.AddWaitGroup()
}
func DoneThreadForShutdown() {
programInstance.DoneWaitGroup()
}