-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoteto_workflow.go
60 lines (50 loc) · 1.38 KB
/
poteto_workflow.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
package poteto
import (
"sort"
"github.com/poteto-go/poteto/constant"
)
type UnitWorkflow struct {
priority uint
workflow WorkflowFunc
}
// workflow is a function that is executed when the server starts | end
// - constant.START_UP_WORKFLOW: "startUp"
// - This is a workflow that is executed when the server starts
type PotetoWorkflows interface {
RegisterWorkflow(workflowType string, priority uint, workflow WorkflowFunc)
ApplyStartUpWorkflows() error
}
type potetoWorkflows struct {
startUpWorkflows []UnitWorkflow
}
func NewPotetoWorkflows() PotetoWorkflows {
return &potetoWorkflows{
startUpWorkflows: []UnitWorkflow{},
}
}
func (pw *potetoWorkflows) RegisterWorkflow(workflowType string, priority uint, workflow WorkflowFunc) {
switch workflowType {
case constant.START_UP_WORKFLOW:
pw.startUpWorkflows = append(pw.startUpWorkflows, UnitWorkflow{priority, workflow})
pw.startUpWorkflows = sortWorkflows(pw.startUpWorkflows)
default:
// pass
}
}
func (pw *potetoWorkflows) ApplyStartUpWorkflows() error {
if len(pw.startUpWorkflows) == 0 {
return nil
}
for _, workflow := range pw.startUpWorkflows {
if err := workflow.workflow(); err != nil {
return err
}
}
return nil
}
func sortWorkflows(workflows []UnitWorkflow) []UnitWorkflow {
sort.SliceStable(workflows, func(i, j int) bool {
return workflows[i].priority < workflows[j].priority
})
return workflows
}