/
process.go
187 lines (170 loc) · 5.1 KB
/
process.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package worker
import (
"context"
"fmt"
"strings"
"time"
"github.com/pkg/errors"
"github.com/mendersoftware/go-lib-micro/log"
"github.com/mendersoftware/workflows/app/processor"
"github.com/mendersoftware/workflows/client/nats"
"github.com/mendersoftware/workflows/model"
"github.com/mendersoftware/workflows/store"
)
// this variable is set when running acceptance tests to disable ephemeral jobs
// without it, it is not possible to inspect the jobs collection to assert the
// values stored in the database
var NoEphemeralWorkflows = false
func processJob(ctx context.Context, job *model.Job,
dataStore store.DataStore, nats nats.Client) error {
l := log.FromContext(ctx)
workflow, err := dataStore.GetWorkflowByName(ctx, job.WorkflowName, job.WorkflowVersion)
if err != nil {
l.Warnf("The workflow %q of job %s does not exist: %v",
job.WorkflowName, job.ID, err)
err := dataStore.UpdateJobStatus(ctx, job, model.StatusFailure)
if err != nil {
return err
}
return nil
}
if !workflow.Ephemeral || NoEphemeralWorkflows {
job.Status = model.StatusPending
_, err = dataStore.UpsertJob(ctx, job)
if err != nil {
return errors.Wrap(err, "insert of the job failed")
}
}
l.Infof("%s: started, %s", job.ID, job.WorkflowName)
success := true
for _, task := range workflow.Tasks {
l.Infof("%s: started, %s task :%s", job.ID, job.WorkflowName, task.Name)
var (
result *model.TaskResult
attempt uint8 = 0
)
for attempt <= task.Retries {
result, err = processTask(task, job, workflow, nats, l)
if err != nil {
_ = dataStore.UpdateJobStatus(ctx, job, model.StatusFailure)
return err
}
attempt++
if result.Success {
break
}
if task.RetryDelaySeconds > 0 {
time.Sleep(time.Duration(task.RetryDelaySeconds) * time.Second)
}
}
job.Results = append(job.Results, *result)
if !workflow.Ephemeral || !result.Success || NoEphemeralWorkflows {
err = dataStore.UpdateJobAddResult(ctx, job, result)
if err != nil {
l.Errorf("Error uploading results: %s", err.Error())
}
}
if !result.Success {
success = false
break
}
}
var status int32
if success {
status = model.StatusDone
} else {
status = model.StatusFailure
}
if !workflow.Ephemeral || NoEphemeralWorkflows {
newStatus := model.StatusToString(status)
err = dataStore.UpdateJobStatus(ctx, job, status)
if err != nil {
l.Warn(fmt.Sprintf("Unable to set job status to %s", newStatus))
return err
}
}
l.Infof("%s: done", job.ID)
return nil
}
func processTask(task model.Task, job *model.Job,
workflow *model.Workflow, nats nats.Client, l *log.Logger) (*model.TaskResult, error) {
var result *model.TaskResult
var err error
ps := processor.NewJobStringProcessor(workflow, job)
jp := processor.NewJobProcessor(job)
if len(task.Requires) > 0 {
for _, require := range task.Requires {
require = ps.ProcessJobString(require)
require = ps.MaybeExecuteGoTemplate(require)
if require == "" {
result := &model.TaskResult{
Name: task.Name,
Type: task.Type,
Success: true,
Skipped: true,
}
return result, nil
}
}
}
switch task.Type {
case model.TaskTypeHTTP:
var httpTask *model.HTTPTask = task.HTTP
if httpTask == nil {
return nil, fmt.Errorf(
"Error: Task definition incompatible " +
"with specified type (http)")
}
result, err = processHTTPTask(httpTask, ps, jp, l)
case model.TaskTypeCLI:
var cliTask *model.CLITask = task.CLI
if cliTask == nil {
return nil, fmt.Errorf(
"Error: Task definition incompatible " +
"with specified type (cli)")
}
result, err = processCLITask(cliTask, ps, jp)
case model.TaskTypeNATS:
var natsTask *model.NATSTask = task.NATS
if natsTask == nil {
return nil, fmt.Errorf(
"Error: Task definition incompatible " +
"with specified type (nats)")
}
result, err = processNATSTask(natsTask, ps, jp, nats)
case model.TaskTypeSMTP:
var smtpTask *model.SMTPTask = task.SMTP
if smtpTask == nil {
return nil, fmt.Errorf(
"Error: Task definition incompatible " +
"with specified type (smtp)")
}
l.Infof("processTask: calling smtp task: From: %s To: %s Subject: %s",
ps.ProcessJobString(smtpTask.From),
ps.ProcessJobString(strings.Join(smtpTask.To, ",")),
ps.ProcessJobString(smtpTask.Subject),
)
result, err = processSMTPTask(smtpTask, ps, jp, l)
default:
result = nil
err = fmt.Errorf("Unrecognized task type: %s", task.Type)
}
if result != nil {
result.Name = task.Name
result.Type = task.Type
}
return result, err
}