@@ -125,6 +125,7 @@ func New(opts *Options) (*Agent, error) {
125
125
Monitor : opts .Monitor ,
126
126
Concurrency : opts .Concurrency ,
127
127
BufferSize : opts .BufferSize ,
128
+ Codefresh : opts .Codefresh ,
128
129
})
129
130
return & Agent {
130
131
id : id ,
@@ -192,7 +193,7 @@ func (a *Agent) startTaskPullerRoutine(ctx context.Context) {
192
193
193
194
// perform all agentTasks (in goroutine)
194
195
for i := range agentTasks {
195
- a .handleAgentTask (& agentTasks [i ])
196
+ a .handleAgentTask (ctx , & agentTasks [i ])
196
197
}
197
198
198
199
// send all wfTasks to tasksQueue
@@ -241,6 +242,25 @@ func (a *Agent) reportStatus(ctx context.Context, status codefresh.AgentStatus)
241
242
}
242
243
}
243
244
245
+ func (a * Agent ) reportTaskStatus (ctx context.Context , taskDef task.Task , err error ) {
246
+ status := task.TaskStatus {
247
+ OccurredAt : time .Now (),
248
+ StatusRevision : taskDef .Metadata .CurrentStatusRevision + 1 ,
249
+ }
250
+ if err != nil {
251
+ status .Status = task .StatusError
252
+ status .Reason = err .Error ()
253
+ status .IsRetriable = true // TODO: make this configurable depending on the error
254
+ } else {
255
+ status .Status = task .StatusSuccess
256
+ }
257
+
258
+ statusErr := a .cf .ReportTaskStatus (ctx , taskDef .Id , status )
259
+ if statusErr != nil {
260
+ a .log .Error ("failed reporting task status" , "error" , statusErr , "task" , taskDef .Id , "workflow" , taskDef .Metadata .WorkflowId )
261
+ }
262
+ }
263
+
244
264
func (a * Agent ) getTasks (ctx context.Context ) (task.Tasks , []* workflow.Workflow ) {
245
265
tasks := a .pullTasks (ctx )
246
266
return a .splitTasks (tasks )
@@ -276,18 +296,18 @@ func (a *Agent) splitTasks(tasks task.Tasks) (task.Tasks, []*workflow.Workflow)
276
296
t .Timeline .Pulled = pullTime
277
297
agentTasks = append (agentTasks , t )
278
298
case task .TypeCreatePod , task .TypeCreatePVC , task .TypeDeletePod , task .TypeDeletePVC :
279
- wf , ok := wfMap [t .Metadata .Workflow ]
299
+ wf , ok := wfMap [t .Metadata .WorkflowId ]
280
300
if ! ok {
281
301
wf = workflow .New (t .Metadata )
282
- wfMap [t .Metadata .Workflow ] = wf
302
+ wfMap [t .Metadata .WorkflowId ] = wf
283
303
}
284
304
285
305
err := wf .AddTask (& t )
286
306
if err != nil {
287
307
a .log .Error ("failed adding task to workflow" , "error" , err )
288
308
}
289
309
default :
290
- a .log .Error ("unrecognized task type" , "type" , t .Type , "tid" , t .Metadata .Workflow , "runtime" , t .Metadata .ReName )
310
+ a .log .Error ("unrecognized task type" , "type" , t .Type , "tid" , t .Metadata .WorkflowId , "runtime" , t .Metadata .ReName )
291
311
}
292
312
}
293
313
@@ -313,14 +333,14 @@ func (a *Agent) splitTasks(tasks task.Tasks) (task.Tasks, []*workflow.Workflow)
313
333
return agentTasks , workflows
314
334
}
315
335
316
- func (a * Agent ) handleAgentTask (t * task.Task ) {
317
- a .log .Info ("executing agent task" , "tid" , t .Metadata .Workflow )
336
+ func (a * Agent ) handleAgentTask (ctx context. Context , t * task.Task ) {
337
+ a .log .Info ("executing agent task" , "tid" , t .Metadata .WorkflowId )
318
338
a .wg .Add (1 )
319
339
go func () {
320
340
defer a .wg .Done ()
321
341
txn := task .NewTaskTransaction (a .monitor , t .Metadata )
322
342
defer txn .End ()
323
- err := a .executeAgentTask (t )
343
+ err := a .executeAgentTask (ctx , t )
324
344
325
345
if err != nil {
326
346
a .log .Error (err .Error ())
@@ -330,7 +350,7 @@ func (a *Agent) handleAgentTask(t *task.Task) {
330
350
}()
331
351
}
332
352
333
- func (a * Agent ) executeAgentTask (t * task.Task ) error {
353
+ func (a * Agent ) executeAgentTask (ctx context. Context , t * task.Task ) error {
334
354
t .Timeline .Started = time .Now ()
335
355
specJSON , err := json .Marshal (t .Spec )
336
356
if err != nil {
@@ -348,9 +368,12 @@ func (a *Agent) executeAgentTask(t *task.Task) error {
348
368
}
349
369
350
370
err = e (& spec , a .log )
371
+ if t .Metadata .ShouldReportStatus {
372
+ a .reportTaskStatus (ctx , * t , err )
373
+ }
351
374
sinceCreation , inRunner , processed := t .GetLatency ()
352
375
a .log .Info ("Done handling agent task" ,
353
- "tid" , t .Metadata .Workflow ,
376
+ "tid" , t .Metadata .WorkflowId ,
354
377
"time since creation" , sinceCreation ,
355
378
"time in runner" , inRunner ,
356
379
"processing time" , processed ,
@@ -410,7 +433,7 @@ func proxyRequest(t *task.AgentTask, log logger.Logger) error {
410
433
func groupTasks (tasks task.Tasks ) map [string ]task.Tasks {
411
434
candidates := map [string ]task.Tasks {}
412
435
for _ , task := range tasks {
413
- name := task .Metadata .Workflow
436
+ name := task .Metadata .WorkflowId
414
437
if name == "" {
415
438
// If for some reason the task is not related to any workflow
416
439
// Might heppen in older versions on Codefresh
0 commit comments