Skip to content

Commit

Permalink
Support setting priority for setup experience-related scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mna committed Jan 7, 2025
1 parent e62c174 commit 359cc84
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
10 changes: 6 additions & 4 deletions ee/server/service/setup_experience.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,14 @@ func (svc *Service) SetupExperienceNextStep(ctx context.Context, hostUUID string
return false, ctxerr.Errorf(ctx, "setup experience script missing content id: %d", *script.SetupExperienceScriptID)
}
req := &fleet.HostScriptRequestPayload{
HostID: host.ID,
ScriptName: script.Name,
ScriptContentID: *script.ScriptContentID,
HostID: host.ID,
ScriptName: script.Name,
ScriptContentID: *script.ScriptContentID,
// because the script execution request is associated with setup experience,
// it will be enqueued with a higher priority and will run before other
// items in the queue.
SetupExperienceScriptID: script.SetupExperienceScriptID,
}
// TODO(mna): setup experience scripts go to the unified queue, but must be higher priority.
res, err := svc.ds.NewHostScriptExecutionRequest(ctx, req)
if err != nil {
return false, ctxerr.Wrap(ctx, err, "queueing setup experience script execution request")
Expand Down
21 changes: 16 additions & 5 deletions server/datastore/mysql/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ func newHostScriptExecutionRequest(ctx context.Context, tx sqlx.ExtContext, requ
const (
insStmt = `
INSERT INTO upcoming_activities
(host_id, user_id, activity_type, execution_id, script_id, script_content_id, policy_id, setup_experience_script_id, payload)
(
host_id, user_id, activity_type, execution_id, script_id, script_content_id,
policy_id, setup_experience_script_id, priority, payload
)
VALUES
(?, ?, 'script', ?, ?, ?, ?, ?,
(?, ?, 'script', ?, ?, ?, ?, ?, ?,
JSON_OBJECT(
'sync_request', ?,
'is_internal', ?,
Expand All @@ -88,6 +91,13 @@ WHERE
`
)

var priority int
if request.SetupExperienceScriptID != nil {
// a bit naive/simplistic for now, but we'll support user-provided
// priorities in a future story and we can improve on how we manage those.
priority = 100
}

execID := uuid.New().String()
result, err := tx.ExecContext(ctx, insStmt,
request.HostID,
Expand All @@ -97,6 +107,7 @@ WHERE
request.ScriptContentID,
request.PolicyID,
request.SetupExperienceScriptID,
priority,
request.SyncRequest,
isInternal,
request.UserID,
Expand Down Expand Up @@ -582,10 +593,10 @@ func (ds *Datastore) deletePendingHostScriptExecutionsForPolicy(ctx context.Cont
}

deleteUAStmt := `
DELETE FROM
DELETE FROM
upcoming_activities
WHERE
policy_id = ? AND
WHERE
policy_id = ? AND
activity_type = 'script' AND
script_id IN (
SELECT id FROM scripts WHERE scripts.global_or_team_id = ?
Expand Down

0 comments on commit 359cc84

Please sign in to comment.