-
Notifications
You must be signed in to change notification settings - Fork 0
/
finalize.go
56 lines (48 loc) · 1.73 KB
/
finalize.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
// Package application
package application
import (
"context"
"time"
kitModuleTrace "github.com/webnice/kit/v3/module/trace"
kitTypes "github.com/webnice/kit/v3/types"
)
// Функция вызова функции Finalize() у компоненты
func (app *impl) finalizeFn(component *kitTypes.ComponentInfo) (err kitTypes.ErrorWithCode) {
var (
ctx context.Context
cfn context.CancelFunc
)
// Создание контекста
ctx, cfn = context.WithCancel(context.Background())
defer cfn()
// Запуск вспомогательной горутины, которая по таймауту выведет в лог сообщение о долгой работе функции Finalize()
go func(cx context.Context, name string, tout time.Duration) {
select {
case <-cx.Done():
return
case <-time.After(tout):
app.cfg.Log().Warning(app.cfg.Errors().ComponentFinalizeWarning(0, name, tout).Error())
}
}(ctx, component.ComponentName, app.cfg.Gist().ComponentFinalizeWarningTimeout())
err = app.finalizeSafeCall(component.ComponentName, component.Component)
return
}
// Запуск функции Finalize() в компоненте с защитой от паники
func (app *impl) finalizeSafeCall(componentName string, cpt kitTypes.Component) (err kitTypes.ErrorWithCode) {
var e error
// Функция защиты от паники
defer func() {
if e := recover(); e != nil {
err = app.cfg.Errors().ComponentFinalizePanicException(0, componentName, e, kitModuleTrace.StackShort())
}
}()
if e = cpt.Finalize(); e != nil {
switch eto := e.(type) {
case kitTypes.ErrorWithCode:
err = eto
default:
err = app.cfg.Errors().ComponentFinalizeExecution(0, componentName, eto)
}
}
return
}