-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
service.go
86 lines (83 loc) · 1.65 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
package service
import "github.com/smartcontractkit/chainlink/core/services/health"
type (
// Service represents a long running service inside the
// Application.
//
// Typically a Service will leverage utils.StartStopOnce to implement these
// calls in a safe manner.
//
// Template
//
// Mockable Foo service with a run loop
// //go:generate mockery --name Foo --output ../../internal/mocks/ --case=underscore
// type (
// // Expose a public interface so we can mock the service.
// Foo interface {
// service.Service
//
// // ...
// }
//
// foo struct {
// // ...
//
// stop chan struct{}
// done chan struct{}
//
// utils.StartStopOnce
// }
// )
//
// var _ Foo = (*foo)(nil)
//
// func NewFoo() Foo {
// f := &foo{
// // ...
// }
//
// return f
// }
//
// func (f *foo) Start() error {
// return f.StartOnce("Foo", func() error {
// go f.run()
//
// return nil
// })
// }
//
// func (f *foo) Close() error {
// return f.StopOnce("Foo", func() error {
// // trigger goroutine cleanup
// close(f.stop)
// // wait for cleanup to complete
// <-f.done
// return nil
// })
// }
//
// func (f *foo) run() {
// // signal cleanup completion
// defer close(f.done)
//
// for {
// select {
// // ...
// case <-f.stop:
// // stop the routine
// return
// }
// }
//
// }
Service interface {
// Start the service.
Start() error
// Stop the Service.
// Invariants: Usually after this call the Service cannot be started
// again, you need to build a new Service to do so.
Close() error
health.Checkable
}
)