We do scheduling for IAmBored in a schedule function. Typically, this looks like below.
addTaskFunc(func(taskID harmonytask.TaskID, tx *harmonydb.Tx) (shouldCommit bool, seriousError error) {
stop = true
var pendings []struct {
ID int64 `db:"id"`
}
err := tx.Select(&pendings, `SELECT id
FROM pdp_delete_data_set
WHERE cleanup_pieces_task_id IS NULL
AND cleanup_pieces_tx_hash IS NULL
AND after_delete_data_set = TRUE
AND delete_tx_hash IS NULL
AND service_termination_epoch IS NOT NULL
AND terminated = FALSE
ORDER BY id`)
if err != nil {
return false, xerrors.Errorf("failed to select pending PDP cleanup data sets: %w", err)
}
if len(pendings) == 0 {
log.Debugw("no pending PDP data sets for piece cleanup")
return false, nil
}
pending := pendings[0]
n, err := tx.Exec(`UPDATE pdp_delete_data_set
SET cleanup_pieces_task_id = $1
WHERE id = $2
AND cleanup_pieces_task_id IS NULL
AND cleanup_pieces_tx_hash IS NULL
AND after_delete_data_set = TRUE
AND delete_tx_hash IS NULL
AND service_termination_epoch IS NOT NULL
AND terminated = FALSE`, taskID, pending.ID)
if err != nil {
return false, xerrors.Errorf("failed to assign PDP cleanup task: %w", err)
}
if n != 1 {
return false, xerrors.Errorf("updated %d rows assigning PDP cleanup task", n)
}
log.Debugw("scheduled PDP cleanupPieces task", "dataSetId", pending.ID)
stop = false
return true, nil
})
We can combine above in a single SQL execution with just setting the ID for first row with condition and then return true if n == 1. This pattern can be used for all schedule function which do not require additional processing before assigning the task.
We do scheduling for IAmBored in a schedule function. Typically, this looks like below.
We can combine above in a single SQL execution with just setting the ID for first row with condition and then return true if n == 1. This pattern can be used for all schedule function which do not require additional processing before assigning the task.