-
Notifications
You must be signed in to change notification settings - Fork 1
/
vabastegi.go
192 lines (152 loc) · 4.39 KB
/
vabastegi.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package vabastegi
import (
"context"
"os"
"os/signal"
"reflect"
"runtime"
"strings"
"sync"
"time"
"github.com/mrsoftware/errors"
)
// Provider is a dependency provider for application.
type Provider[T any] func(context.Context, *App[T]) error
// App is the dependency injection manger.
type App[T any] struct {
waitGroup sync.WaitGroup
errors *errors.MultiError
onShutdown []func(ctx context.Context) error
Hub T
options Options
backgroundTasksCount int
graceFullOnce sync.Once
}
// New instance of App Dependency management.
func New[T any](options ...Option) *App[T] {
app := App[T]{
errors: errors.NewMultiError(),
options: Options{EventHandlers: make(EventHandlers, 0)},
}
app.UpdateOptions(options...)
return &app
}
// UpdateOptions is used if you want to change any options for App.
func (a *App[ـ]) UpdateOptions(options ...Option) {
for _, option := range options {
option(&a.options)
}
if a.options.GracefulShutdown {
a.registerGracefulShutdown()
}
}
// Builds the dependency structure of your app.
func (a *App[T]) Builds(ctx context.Context, providers ...Provider[T]) (err error) {
startAt := time.Now()
a.options.EventHandlers.Publish(&OnBuildsExecuting{BuildAt: startAt})
defer func() {
a.options.EventHandlers.Publish(&OnApplicationShutdownExecuted{Runtime: time.Since(startAt), Err: err})
}()
for _, provider := range providers {
err := a.Build(ctx, provider)
if err == nil {
continue
}
a.Shutdown(ctx, "Provider Failure")
return err
}
return nil
}
// Build use the provider to set a dependency.
func (a *App[T]) Build(ctx context.Context, provider Provider[T]) (err error) {
startAt := time.Now()
a.options.EventHandlers.Publish(&OnBuildExecuting{
ProviderName: a.getProviderName(provider, 0),
CallerPath: a.getProviderName(provider, -1),
BuildAt: startAt,
})
defer func() {
a.options.EventHandlers.Publish(&OnBuildExecuted{
ProviderName: a.getProviderName(provider, 0),
CallerPath: a.getProviderName(provider, -1),
Runtime: time.Now().Sub(startAt),
Err: err,
})
}()
return provider(ctx, a)
}
// RunTask in background.
func (a *App[ـ]) RunTask(fn func()) {
go fn()
a.backgroundTasksCount++
a.waitGroup.Add(1)
}
// Wait for background task to done or any shutdown signal.
func (a *App[ـ]) Wait() error {
a.waitGroup.Wait()
return a.errors.Err()
}
// Shutdown ths application.
func (a *App[ـ]) Shutdown(ctx context.Context, reason string) {
startAt := time.Now()
a.options.EventHandlers.Publish(&OnApplicationShutdownExecuting{
Reason: reason,
ShutdownAt: startAt,
})
defer func() {
a.options.EventHandlers.Publish(&OnApplicationShutdownExecuted{
Reason: reason,
Runtime: time.Now().Sub(startAt),
Err: a.errors.Err(),
})
}()
for _, fn := range a.onShutdown {
a.errors.Add(a.shutdown(ctx, fn))
}
for i := 0; i < a.backgroundTasksCount; i++ {
a.waitGroup.Done()
}
}
func (a *App[ـ]) shutdown(ctx context.Context, fn func(context.Context) error) (err error) {
startAt := time.Now()
a.options.EventHandlers.Publish(&OnShutdownExecuting{
ProviderName: a.getProviderName(fn, 1),
CallerPath: a.getProviderName(fn, -1),
ShutdownAt: startAt,
})
defer func() {
a.options.EventHandlers.Publish(&OnShutdownExecuted{
ProviderName: a.getProviderName(fn, 1),
CallerPath: a.getProviderName(fn, -1),
Runtime: time.Now().Sub(startAt),
Err: err,
})
}()
return fn(ctx)
}
// OnShutdown register any method for Shutdown method.
func (a *App[ـ]) OnShutdown(fn func(ctx context.Context) error) {
a.onShutdown = append(a.onShutdown, fn)
}
func (a *App[ـ]) getProviderName(creator interface{}, index int) string {
reference := runtime.FuncForPC(reflect.ValueOf(creator).Pointer()).Name()
if index == -1 {
return reference
}
parts := strings.Split(reference, ".")
return parts[len(parts)-(1+index)]
}
// Log the message.
func (a *App[ـ]) Log(level logLevel, message string, args ...interface{}) {
a.options.EventHandlers.Publish(&OnLog{LogAt: time.Now(), Level: level, Message: message, Args: args})
}
func (a *App[ـ]) registerGracefulShutdown() {
a.graceFullOnce.Do(func() {
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, os.Interrupt)
go func() {
appSignal := <-interruptChan
a.Shutdown(context.Background(), appSignal.String())
}()
})
}