-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
199 lines (174 loc) · 4.38 KB
/
repository.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package pgx
import (
"context"
a "github.com/dalthon/ana"
pgx "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
var fetchOrStartQuery string = `
SELECT
status,
key,
target,
payload,
reference_time,
started_at,
timeout,
expiration,
result,
error_message
FROM ana.fetch_or_start(
@key,
@target,
@payload,
@reference_time,
@timeout,
@expiration
);
`
var lockTrackOperationQuery string = `
SELECT *
FROM ana.tracked_operations
WHERE
key = @key AND
target = @target AND
reference_time = @reference_time
FOR UPDATE;
`
var failTimedOutStillRunningQuery string = `
UPDATE ana.tracked_operations AS operation
SET
status = 'failed',
finished_at = NOW(),
error_count = error_count + 1,
error_message = 'Operation timed out'
FROM (
SELECT key, target
FROM ana.tracked_operations AS t
WHERE
t.status = 'running' AND
t.timeout < NOW()
ORDER BY t.timeout ASC
LIMIT @count
FOR UPDATE SKIP LOCKED
) AS expired
WHERE operation.key = expired.key AND operation.target = expired.target;
`
var failExpiredStillRunningQuery string = `
UPDATE ana.tracked_operations AS operation
SET
status = 'failed',
finished_at = NOW(),
error_count = error_count + 1,
error_message = 'Operation expired'
FROM (
SELECT key, target
FROM ana.tracked_operations AS t
WHERE
t.status = 'running' AND
t.expiration < NOW()
ORDER BY t.expiration ASC
LIMIT @count
FOR UPDATE SKIP LOCKED
) AS expired
WHERE operation.key = expired.key AND operation.target = expired.target;
`
var deleteExpiredQuery string = `
DELETE FROM ana.tracked_operations AS operation
USING (
SELECT key, target
FROM ana.tracked_operations AS t
WHERE
t.status = @status AND
t.expiration < NOW()
ORDER BY t.expiration ASC
LIMIT @count
FOR UPDATE SKIP LOCKED
) AS expired
WHERE operation.key = expired.key AND operation.target = expired.target;
`
type PgxRepository[P any, R any] struct {
pool *pgxpool.Pool
}
func NewPgxRepository[P any, R any](pool *pgxpool.Pool) *PgxRepository[P, R] {
return &PgxRepository[P, R]{pool: pool}
}
func (repo *PgxRepository[P, R]) FetchOrStart(operation a.Operation[P, R, *PgxContext[P, R]]) *a.TrackedOperation[P, R] {
rows, err := repo.pool.Query(
context.Background(),
fetchOrStartQuery,
pgx.NamedArgs{
"key": operation.Key(),
"target": operation.Target(),
"payload": serialize(operation.Payload()),
"reference_time": operation.ReferenceTime(),
"timeout": operation.Timeout(),
"expiration": operation.Expiration(),
},
)
if err != nil {
panic(err)
}
return rowsToTrackedOperation[P, R](rows)
}
func (repo *PgxRepository[P, R]) NewSession(operation a.Operation[P, R, *PgxContext[P, R]]) *a.Session[P, R, *PgxContext[P, R]] {
context := context.Background()
outerTx, _ := repo.pool.Begin(context)
tx, _ := outerTx.Begin(context)
tx.Exec(context, lockTrackOperationQuery, pgx.NamedArgs{
"key": operation.Key(),
"target": operation.Target(),
"reference_time": operation.ReferenceTime(),
})
return a.NewSession(operation, NewPgxContext[P, R](outerTx, tx, context))
}
func (repo *PgxRepository[P, R]) FailTimedOutStillRunning(count int) int64 {
info, err := repo.pool.Exec(
context.Background(),
failTimedOutStillRunningQuery,
pgx.NamedArgs{"count": count},
)
if err != nil {
panic(err)
}
return info.RowsAffected()
}
func (repo *PgxRepository[P, R]) FailExpiredStillRunning(count int) int64 {
info, err := repo.pool.Exec(
context.Background(),
failExpiredStillRunningQuery,
pgx.NamedArgs{"count": count},
)
if err != nil {
panic(err)
}
return info.RowsAffected()
}
func (repo *PgxRepository[P, R]) DeleteExpired(status a.TrackedOperationStatus, count int) int64 {
info, err := repo.pool.Exec(
context.Background(),
deleteExpiredQuery,
pgx.NamedArgs{
"status": trackedStatusToPgStatus(status),
"count": count,
},
)
if err != nil {
panic(err)
}
return info.RowsAffected()
}
func trackedStatusToPgStatus(status a.TrackedOperationStatus) string {
switch status {
case a.Ready:
return "ready"
case a.Running:
return "running"
case a.Finished:
return "finished"
case a.Failed:
return "failed"
default:
panic("Dead code")
}
}