-
Notifications
You must be signed in to change notification settings - Fork 0
/
interrupt.go
79 lines (65 loc) · 2.84 KB
/
interrupt.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
// Package interrupt
package interrupt
import (
"os"
kitModuleCfg "github.com/webnice/kit/v3/module/cfg"
kitModuleCfgReg "github.com/webnice/kit/v3/module/cfg/reg"
kitModuleInterrupt "github.com/webnice/kit/v3/module/interrupt"
kitTypes "github.com/webnice/kit/v3/types"
)
const (
newLine = "\n"
tplInterruptNew = "Получен сигнал прерывания %q."
tplInterruptEnabled = "Включён перехват системных прерываний."
tplInterruptDisabled = "Отключён перехват системных прерываний."
sigInterrupt = "interrupt"
)
// Структура объекта компоненты.
type impl struct {
i7t kitModuleInterrupt.Interface
}
// Регистрация компоненты в приложении.
func init() { kitModuleCfgReg.Registration(newComponent()) }
// Конструктор объекта компоненты.
func newComponent() kitTypes.Component { return new(impl) }
// Ссылка на функцию получения значения режима отладки, для удобного использования внутри компоненты.
func (ipt *impl) debug() bool { return kitModuleCfg.Get().Debug() }
// Ссылка на менеджер логирования, для удобного использования внутри компоненты.
func (ipt *impl) log() kitTypes.Logger { return kitModuleCfg.Get().Log() }
// Preferences Функция возвращает настройки компоненты.
func (ipt *impl) Preferences() kitTypes.ComponentPreferences {
const (
cConfiguration = `(?mi)application/component/configuration$`
cLogging = `(?mi)application/component/logging$`
)
return kitTypes.ComponentPreferences{
After: []string{cConfiguration, cLogging},
}
}
// Initiate Функция инициализации компонента и подготовки компонента к запуску.
func (ipt *impl) Initiate() (err error) { return }
// Do Выполнение компонента приложения.
func (ipt *impl) Do() (levelDone bool, levelExit bool, err error) {
ipt.i7t = kitModuleInterrupt.New(func(sig os.Signal) {
// Печать новой строки, если был нажат Ctrl-C
if sig.String() == sigInterrupt {
_, _ = os.Stdout.WriteString(newLine)
}
ipt.log().Alertf(tplInterruptNew, sig.String())
kitModuleCfg.Get().Gist().RunlevelExitAsync()
})
if ipt.i7t.Start(); ipt.debug() {
ipt.log().Infof(tplInterruptEnabled)
}
return
}
// Finalize Функция вызывается перед завершением компонента и приложения в целом
func (ipt *impl) Finalize() (err error) {
if ipt.i7t == nil {
return
}
if ipt.i7t.Stop(); ipt.debug() {
ipt.log().Infof(tplInterruptDisabled)
}
return
}