-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
61 lines (50 loc) · 1.08 KB
/
context.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
package work
import (
"context"
"sync"
)
// Context for the Job and is reset for every execution
type Context struct {
// context.Context allows it to be a compatible context
context.Context
// status records the job's execution Status (StatusSuccess, StatusBadRequest, etc)
status Status
// moot keeps things sycronized
moot *sync.Mutex
// keys stores key/values for the context
keys map[string]interface{}
}
// NewContext factory
func NewContext(ctx context.Context) Context {
return Context{
ctx,
-1,
&sync.Mutex{},
map[string]interface{}{},
}
}
// Get a key/value
func (c *Context) Get(k string) (interface{}, bool) {
c.moot.Lock()
defer c.moot.Unlock()
value, exists := c.keys[k]
return value, exists
}
// Set a key/value
func (c *Context) Set(k string, v interface{}) {
c.moot.Lock()
defer c.moot.Unlock()
c.keys[k] = v
}
// Status retrieves the context's status
func (c *Context) Status() Status {
return c.status
}
// SetStatus for the context
func (c *Context) SetStatus(s Status) {
if c.status != 0 {
}
c.moot.Lock()
defer c.moot.Unlock()
c.status = s
}