forked from fieldryand/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
94 lines (83 loc) · 2.64 KB
/
example.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package goflow
import (
"errors"
"math/rand"
)
// Crunch some numbers
func complexAnalyticsJob() *Job {
j := &Job{
Name: "example-complex-analytics",
Schedule: "* * * * * *",
Active: false,
}
j.Add(&Task{
Name: "sleep-one",
Operator: Command{Cmd: "sleep", Args: []string{"1"}},
})
j.Add(&Task{
Name: "add-one-one",
Operator: Command{Cmd: "sh", Args: []string{"-c", "echo $((1 + 1))"}},
})
j.Add(&Task{
Name: "sleep-two",
Operator: Command{Cmd: "sleep", Args: []string{"2"}},
})
j.Add(&Task{
Name: "add-two-four",
Operator: Command{Cmd: "sh", Args: []string{"-c", "echo $((2 + 4))"}},
})
j.Add(&Task{
Name: "add-three-four",
Operator: Command{Cmd: "sh", Args: []string{"-c", "echo $((3 + 4))"}},
})
j.Add(&Task{
Name: "whoops-with-constant-delay",
Operator: Command{Cmd: "whoops", Args: []string{}},
Retries: 5,
RetryDelay: ConstantDelay{Period: 1},
})
j.Add(&Task{
Name: "whoops-with-exponential-backoff",
Operator: Command{Cmd: "whoops", Args: []string{}},
Retries: 1,
RetryDelay: ExponentialBackoff{},
})
j.Add(&Task{
Name: "totally-skippable",
Operator: Command{Cmd: "sh", Args: []string{"-c", "echo 'everything succeeded'"}},
TriggerRule: "allSuccessful",
})
j.Add(&Task{
Name: "clean-up",
Operator: Command{Cmd: "sh", Args: []string{"-c", "echo 'cleaning up now'"}},
TriggerRule: "allDone",
})
j.SetDownstream(j.Task("sleep-one"), j.Task("add-one-one"))
j.SetDownstream(j.Task("add-one-one"), j.Task("sleep-two"))
j.SetDownstream(j.Task("sleep-two"), j.Task("add-two-four"))
j.SetDownstream(j.Task("add-one-one"), j.Task("add-three-four"))
j.SetDownstream(j.Task("sleep-one"), j.Task("whoops-with-constant-delay"))
j.SetDownstream(j.Task("sleep-one"), j.Task("whoops-with-exponential-backoff"))
j.SetDownstream(j.Task("whoops-with-constant-delay"), j.Task("totally-skippable"))
j.SetDownstream(j.Task("whoops-with-exponential-backoff"), j.Task("totally-skippable"))
j.SetDownstream(j.Task("totally-skippable"), j.Task("clean-up"))
return j
}
// RandomFailure fails randomly. This is a contrived example for demo purposes.
type RandomFailure struct{ n int }
// rng with seed=1
var r = rand.New(rand.NewSource(1))
// Run implements failures at random intervals.
func (o RandomFailure) Run() (interface{}, error) {
x := r.Intn(o.n)
if x == o.n-1 {
return nil, errors.New("unlucky")
}
return x, nil
}
// Use our custom operation in a job.
func customOperatorJob() *Job {
j := &Job{Name: "example-custom-operator", Schedule: "* * * * * *", Active: true}
j.Add(&Task{Name: "random-failure", Operator: RandomFailure{4}})
return j
}