Skip to content

Commit

Permalink
Improve the claim query, and remove a now unused index.
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Apr 1, 2024
1 parent b7c1769 commit 835024c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 2 deletions.
2 changes: 1 addition & 1 deletion nanoq.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (q *Client) RunTransaction(ctx context.Context, fn func(tx *sqlx.Tx) error)
// The claim is valid until the transaction is committed or rolled back.
func (q *Client) ClaimTask(ctx context.Context, tx *sqlx.Tx) (Task, error) {
t := Task{}
err := tx.GetContext(ctx, &t, `SELECT * FROM tasks WHERE scheduled_at <= UTC_TIMESTAMP() ORDER BY created_at ASC LIMIT 1 FOR UPDATE SKIP LOCKED`)
err := tx.GetContext(ctx, &t, `SELECT * FROM tasks WHERE scheduled_at <= UTC_TIMESTAMP() ORDER BY scheduled_at ASC LIMIT 1 FOR UPDATE SKIP LOCKED`)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return t, ErrNoTasks
Expand Down
1 change: 0 additions & 1 deletion table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ CREATE TABLE `tasks` (
`scheduled_at` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (`id`),
UNIQUE KEY `fingerprint` (`fingerprint`),
KEY `created_at` (`created_at`),
KEY `scheduled_at` (`scheduled_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Expand Down

0 comments on commit 835024c

Please sign in to comment.