Repository navigation
Expand file tree
/
Copy pathWorkerSystem.go
More file actions
125 lines (94 loc) · 2.81 KB
/
Copy pathWorkerSystem.go
File metadata and controls
125 lines (94 loc) · 2.81 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
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
package limbov1
import (
"context"
"sync"
"time"
"unsafe"
"github.com/limboware/limbo/debug"
errnov1 "github.com/rejchev/errno"
)
type WorkerSystem struct {
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
}
func WorkerSystemFactory(buff *System) {
v := new(WorkerSystem)
*buff = System{
Instance: unsafe.Pointer(v),
Init: v.Load,
Activate: nil,
Update: nil,
Deactivate: nil,
Destroy: v.Unload,
}
}
func (x *WorkerSystem) Load() errnov1.Code {
debug.Get().Printf("<WorkerSystem::Init> start")
Events().Subscribe("workers.new", x.onWorker)
Events().Subscribe("workers.running", x.onWorkerRunning)
Events().Subscribe("workers.finished", x.onWorkerFinished)
Events().Subscribe("workers.remove", x.onWorkerRemove)
x.ctx, x.cancel = context.WithCancel(context.Background())
if err := Workers().Init(); errnov1.FAIL(err) {
return err
}
debug.Get().Printf("<WorkerSystem::Init> done")
return errnov1.OK
}
func (x *WorkerSystem) onWorker(_ string, data any) {
debug.Get().Printf("<WorkerSystem::onWorker> worker %v", data)
if w := data.(Worker); Workers().IsAlive(w) {
ctx, cancel := context.WithCancel(x.ctx)
x.wg.Go(func() {
worker := Worker_t{
ID: w,
Rate: 64,
}
worker.Init()
errno := worker.Run(ctx)
Events().PublishAsync("workers.finished", &OnWorkerFinished{
ID: worker.ID,
Tasks: worker.Tasks(),
At: time.Now().Unix(),
Code: errno,
})
worker.Destroy()
})
Workers().SetCancelFn(w, cancel)
Events().Publish("workers.created", w)
debug.Get().Printf("<WorkerSystem::onWorker> worker %d created", w)
}
}
func (x *WorkerSystem) onWorkerRunning(_ string, data any) {
debug.Get().Printf("<WorkerSystem::onWorkerRunning> called with: %v", data)
if w := data.(*OnWorkerRun); Workers().IsAlive(w.ID) {
Workers().SetRunnedAt(w.ID, w.At)
debug.Get().Printf("<WorkerSystem::onWorkerRunning> worker: %d; at: %d", w.ID, w.At)
}
}
func (x *WorkerSystem) onWorkerFinished(_ string, data any) {
debug.Get().Printf("<WorkerSystem::onWorkerFinished> called with: %v", data)
if w, ok := data.(*OnWorkerFinished); ok {
if Workers().IsAlive(w.ID) {
Workers().SetShuthdownedAt(w.ID, w.At)
}
debug.Get().Printf("<WorkerSystem::onWorkerFinished> worker: %d; at: %d", w.ID, w.At)
}
}
func (x *WorkerSystem) onWorkerRemove(_ string, data any) {
debug.Get().Printf("<WorkerSystem::onWorkerRemove> called with: %v", data)
if w := data.(Worker); Workers().IsAlive(w) {
if cancelFn := Workers().CancelFn(w); cancelFn != nil {
cancelFn()
debug.Get().Printf("<WorkerSystem::onWorkerRemove> worker: %d canceled", w)
}
}
}
func (x *WorkerSystem) Unload() {
debug.Get().Printf("<WorkerSystem::Unload> start")
Workers().Destroy()
x.cancel()
x.wg.Wait()
debug.Get().Printf("<WorkerSystem::Unload> done")
}