/
output.go
64 lines (53 loc) · 1.52 KB
/
output.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
package worker
import (
"context"
"log"
"time"
"github.com/kcarretto/paragon/graphql"
"github.com/kcarretto/paragon/graphql/models"
)
// taskOutput acts like a stream buffer which submits task execution results as they are written.
type taskOutput struct {
ID int
Ctx context.Context
Graph graphql.Client
id int
err *string
}
// Start submits an initial result for the task, indicating it's execution start time.
func (t *taskOutput) Start() {
now := time.Now()
if err := t.Graph.SubmitTaskResult(t.Ctx, models.SubmitTaskResultRequest{
ID: t.ID,
ExecStartTime: &now,
}); err != nil {
log.Printf("[ERR] Failed to submit task result for execution start: %s", err.Error())
}
}
// Stop submits the final result for the task, indicating it's execution stop time and any errors.
func (t *taskOutput) Stop(execErr error) {
now := time.Now()
if execErr != nil {
errMsg := execErr.Error()
t.err = &errMsg
}
if err := t.Graph.SubmitTaskResult(t.Ctx, models.SubmitTaskResultRequest{
ID: t.ID,
Error: t.err,
ExecStopTime: &now,
}); err != nil {
log.Printf("[ERR] Failed to submit task result for execution stop: %s", err.Error())
}
}
// Write submits task output.
func (t *taskOutput) Write(p []byte) (int, error) {
output := string(p)
if err := t.Graph.SubmitTaskResult(t.Ctx, models.SubmitTaskResultRequest{
ID: t.ID,
Output: &output,
}); err != nil {
log.Printf("[ERR] Failed to submit task execution result: %s", err.Error())
return 0, err
}
return len(p), nil
}