@@ -125,6 +125,7 @@ func New(opts *Options) (*Agent, error) {
125125 Monitor : opts .Monitor ,
126126 Concurrency : opts .Concurrency ,
127127 BufferSize : opts .BufferSize ,
128+ Codefresh : opts .Codefresh ,
128129 })
129130 return & Agent {
130131 id : id ,
@@ -192,7 +193,7 @@ func (a *Agent) startTaskPullerRoutine(ctx context.Context) {
192193
193194 // perform all agentTasks (in goroutine)
194195 for i := range agentTasks {
195- a .handleAgentTask (& agentTasks [i ])
196+ a .handleAgentTask (ctx , & agentTasks [i ])
196197 }
197198
198199 // send all wfTasks to tasksQueue
@@ -241,6 +242,25 @@ func (a *Agent) reportStatus(ctx context.Context, status codefresh.AgentStatus)
241242 }
242243}
243244
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+
244264func (a * Agent ) getTasks (ctx context.Context ) (task.Tasks , []* workflow.Workflow ) {
245265 tasks := a .pullTasks (ctx )
246266 return a .splitTasks (tasks )
@@ -276,18 +296,18 @@ func (a *Agent) splitTasks(tasks task.Tasks) (task.Tasks, []*workflow.Workflow)
276296 t .Timeline .Pulled = pullTime
277297 agentTasks = append (agentTasks , t )
278298 case task .TypeCreatePod , task .TypeCreatePVC , task .TypeDeletePod , task .TypeDeletePVC :
279- wf , ok := wfMap [t .Metadata .Workflow ]
299+ wf , ok := wfMap [t .Metadata .WorkflowId ]
280300 if ! ok {
281301 wf = workflow .New (t .Metadata )
282- wfMap [t .Metadata .Workflow ] = wf
302+ wfMap [t .Metadata .WorkflowId ] = wf
283303 }
284304
285305 err := wf .AddTask (& t )
286306 if err != nil {
287307 a .log .Error ("failed adding task to workflow" , "error" , err )
288308 }
289309 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 )
291311 }
292312 }
293313
@@ -313,14 +333,14 @@ func (a *Agent) splitTasks(tasks task.Tasks) (task.Tasks, []*workflow.Workflow)
313333 return agentTasks , workflows
314334}
315335
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 )
318338 a .wg .Add (1 )
319339 go func () {
320340 defer a .wg .Done ()
321341 txn := task .NewTaskTransaction (a .monitor , t .Metadata )
322342 defer txn .End ()
323- err := a .executeAgentTask (t )
343+ err := a .executeAgentTask (ctx , t )
324344
325345 if err != nil {
326346 a .log .Error (err .Error ())
@@ -330,7 +350,7 @@ func (a *Agent) handleAgentTask(t *task.Task) {
330350 }()
331351}
332352
333- func (a * Agent ) executeAgentTask (t * task.Task ) error {
353+ func (a * Agent ) executeAgentTask (ctx context. Context , t * task.Task ) error {
334354 t .Timeline .Started = time .Now ()
335355 specJSON , err := json .Marshal (t .Spec )
336356 if err != nil {
@@ -348,9 +368,12 @@ func (a *Agent) executeAgentTask(t *task.Task) error {
348368 }
349369
350370 err = e (& spec , a .log )
371+ if t .Metadata .ShouldReportStatus {
372+ a .reportTaskStatus (ctx , * t , err )
373+ }
351374 sinceCreation , inRunner , processed := t .GetLatency ()
352375 a .log .Info ("Done handling agent task" ,
353- "tid" , t .Metadata .Workflow ,
376+ "tid" , t .Metadata .WorkflowId ,
354377 "time since creation" , sinceCreation ,
355378 "time in runner" , inRunner ,
356379 "processing time" , processed ,
@@ -410,7 +433,7 @@ func proxyRequest(t *task.AgentTask, log logger.Logger) error {
410433func groupTasks (tasks task.Tasks ) map [string ]task.Tasks {
411434 candidates := map [string ]task.Tasks {}
412435 for _ , task := range tasks {
413- name := task .Metadata .Workflow
436+ name := task .Metadata .WorkflowId
414437 if name == "" {
415438 // If for some reason the task is not related to any workflow
416439 // Might heppen in older versions on Codefresh
0 commit comments