-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathroot.go
More file actions
67 lines (58 loc) · 1.27 KB
/
Copy pathroot.go
File metadata and controls
67 lines (58 loc) · 1.27 KB
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
package task
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
)
// shutdown 关闭接口
type shutdown interface {
Shutdown()
}
// OSSignal 操作系统信号处理任务
type OSSignal struct {
ChannelTask
root shutdown
}
func (o *OSSignal) Start() error {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
o.SignalChan = signalChan
o.OnStop(func() {
signal.Stop(signalChan)
close(signalChan)
})
return nil
}
func (o *OSSignal) Tick(any) {
go o.root.Shutdown()
}
// ManagerItem 管理器项目接口
type ManagerItem[K comparable] interface {
ITask
GetKey() K
}
// RootManager 根任务管理器
type RootManager[K comparable, T ManagerItem[K]] struct {
WorkCollection[K, T]
}
// Init 初始化根任务管理器
func (m *RootManager[K, T]) Init() {
m.parentCtx = context.Background()
m.reset()
m.handler = m
m.Logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
m.StartTime = time.Now()
m.AddTask(&OSSignal{root: m}).WaitStarted()
m.state = TASK_STATE_STARTED
}
// Shutdown 关闭根任务管理器
func (m *RootManager[K, T]) Shutdown() {
fmt.Println("RootManager Shutdown...")
m.Stop(ErrExit)
m.dispose()
fmt.Println("RootManager Shutdown done")
}