-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution.go
More file actions
63 lines (51 loc) · 1.35 KB
/
execution.go
File metadata and controls
63 lines (51 loc) · 1.35 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
package main
import (
"time"
"github.com/google/uuid"
"github.com/qor/transition"
)
// Execution is a object for each trigger of jobs.
type Execution struct {
ID string `gorm:"type:varchar(40); primary_key"`
JobID string `gorm:"type:varchar(40); not null"`
Job Job
TriggeredBy string `gorm:"type:varchar(255)"`
CreatedAt time.Time
UpdatedAt time.Time
transition.Transition
}
// NewExecutionOption is a struct of essential fields to create new execution.
// All fields required.
type NewExecutionOption struct {
JobID string
TriggeredBy string
}
// States:
// - created
// - started <- created
// - canceled <- created, started
// - finished <- started
var executionSM *transition.StateMachine
// NewExecution create new execution object and return it.
func NewExecution(opt *NewExecutionOption) *Execution {
id, _ := uuid.NewUUID()
exe := Execution{
ID: id.String(),
JobID: opt.JobID,
}
exe.SetState("created")
// TODO - save job to database
return &exe
}
// Start starts execution.
func (e *Execution) Start() error {
return executionSM.Trigger("start", e, nil)
}
func init() {
executionSM = transition.New(&Execution{})
executionSM.Initial("created")
executionSM.State("started")
executionSM.State("canceled")
executionSM.State("finished")
executionSM.Event("start").To("started").From("created")
}