-
Notifications
You must be signed in to change notification settings - Fork 1
/
cancel_check.go
99 lines (87 loc) · 2.22 KB
/
cancel_check.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 orchestrator
import (
"bytes"
"context"
"encoding/json"
"net/http"
"time"
artifact "github.com/deepcode-ai/artifacts/types"
"github.com/google/uuid"
)
const (
cancelCheckPublishPath = "/api/runner/cancel-check/results"
)
type CancelCheckTask struct {
runner *Runner
opts *TaskOpts
driver Driver
signer Signer
client *http.Client
}
// NewCancelCheckTask registers a new cancel check task with the supplied properties of
// driver, provider facade and license store.
func NewCancelCheckTask(runner *Runner, opts *TaskOpts, driver Driver, signer Signer, client *http.Client) *CancelCheckTask {
t := &CancelCheckTask{
opts: opts,
driver: driver,
signer: signer,
client: client,
runner: runner,
}
if t.client == nil {
t.client = http.DefaultClient
}
return t
}
// Run creates the template for the cancel check job and triggers the cancel check job.
func (t *CancelCheckTask) Run(ctx context.Context, run *artifact.CancelCheckRun) error {
job, err := NewCancelCheckDriverJob(run, &CancelCheckOpts{
KubernetesOpts: t.opts.KubernetesOpts,
})
if err != nil {
return err
}
result := artifact.CancelCheckResult{}
if err := t.driver.DeleteJob(ctx, job); err != nil {
result.RunID = run.RunID
result.Status = artifact.Status{
Code: 5001,
HMessage: "Error cancelling the check",
Err: err.Error(),
}
} else {
result.RunID = run.RunID
result.Status = artifact.Status{
Code: 5000,
HMessage: "Check cancelled successfully",
Err: "",
}
}
payload := artifact.CancelCheckResultCeleryTask{
ID: uuid.NewString(),
Task: "cancel-check",
KWArgs: result,
Retries: 0,
}
payloadJSON, err := json.Marshal(payload)
if err != nil {
return err
}
publisherURL := t.opts.RemoteHost + cancelCheckPublishPath
publisherToken, err := t.signer.GenerateToken(t.runner.ID, []string{ScopeAnalysis}, nil, 30*time.Minute)
if err != nil {
return err
}
req, err := http.NewRequest("POST", publisherURL, bytes.NewReader(payloadJSON))
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+publisherToken)
req.Header.Set("Content-Type", "application/json")
resp, err := t.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}