-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathactivity.go
executable file
·99 lines (75 loc) · 1.88 KB
/
activity.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
95
96
97
98
99
package counter
import (
"sync/atomic"
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data/metadata"
)
const (
ovValue = "value"
)
var counters = make(map[string]*Counter)
type CounterFunc func() uint64
type Settings struct {
CounterName string `md:"counterName,required"` // The name of the counter
Op string `md:"op,allowed(get,increment,reset)"` // The counter operation, 'get' is the default operation
}
type Output struct {
Value int `md:"value"` // The result of the counter operation
}
func init() {
_ = activity.Register(&Activity{}, New)
}
var activityMd = activity.ToMetadata(&Settings{}, &Output{})
// Activity is a Counter Activity implementation
type Activity struct {
invoke CounterFunc
}
func New(ctx activity.InitContext) (activity.Activity, error) {
s := &Settings{}
err := metadata.MapToStruct(ctx.Settings(), s, true)
if err != nil {
return nil, err
}
act := &Activity{}
counter, exists := counters[s.CounterName]
if !exists {
//log creating counter
counter = &Counter{val: 0}
counters[s.CounterName] = counter
}
switch s.Op {
case "increment":
act.invoke = counter.Increment
case "reset":
act.invoke = counter.Reset
default:
act.invoke = counter.Get
}
return act, nil
}
// Metadata implements activity.Activity.Metadata
func (a *Activity) Metadata() *activity.Metadata {
return activityMd
}
// Eval implements activity.Activity.Eval
func (a *Activity) Eval(context activity.Context) (done bool, err error) {
val := a.invoke()
err = context.SetOutput(ovValue, int(val))
if err != nil {
return false, err
}
return true, nil
}
type Counter struct {
val uint64
}
func (c *Counter) Get() uint64 {
return atomic.LoadUint64(&c.val)
}
func (c *Counter) Increment() uint64 {
return atomic.AddUint64(&c.val, 1)
}
func (c *Counter) Reset() uint64 {
atomic.StoreUint64(&c.val, 0)
return 0
}