/
actions.go
78 lines (67 loc) · 2.56 KB
/
actions.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
package basic
import (
"context"
"io"
"github.com/Oracen/procflow/core/collections"
"github.com/Oracen/procflow/core/constants"
"github.com/Oracen/procflow/core/fileio"
"github.com/Oracen/procflow/core/store"
"github.com/Oracen/procflow/core/stringhandle"
"github.com/Oracen/procflow/core/tracker"
)
var (
singletonPtr Singleton
// Local pointer to global state, can be reset for testing
StateManager = &store.StateManager
)
func mockStateManagement() {
stateManager := store.CreateNewStateManager()
StateManager = &stateManager
}
func registerGlobal(singletonPointer *Singleton, collection *Collection, writer func(string) io.Writer) *exporter {
storage := store.CreateGlobalSingleton(singletonPointer, store.StateManager.GetLock())
storage.AddObject(collection)
export := exporter{storage, writer}
return &export
}
// Initialises string-based basic tracking object with required params
func RegisterTracker(ctx context.Context) (t Tracker) {
// Set up simple values
parentName := stringhandle.GetParentFlow(ctx)
collection := collections.CreateNewBasicCollector[string]()
if StateManager.UseGlobalState() {
// If shared state enabled, use the singleton to bring the object in
export := registerGlobal(&singletonPtr, &collection, fileio.CreateFileEncapsulation("flow.txt"))
StateManager.AddExporter("basic", export)
}
return tracker.RegisterBasicTracker(&collection, parentName)
}
// Action annotation for marking start of a process flow
func Start(tracker *Tracker, name, description string) (node Node) {
if StateManager.TrackState() {
taskName := stringhandle.PackNames(name, taskLabel.START)
node = tracker.StartFlow(stringhandle.PackNames(tracker.NameParentNode, taskName))
}
return
}
// Action annotation for marking intermediate tasks of a process
func Task(ctx context.Context, tracker *Tracker, inputs []Node, name, description string) (ctxNew context.Context, node Node) {
if StateManager.TrackState() {
taskName := stringhandle.PackNames(name, taskLabel.TASK)
node = tracker.AddNode(inputs, stringhandle.PackNames(tracker.NameParentNode, taskName))
}
ctxNew = context.WithValue(ctx, constants.ContextParentFlowKey, name)
return
}
// Action annotation for marking the endpoints of a process flow
func End(tracker *Tracker, inputs []Node, name, description string, isError bool) (node Node) {
if StateManager.TrackState() {
nodeType := taskLabel.END
if isError {
nodeType = taskLabel.ERROR
}
taskName := stringhandle.PackNames(name, nodeType)
node = tracker.EndFlow(inputs, stringhandle.PackNames(tracker.NameParentNode, taskName))
}
return
}