-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrorprocesses.go
More file actions
185 lines (150 loc) · 4.56 KB
/
errorprocesses.go
File metadata and controls
185 lines (150 loc) · 4.56 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
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
// Actress Copyright (C) 2024 Bjørn Tore Svinningen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package actress
import (
"context"
"fmt"
"log/slog"
"os"
"unsafe"
)
// processes holds information about what process functions
// who belongs to what event, and also a map of the started
// processes.
type errorProcesses struct {
procMap map[EventName]*Process
}
// // Delete an Event and it's process from the processes map.
// func (p *processes) delete(et Name, proc *Process) {
// p.mu.Lock()
// defer p.mu.Unlock()
// p.procMap[et].cancel()
// delete(p.procMap, et)
// }
// Checks if the event is defined in the processes map, and returns true if it is.
func (p *errorProcesses) IsEventDefined(ev EventName) bool {
if _, ok := p.procMap[ev]; !ok {
return false
}
return true
}
// Prepare and return a new *processes structure.
func newErrorProcesses() *errorProcesses {
p := errorProcesses{
procMap: make(map[EventName]*Process),
}
return &p
}
// Router for error events.
const ERRouter EventName = "ERRouter"
// Process function for routing and handling events.
func erRouterFn(ctx context.Context, p *Process) func() {
fn := func() {
for {
select {
case e := <-p.ErrorEventCh:
// If there is a next event defined, we make a copy of all the fields of the current event,
// and put that as the previousEvent on the next event. We can use this information later
// if need to check something in the previous event.
if e.NextEvent != nil {
// Keep the information about the current event, so we are able to check for things
// like ackTimeout and what node to reply back to if ack should be given.
e.NextEvent.PreviousEvent = CopyEventFields(&e)
}
inCh := p.ErrorProcesses.procMap[e.Name].InCh
slog.Debug("erRouterFn", "Routing event", p.Event, "node", p.Config.NodeName, "name", e.Name, "Inch", inCh)
inCh <- e
case <-p.Ctx.Done():
slog.Debug("erRouterFn", "got ctx.Done, on", p.Config.NodeName)
return
}
}
}
return fn
}
// Instructions for error logging.
const InstructionError Instruction = "InstructionError"
const InstructionInfo Instruction = "InstructionInfo"
const InstructionDebug Instruction = "InstructionDebug"
const InstructionFatal Instruction = "InstructionFatal"
// Log errors.
const ERLog EventName = "ERLog"
// Will log errors to the console based on the Instruction field of the event.
//
// NB: The "none" is handled in the AddEvent function, to drop the event as
// early as possible, instead of sending it all the way to be dropped here.
func erLogFn(ctx context.Context, p *Process) func() {
fn := func() {
for {
p.SignalReady()
select {
case er := <-p.InCh:
switch er.Instruction {
case InstructionError:
slog.Error("erLogFn", "instructionError, msg", er.Err)
case InstructionInfo:
slog.Info("erLogFn", "instructionInfo, msg", er.Err)
case InstructionDebug:
slog.Debug("erLogFn", "instructionDebug, msg", er.Err)
case InstructionFatal:
slog.Error("erLogFn", "instructionFatal, msg", er.Err)
os.Exit(1)
default:
slog.Error("erLogFn", "default, msg", er.Err)
}
case <-p.Ctx.Done():
return
}
}
}
return fn
}
// Log and exit system.
const ERTest EventName = "ERTest"
func erTestFn(ctx context.Context, p *Process) func() {
fn := func() {
for {
p.SignalReady()
select {
case er := <-p.InCh:
go func() {
drop := fmt.Sprintf("erTestFn: error for fatal logging received: %v\n", er.Err)
_ = drop
}()
case <-p.Ctx.Done():
return
}
}
}
return fn
}
// Will drop the event if it is an error event.
const ERNone EventName = "ERNone"
// Process function for dropping error events. Primarily used for testing.
func erNoneFn(ctx context.Context, p *Process) func() {
use := func(p unsafe.Pointer) {}
fn := func() {
for {
p.SignalReady()
select {
case er := <-p.InCh:
use(unsafe.Pointer(&er.Err))
case <-p.Ctx.Done():
return
}
}
}
return fn
}