set exit_code = -1 for scripts that are pending before upgrade to 4.44.0 (#16581)

for https://github.com/fleetdm/fleet/issues/16547

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

<!-- Note that API documentation changes are now addressed by the
product design team. -->

- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality
This commit is contained in:
Roberto Dip 2024-02-02 21:20:56 -03:00 committed by GitHub
parent dbf53cae6a
commit db58539c9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 16 deletions

View file

@ -49,23 +49,23 @@ func Up_20240126020643(tx *sql.Tx) error {
return errors.Wrap(err, "create host_activities table")
}
// Prior to this update, the database didn't differentiate between
// "async" and "sync" requests. With Fleet v4.44.0, all async requests
// will execute regardless of their pending duration. To avoid
// unintended execution of old requests upon server upgrade, these are
// now marked as "sync", reflecting their original 5-minute execution
// limit.
// Force an exit_code for scripts that didn't run previous to this update.
// In previous releases, scripts were allowed to be queued only for 5
// minutes, since we're removing that restriction, any scripts with a
// null exit_code would be unexpectedly sent to the host unless we do
// this.
const setOldScriptsAsSyncStmt = `
UPDATE host_script_results hsr
SET
sync_request = 1,
exit_code = -1,
updated_at = hsr.updated_at
WHERE
user_id IS NULL
exit_code IS NULL
AND user_id IS NULL
AND created_at < CURRENT_TIMESTAMP
`
if _, err := tx.Exec(setOldScriptsAsSyncStmt); err != nil {
return errors.Wrap(err, "set sync_request = 1 for old scripts")
return errors.Wrap(err, "set exit_code = -1 for old scripts")
}
return nil

View file

@ -32,24 +32,24 @@ func TestUp_20240126020643(t *testing.T) {
// existing host execution request's timestamp hasn't changed (despite
// added column, and modified sync_request)
type scriptResults struct {
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
SyncRequest bool `db:"sync_request"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
ExitCode *int `db:"exit_code"`
}
var sr scriptResults
err := db.Get(&sr, `SELECT created_at, updated_at, sync_request FROM host_script_results WHERE id = ?`, hsr1)
err := db.Get(&sr, `SELECT created_at, updated_at, exit_code FROM host_script_results WHERE id = ?`, hsr1)
require.NoError(t, err)
assert.Equal(t, minutesAgo, sr.CreatedAt)
assert.Equal(t, minutesAgo, sr.UpdatedAt)
assert.True(t, sr.SyncRequest)
assert.Equal(t, -1, *sr.ExitCode)
sr = scriptResults{}
err = db.Get(&sr, `SELECT created_at, updated_at, sync_request FROM host_script_results WHERE id = ?`, hsr2)
err = db.Get(&sr, `SELECT created_at, updated_at, exit_code FROM host_script_results WHERE id = ?`, hsr2)
require.NoError(t, err)
assert.Equal(t, minutesAgo, sr.CreatedAt)
assert.Equal(t, minutesAgo, sr.UpdatedAt)
assert.True(t, sr.SyncRequest)
assert.Equal(t, 1, *sr.ExitCode)
// create a new host execution request with user u1 and one with u2
hsr3 := execNoErrLastID(t, db, `INSERT INTO host_script_results (host_id, execution_id, script_contents, output, user_id) VALUES (?, ?, ?, ?, ?)`, 1, uuid.NewString(), "echo 'hello'", "", u1)