Skip to content

Commit

Permalink
Rearrange NewDistributedQueryCampaign to avoid leaving orphaned live …
Browse files Browse the repository at this point in the history
…queries if ctx canceled
  • Loading branch information
lucasmrod committed Feb 29, 2024
1 parent fa1cc0e commit c60310b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 5 additions & 5 deletions server/service/campaigns.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,17 @@ func (svc *Service) NewDistributedQueryCampaign(ctx context.Context, queryString
}
}

err = svc.liveQueryStore.RunQuery(strconv.Itoa(int(campaign.ID)), queryString, hostIDs)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "run query")
}

// Metrics are used for total hosts targeted for the activity feed.
campaign.Metrics, err = svc.ds.CountHostsInTargets(ctx, filter, targets, time.Now())
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "counting hosts")
}

err = svc.liveQueryStore.RunQuery(strconv.Itoa(int(campaign.ID)), queryString, hostIDs)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "run query")
}

return campaign, nil
}

Expand Down
7 changes: 7 additions & 0 deletions server/service/live_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,15 @@ func (svc *Service) RunLiveQueryDeadline(
queryIDPtr = nil
queryString = query
}

campaign, err := svc.NewDistributedQueryCampaign(ctx, queryString, queryIDPtr, fleet.HostTargets{HostIDs: hostIDs})
if err != nil {
level.Error(svc.logger).Log(
"msg", "new distributed query campaign",
"queryString", queryString,
"queryID", queryID,
"err", err,
)
resultsCh <- fleet.QueryCampaignResult{QueryID: queryID, Error: ptr.String(err.Error()), Err: err}
return
}
Expand Down

0 comments on commit c60310b

Please sign in to comment.