-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.dispatch.go
68 lines (57 loc) · 1.35 KB
/
queries.dispatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package pgotask
import (
"context"
"database/sql"
"errors"
"log/slog"
"time"
)
var lockTasksQuery = `LOCK ` + table[Task]()
func lockTasks(ctx context.Context, tx *sql.Tx) error {
slog.DebugContext(ctx, "Executing query",
slog.String("query", lockTasksQuery),
)
if _, err := tx.ExecContext(ctx, lockTasksQuery); err != nil {
return errors.Join(ErrExecQuery, err)
}
return nil
}
var findPendingTasksQuery = `
SELECT
` + join(columns[Task](false)) + `
FROM ` + table[Task]() + `
WHERE
` + column[Task]("completed_at") + ` IS NULL AND
` + column[Task]("dispatch_after") + ` <= $1
ORDER BY ` + column[Task]("dispatch_after")
func findPendingTasks(ctx context.Context, tx *sql.Tx) ([]Task, error) {
now := time.Now()
slog.DebugContext(ctx, "Executing query",
slog.String("query", findPendingTasksQuery),
slog.Time("$1", now),
)
rows, err := tx.QueryContext(ctx, findPendingTasksQuery, now)
if err != nil {
return nil, errors.Join(ErrExecQuery, err)
}
defer rows.Close()
tasks := make([]Task, 0)
for rows.Next() {
var task Task
if err := rows.Scan(
&task.ID,
&task.Type,
&task.TypeVersion,
&task.Payload,
&task.Idempotent,
&task.DispatchAfter,
&task.CompletedAt,
&task.CreatedAt,
&task.UpdatedAt,
); err != nil {
return nil, errors.Join(ErrScanRow, err)
}
tasks = append(tasks, task)
}
return tasks, nil
}