MCPcopy Index your code
hub / github.com/PatchMon/PatchMon / ProcessTask

Method ProcessTask

server-source-code/internal/queue/jobs.go:505–581  ·  view source on GitHub ↗

ProcessTask implements asynq.Handler.

(ctx context.Context, t *asynq.Task)

Source from the content-addressed store, hash-verified

503
504// ProcessTask implements asynq.Handler.
505func (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) {

Callers

nothing calls this directly

Calls 8

UnmarshalMethod · 0.80
IsConnectedMethod · 0.80
SendMessageMethod · 0.80
GetHostByApiIDMethod · 0.65
InsertJobHistoryMethod · 0.65
GetFirstSettingsMethod · 0.65

Tested by

no test coverage detected