ProcessTask implements asynq.Handler.
(ctx context.Context, t *asynq.Task)
| 503 | |
| 504 | // ProcessTask implements asynq.Handler. |
| 505 | func (h *UpdateAgentHandler) ProcessTask(ctx context.Context, t *asynq.Task) error { |
| 506 | var p UpdateAgentPayload |
| 507 | if err := json.Unmarshal(t.Payload(), &p); err != nil { |
| 508 | return err |
| 509 | } |
| 510 | |
| 511 | taskID, _ := asynq.GetTaskID(ctx) |
| 512 | retryCount, _ := asynq.GetRetryCount(ctx) |
| 513 | attempt := int32(retryCount + 1) |
| 514 | |
| 515 | if h.db != nil && taskID != "" && retryCount == 0 { |
| 516 | host, err := h.db.Queries.GetHostByApiID(ctx, p.ApiID) |
| 517 | var hostID *string |
| 518 | if err == nil { |
| 519 | hostID = &host.ID |
| 520 | } |
| 521 | apiIDPtr := &p.ApiID |
| 522 | _ = h.db.Queries.InsertJobHistory(ctx, db.InsertJobHistoryParams{ |
| 523 | ID: uuid.New().String(), |
| 524 | JobID: taskID, |
| 525 | QueueName: QueueAgentCommands, |
| 526 | JobName: TypeUpdateAgent, |
| 527 | HostID: hostID, |
| 528 | ApiID: apiIDPtr, |
| 529 | Status: "active", |
| 530 | AttemptNumber: attempt, |
| 531 | }) |
| 532 | } |
| 533 | |
| 534 | if !p.BypassSettings { |
| 535 | settings, err := h.db.Queries.GetFirstSettings(ctx) |
| 536 | if err != nil || !settings.AutoUpdate { |
| 537 | msg := "Auto-update is disabled in server settings" |
| 538 | if taskID != "" && h.db != nil { |
| 539 | _ = h.db.Queries.UpdateJobHistoryFailed(ctx, db.UpdateJobHistoryFailedParams{JobID: taskID, ErrorMessage: &msg}) |
| 540 | } |
| 541 | h.log.Info("update_agent: skipped", "api_id", p.ApiID, "reason", msg) |
| 542 | return nil |
| 543 | } |
| 544 | host, err := h.db.Queries.GetHostByApiID(ctx, p.ApiID) |
| 545 | if err != nil { |
| 546 | msg := "Host not found" |
| 547 | if taskID != "" && h.db != nil { |
| 548 | _ = h.db.Queries.UpdateJobHistoryFailed(ctx, db.UpdateJobHistoryFailedParams{JobID: taskID, ErrorMessage: &msg}) |
| 549 | } |
| 550 | return nil |
| 551 | } |
| 552 | if !host.AutoUpdate { |
| 553 | msg := "Auto-update is disabled for this host" |
| 554 | if taskID != "" && h.db != nil { |
| 555 | _ = h.db.Queries.UpdateJobHistoryFailed(ctx, db.UpdateJobHistoryFailedParams{JobID: taskID, ErrorMessage: &msg}) |
| 556 | } |
| 557 | h.log.Info("update_agent: skipped", "api_id", p.ApiID, "reason", msg) |
| 558 | return nil |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | if !h.registry.IsConnected(p.ApiID) { |
nothing calls this directly
no test coverage detected