-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
49 lines (42 loc) · 1.18 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
package sup
import (
"context"
)
type Context = context.Context
type ctxKey struct{}
type ctxInfo struct {
task *boundTask
path string
}
func appendCtxInfo(ctx Context, x ctxInfo) Context {
return context.WithValue(ctx, ctxKey{}, x)
}
// CtxTaskName returns the name of the current task
// (or if there is no task annotated as owner of this context,
// returns the empty string).
//
// Task name and path info is annotated when tasks are launched by supervisors,
// and may be missing if you call a task's Run method manually.
func CtxTaskName(ctx Context) string {
ctxInfo, ok := ctx.Value(ctxKey{}).(ctxInfo)
if !ok {
return ""
}
return ctxInfo.task.name
}
// CtxTaskPath returns the full path of names for each task in the supervision
// tree above this one
// (or if there is no task annotated as owner of this context,
// returns the empty string).
//
// The path is separated by slashes, as per file paths.
//
// Task name and path info is annotated when tasks are launched by supervisors,
// and may be missing if you call a task's Run method manually.
func CtxTaskPath(ctx Context) string {
ctxInfo, ok := ctx.Value(ctxKey{}).(ctxInfo)
if !ok {
return ""
}
return ctxInfo.path
}