-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
116 lines (96 loc) · 3.21 KB
/
task.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2021, Justen Walker
// SPDX-License-Identifier: Apache-2.0
package gw
import (
"context"
"fmt"
"google.golang.org/grpc"
"go.justen.tech/goodwill/gw/config"
"go.justen.tech/goodwill/gw/docker"
"go.justen.tech/goodwill/gw/jsonstore"
"go.justen.tech/goodwill/gw/kv"
"go.justen.tech/goodwill/gw/lock"
"go.justen.tech/goodwill/gw/secret"
"go.justen.tech/goodwill/gw/taskcontext"
"go.justen.tech/goodwill/gw/value"
)
// newTask creates a new Task connection between the client and the GRPC service on the agent
func newTask(addr string, rt runtime, opts ...grpc.DialOption) (*Task, error) {
conn, err := grpc.Dial(addr, opts...)
if err != nil {
return nil, fmt.Errorf("unable to connect to grpc server %q: %w", addr, err)
}
return &Task{
OrgName: rt.OrgName,
WorkingDir: rt.WorkingDir,
ProcessID: rt.ProcessID,
ServerVersion: rt.ServerVersion,
conn: conn,
}, nil
}
// runtime contains the process runtime information
type runtime struct {
OrgName string
WorkingDir string
ProcessID string
ServerVersion string
}
type Task struct {
OrgName string
WorkingDir string
ProcessID string
ServerVersion string
conn *grpc.ClientConn
}
type TaskRunner interface {
Run(ctx context.Context, ts *Task) (map[string]value.Value, error)
}
type TaskRunnerFunc func(ctx context.Context, ts *Task) (map[string]value.Value, error)
func (f TaskRunnerFunc) Run(ctx context.Context, ts *Task) (map[string]value.Value, error) {
return f(ctx, ts)
}
const (
logLineVar = "_goodwill_log_line"
logLineCallExpr = `${log.call(` + logLineVar + `)}`
)
// Log emits a log entry on the process.
func (c *Task) Log(ctx context.Context, format string, v ...interface{}) error {
return c.Context().EvaluateParams(ctx, logLineCallExpr, value.Discard, map[string]value.Value{
logLineVar: value.String(fmt.Sprintf(format, v...)),
})
}
// Docker returns a service for running Docker containers
func (c *Task) Docker() *docker.Service {
return docker.NewService(c.conn)
}
// Context returns a service for interacting with the process context
// such as setting and getting variabes, and evaluating the results of Java Expressions in JEL 3.0
func (c *Task) Context() *taskcontext.Service {
return taskcontext.NewService(c.conn)
}
// Config returns a service for retrieving process configuration
func (c *Task) Config() *config.Service {
return config.NewService(c.conn)
}
// Secret returns a service for manipulating secrets
// within the current Organization
func (c *Task) Secret() *secret.Service {
return secret.NewService(c.OrgName, c.conn)
}
// JSONStore returns a jsonstore.Store for the given store name
// The store should already exist, otherwise operations will fail.
// This store should be in the current Org of the process.
func (c *Task) JSONStore(name string) *jsonstore.Store {
return jsonstore.NewService(c.OrgName, name, c.conn)
}
// Lock returns a service for setting project-level locks
func (c *Task) Lock() *lock.Service {
return lock.NewService(c.conn)
}
// KV returns a service for interacting with the project's key-value storage system
func (c *Task) KV() *kv.Service {
return kv.NewService(c.conn)
}
func (c *Task) Close() error {
return c.conn.Close()
}