Skip to content

Commit

Permalink
Adapt is execution pending on host check
Browse files Browse the repository at this point in the history
  • Loading branch information
mna committed Jan 7, 2025
1 parent 68b9ec4 commit 6fa982b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
11 changes: 10 additions & 1 deletion server/datastore/mysql/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,19 @@ func (ds *Datastore) IsExecutionPendingForHost(ctx context.Context, hostID uint,
host_id = ? AND
script_id = ? AND
exit_code IS NULL
UNION
SELECT
1
FROM
upcoming_activities
WHERE
host_id = ? AND
activity_type = 'script' AND
script_id = ?
`

var results []*uint
if err := sqlx.SelectContext(ctx, ds.reader(ctx), &results, getStmt, hostID, scriptID); err != nil {
if err := sqlx.SelectContext(ctx, ds.reader(ctx), &results, getStmt, hostID, scriptID, hostID, scriptID); err != nil {
return false, ctxerr.Wrap(ctx, err, "is execution pending for host")
}
return len(results) > 0, nil
Expand Down
1 change: 0 additions & 1 deletion server/service/osquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -1984,7 +1984,6 @@ func (svc *Service) processScriptsForNewlyFailingPolicies(
continue
}

// TODO(mna): this has to look both in host_script_results and the upcoming queue
scriptIsAlreadyPending, err := svc.ds.IsExecutionPendingForHost(ctx, hostID, scriptMetadata.ID)
if err != nil {
return ctxerr.Wrap(ctx, err, "check whether script is pending execution")
Expand Down
1 change: 0 additions & 1 deletion server/service/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ func (svc *Service) RunHostScript(ctx context.Context, request *fleet.HostScript
return nil, fleet.NewInvalidArgumentError("script_id", `The script does not belong to the same team (or no team) as the host.`)
}

// TODO(mna): must look in host_script_results and the upcoming queue
isQueued, err := svc.ds.IsExecutionPendingForHost(ctx, request.HostID, *request.ScriptID)
if err != nil {
return nil, err
Expand Down

0 comments on commit 6fa982b

Please sign in to comment.