This repository has been archived by the owner on May 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
114 lines (102 loc) · 2.65 KB
/
helpers.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
111
112
113
114
// Copyright 2019 github.com/ucirello and https://cirello.io. 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 writing, software distributed
// under the License is distributed on a "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 supervisor
import (
"context"
"sync"
)
func serve(s *Supervisor, ctx context.Context, processFailure processFailure) {
s.running.Lock()
defer s.running.Unlock()
startServices(s, ctx, processFailure)
var wg sync.WaitGroup
wg.Add(1)
go func() {
for {
select {
case <-s.added:
startServices(s, ctx, processFailure)
case <-ctx.Done():
wg.Done()
return
}
}
}()
<-ctx.Done()
wg.Wait()
s.runningServices.Wait()
s.mu.Lock()
s.cancelations = make(map[string]context.CancelFunc)
s.mu.Unlock()
}
func startServices(s *Supervisor, supervisorCtx context.Context, processFailure processFailure) {
s.mu.Lock()
defer s.mu.Unlock()
var wg sync.WaitGroup
for _, name := range s.svcorder {
svc := s.services[name]
if _, ok := s.cancelations[name]; ok {
continue
}
wg.Add(1)
terminateCtx, terminate := context.WithCancel(supervisorCtx)
s.cancelations[name] = terminate
s.terminations[name] = terminate
go func(name string, svc ServiceSpecification) {
s.runningServices.Add(1)
defer s.runningServices.Done()
wg.Done()
retry := true
for retry {
retry = svc.svctype == permanent
s.logf("%s starting", name)
func() {
defer func() {
if r := recover(); r != nil {
s.logf("%s panic: %v", name, r)
retry = svc.svctype == permanent || svc.svctype == transient
}
}()
ctx, cancel := context.WithCancel(terminateCtx)
s.mu.Lock()
s.cancelations[name] = cancel
s.mu.Unlock()
svc.svc.Serve(ctx)
}()
if retry {
processFailure()
}
select {
case <-terminateCtx.Done():
s.logf("%s restart aborted (terminated)", name)
return
case <-supervisorCtx.Done():
s.logf("%s restart aborted (supervisor halted)", name)
return
default:
}
switch svc.svctype {
case temporary:
s.logf("%s exited (temporary)", name)
return
case transient:
s.logf("%s exited (transient)", name)
default:
s.logf("%s exited (permanent)", name)
}
}
}(name, svc)
}
wg.Wait()
}