-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.sql
More file actions
89 lines (81 loc) · 2.32 KB
/
query.sql
File metadata and controls
89 lines (81 loc) · 2.32 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
-- name: PopContentQueueItems :many
WITH cte AS (
SELECT id
FROM content_queue
WHERE content_queue.attempts <= $1
AND (
(status = 'waiting' AND (locked_until IS NULL OR locked_until < now()))
OR (status = 'processing' AND locked_until < now())
)
ORDER BY enqueued_at
LIMIT $2
FOR UPDATE SKIP LOCKED
)
UPDATE content_queue q
SET status = 'processing',
locked_until = now() + interval '120 seconds',
attempts = attempts + 1
FROM cte
WHERE q.id = cte.id
RETURNING q.*;
-- name: PopProfileQueueItems :many
WITH cte AS (
SELECT id
FROM profile_queue
WHERE profile_queue.attempts <= $1
AND (
(status = 'waiting' AND (locked_until IS NULL OR locked_until < now()))
OR (status = 'processing' AND locked_until < now())
)
ORDER BY enqueued_at
LIMIT $2
FOR UPDATE SKIP LOCKED
)
UPDATE profile_queue q
SET status = 'processing',
locked_until = now() + interval '120 seconds',
attempts = attempts + 1
FROM cte
WHERE q.id = cte.id
RETURNING q.*;
-- name: PopUserQueueItems :many
WITH cte AS (
SELECT id
FROM user_queue
WHERE user_queue.attempts <= $1
AND (
(status = 'waiting' AND (locked_until IS NULL OR locked_until < now()))
OR (status = 'processing' AND locked_until < now())
)
ORDER BY enqueued_at
LIMIT $2
FOR UPDATE SKIP LOCKED
)
UPDATE user_queue q
SET status = 'processing',
locked_until = now() + interval '120 seconds',
attempts = attempts + 1
FROM cte
WHERE q.id = cte.id
RETURNING q.*;
-- name: EnqueueContentItems :copyfrom
INSERT INTO content_queue (payload)
VALUES
($1);
-- name: EnqueueProfileItems :copyfrom
INSERT INTO profile_queue (payload)
VALUES
($1);
-- name: EnqueueUserItems :copyfrom
INSERT INTO user_queue (payload)
VALUES
($1);
-- name: DeleteContentQueueItems :execrows
DELETE FROM content_queue
WHERE id = ANY(@arr::bigint[]) AND status = 'processing'; -- ensure only processing items are deleted
-- name: DeleteProfileQueueItems :execrows
DELETE FROM profile_queue
WHERE id = ANY(@arr::bigint[]) AND status = 'processing'; -- ensure only processing items are deleted
-- name: DeleteUserQueueItems :execrows
DELETE FROM user_queue
WHERE id = ANY(@arr::bigint[]) AND status = 'processing'; -- ensure only processing items are deleted