-
Notifications
You must be signed in to change notification settings - Fork 25
/
pgqueue.go
843 lines (731 loc) · 23.3 KB
/
pgqueue.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
package queue
import (
"container/list"
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/content-services/content-sources-backend/pkg/config"
ce "github.com/content-services/content-sources-backend/pkg/errors"
"github.com/content-services/content-sources-backend/pkg/models"
"github.com/google/uuid"
pgxzero "github.com/jackc/pgx-zerolog"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/tracelog"
"github.com/lib/pq"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const taskInfoReturning = ` id, type, payload, queued_at, started_at, finished_at, status, error, org_id, repository_uuid, token, request_id, retries, next_retry_time ` // fields to return when returning taskInfo
const (
sqlNotify = `NOTIFY tasks`
sqlListen = `LISTEN tasks`
sqlUnlisten = `UNLISTEN tasks`
sqlEnqueue = `INSERT INTO tasks(id, type, payload, queued_at, org_id, repository_uuid, status, request_id, account_id) VALUES ($1, $2, $3, statement_timestamp(), $4, $5, $6, $7, $8)`
sqlDequeue = `
UPDATE tasks
SET token = $1, started_at = statement_timestamp(), status = 'running'
WHERE id = (
SELECT id
FROM ready_tasks
-- use ANY here, because "type in ()" doesn't work with bound parameters
-- literal syntax for this is '{"a", "b"}': https://www.postgresql.org/docs/13/arrays.html
WHERE type = ANY($2)
LIMIT 1
FOR UPDATE SKIP LOCKED
)
RETURNING ` + taskInfoReturning
//nolint:unused,deadcode,varcheck
sqlDequeueByID = `
UPDATE tasks
SET token = $1, started_at = statement_timestamp()
WHERE id = (
SELECT id
FROM ready_tasks
WHERE id = $2
LIMIT 1
FOR UPDATE SKIP LOCKED
)
RETURNING token, type, payload, queued_at, started_at`
sqlRequeue = `
UPDATE tasks
SET started_at = NULL, token = NULL, status = 'pending', retries = retries + 1, queued_at = statement_timestamp()
WHERE id = $1 AND started_at IS NOT NULL AND finished_at IS NULL`
sqlRequeueFailedTasks = `
UPDATE tasks
SET started_at = NULL, finished_at = NULL, token = NULL, status = 'pending', retries = retries + 1, queued_at = statement_timestamp()
WHERE started_at IS NOT NULL AND finished_at IS NOT NULL AND status = 'failed' AND retries < 3 AND next_retry_time <= statement_timestamp() AND type = ANY($1::text[])`
sqlInsertDependency = `INSERT INTO task_dependencies VALUES ($1, $2)`
sqlQueryDependencies = `
SELECT dependency_id
FROM task_dependencies
WHERE task_id = $1`
sqlQueryDependents = `
SELECT task_id
FROM task_dependencies
WHERE dependency_id = $1`
//nolint:unused,deadcode,varcheck
sqlQueryTask = `
SELECT type, payload, repository_uuid, org_id, queued_at, started_at, finished_at, status, error
FROM tasks
WHERE id = $1`
sqlQueryTaskStatus = `
SELECT ` + taskInfoReturning +
` FROM tasks
WHERE id = $1`
sqlQueryIdFromToken = `
SELECT id, status FROM tasks WHERE token = $1`
sqlFinishTask = `
UPDATE tasks
SET finished_at = statement_timestamp(), status = $1, error = (left($2, 4000)), next_retry_time = $3
WHERE id = $4 AND finished_at IS NULL
RETURNING finished_at`
sqlCancelTask = `
UPDATE tasks
SET status = 'canceled'
WHERE id = $1 AND finished_at IS NULL
RETURNING type, started_at`
// sqlUpdatePayload
sqlUpdatePayload = `
UPDATE tasks
SET payload = $1
WHERE id = $2`
sqlInsertHeartbeat = `
INSERT INTO task_heartbeats(token, id, heartbeat)
VALUES ($1, $2, statement_timestamp())`
sqlQueryHeartbeats = `
SELECT token
FROM task_heartbeats
WHERE age(statement_timestamp(), heartbeat) > $1`
sqlRefreshHeartbeat = `
UPDATE task_heartbeats
SET heartbeat = statement_timestamp()
WHERE token = $1`
sqlDeleteHeartbeat = `
DELETE FROM task_heartbeats
WHERE id = $1`
sqlDeleteAllTasks = `
TRUNCATE tasks, task_heartbeats, task_dependencies`
)
// These interfaces represent all the interactions with pgxpool that are needed for the pgqueue
// They do not implement the exact interface as their pgx(pool) equivalents, as we need to
// return instances of our own interfaces sometimes (For example, Pool.Acquire should return
// our Transaction interface instead of pgxpool.Trans)
// Pool matches the pgxpool.Pool struct
type Pool interface {
Transaction
Acquire(ctx context.Context) (Connection, error)
}
// Transaction mimics the pgx.Tx struct
type Transaction interface {
Begin(ctx context.Context) (pgx.Tx, error)
Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
}
type Connection interface {
Transaction
Conn() *pgx.Conn
Release()
}
// PgQueue a task queue backed by postgres, using pgxpool.Pool using a wrapper (PgxPoolWrapper) that implements a Pool interface
type PgQueue struct {
Pool Pool
dequeuers *dequeuers
stopListener func()
}
// thread-safe list of dequeuers
type dequeuers struct {
list *list.List
mutex sync.Mutex
}
func newDequeuers() *dequeuers {
return &dequeuers{
list: list.New(),
}
}
func (d *dequeuers) pushBack(c chan struct{}) *list.Element {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.list.PushBack(c)
}
func (d *dequeuers) remove(e *list.Element) {
d.mutex.Lock()
defer d.mutex.Unlock()
d.list.Remove(e)
}
func (d *dequeuers) notifyAll() {
d.mutex.Lock()
defer d.mutex.Unlock()
cur := d.list.Front()
for cur != nil {
listenerChan, _ := cur.Value.(chan struct{})
// notify in a non-blocking way
select {
case listenerChan <- struct{}{}:
default:
}
cur = cur.Next()
}
}
func NewPgxPool(url string) (*pgxpool.Pool, error) {
pxConfig, err := pgxpool.ParseConfig(url)
pxConfig.MaxConns = int32(config.Get().Database.PoolLimit)
if err != nil {
return nil, err
}
if config.Get().Tasking.PGXLogging {
level, err := tracelog.LogLevelFromString(config.Get().Logging.Level)
if err != nil {
log.Logger.Error().Err(err).Msg("could not set log level for pgx logging, defaulting to DEBUG")
level = tracelog.LogLevelDebug
}
pxConfig.ConnConfig.Tracer = &tracelog.TraceLog{
Logger: pgxzero.NewLogger(log.Logger),
LogLevel: level,
}
if err != nil {
log.Error().Err(err).Msg("Error setting Pgx log level")
}
}
pool, err := pgxpool.NewWithConfig(context.Background(), pxConfig)
if err != nil {
return nil, fmt.Errorf("error establishing connection: %w", err)
}
return pool, nil
}
func NewPgQueue(url string) (PgQueue, error) {
var poolWrapper Pool
pool, err := NewPgxPool(url)
if err != nil {
return PgQueue{}, fmt.Errorf("error establishing connection: %w", err)
}
listenContext, cancel := context.WithCancel(context.Background())
poolWrapper = &PgxPoolWrapper{pool: pool}
q := PgQueue{
Pool: poolWrapper,
dequeuers: newDequeuers(),
stopListener: cancel,
}
listenerReady := make(chan struct{})
go q.listen(listenContext, listenerReady)
// wait for the listener to become ready
<-listenerReady
return q, nil
}
func (q *PgQueue) listen(ctx context.Context, ready chan<- struct{}) {
ready <- struct{}{}
for {
err := q.waitAndNotify(ctx)
if err != nil {
// shutdown the listener if the context is canceled
if errors.Is(err, context.Canceled) {
log.Logger.Info().Msg("Shutting down the listener")
return
}
// otherwise, just log the error and continue, there might just
// be a temporary networking issue
log.Logger.Error().Err(err).Msg("Error waiting for notification on tasks channel")
// backoff to avoid log spam
time.Sleep(time.Millisecond * 500)
}
}
}
func (q *PgQueue) waitAndNotify(ctx context.Context) error {
conn, err := q.Pool.Acquire(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
return err
}
panic(fmt.Errorf("error connecting to database: %v", err))
}
defer func() {
// use the empty context as the listening context is already cancelled at this point
_, err := conn.Exec(context.Background(), sqlUnlisten)
if err != nil && !errors.Is(err, context.DeadlineExceeded) {
log.Logger.Error().Err(err).Msg("Error unlistening for tasks in dequeue")
}
conn.Release()
}()
_, err = conn.Exec(ctx, sqlListen)
if err != nil {
if errors.Is(err, context.Canceled) {
return err
}
panic(fmt.Errorf("error listening on tasks channel: %v", err))
}
_, err = conn.Conn().WaitForNotification(ctx)
if err != nil {
return err
}
// something happened in the database, notify all dequeuers
q.dequeuers.notifyAll()
return nil
}
func (p *PgQueue) Enqueue(task *Task) (uuid.UUID, error) {
taskID := uuid.New()
conn, err := p.Pool.Acquire(context.Background())
if err != nil {
return uuid.Nil, err
}
defer conn.Release()
tx, err := conn.Begin(context.Background())
if err != nil {
return uuid.Nil, fmt.Errorf("error starting database transaction: %w", err)
}
defer func() {
errRollback := tx.Rollback(context.Background())
if errRollback != nil && !errors.Is(errRollback, pgx.ErrTxClosed) {
err = fmt.Errorf("error rolling back enqueue transaction: %w: %v", errRollback, err)
}
}()
_, err = tx.Exec(context.Background(), sqlEnqueue,
taskID.String(), task.Typename, task.Payload, task.OrgId, task.RepositoryUUID,
config.TaskStatusPending, task.RequestID, task.AccountId)
if err != nil {
return uuid.Nil, fmt.Errorf("error enqueuing task: %w", err)
}
for _, d := range task.Dependencies {
_, err = tx.Exec(context.Background(), sqlInsertDependency, taskID, d)
if err != nil {
return uuid.Nil, fmt.Errorf("error inserting dependency: %w", err)
}
}
_, err = conn.Exec(context.Background(), sqlNotify)
if err != nil {
return uuid.Nil, fmt.Errorf("error notifying tasks channel: %w", err)
}
err = tx.Commit(context.Background())
if err != nil {
return uuid.Nil, fmt.Errorf("unable to commit database transaction: %w", err)
}
return taskID, nil
}
func (p *PgQueue) Dequeue(ctx context.Context, taskTypes []string) (*models.TaskInfo, error) {
// add ourselves as a dequeuer
c := make(chan struct{}, 1)
el := p.dequeuers.pushBack(c)
defer p.dequeuers.remove(el)
var info *models.TaskInfo
var err error
token := uuid.New()
for {
info, err = p.dequeueMaybe(ctx, token, taskTypes)
if err == nil {
break
}
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
return nil, ErrContextCanceled
}
return nil, fmt.Errorf("error dequeuing task: %v", err)
}
// no suitable task was found, wait for the next queue update
select {
case <-c:
case <-ctx.Done():
return nil, ErrContextCanceled
}
}
return info, nil
}
// dequeueMaybe is just a smaller helper for acquiring a connection and
// running the sqlDequeue query
func (p *PgQueue) dequeueMaybe(ctx context.Context, token uuid.UUID, taskTypes []string) (info *models.TaskInfo, err error) {
info = &models.TaskInfo{}
tx, err := p.Pool.Begin(ctx)
if err != nil {
return nil, fmt.Errorf("error starting a new transaction when dequeueing: %w", err)
}
defer func() {
errRollback := tx.Rollback(context.Background())
if errRollback != nil && !errors.Is(errRollback, pgx.ErrTxClosed) {
err = fmt.Errorf("error rolling back dequeue transaction: %w: %v", errRollback, err)
}
}()
err = tx.QueryRow(ctx, sqlDequeue, token, taskTypes).Scan(
&info.Id, &info.Typename, &info.Payload, &info.Queued, &info.Started, &info.Finished, &info.Status,
&info.Error, &info.OrgId, &info.RepositoryUUID, &info.Token, &info.RequestID, &info.Retries, &info.NextRetryTime,
)
if err != nil {
return nil, fmt.Errorf("error during dequeue query: %w", err)
}
// insert heartbeat
_, err = tx.Exec(ctx, sqlInsertHeartbeat, token, info.Id)
if err != nil {
return nil, fmt.Errorf("error inserting the task's heartbeat: %w", err)
}
dependencies, err := p.taskDependencies(ctx, tx, info.Id)
if err != nil {
return nil, fmt.Errorf("error querying the task's dependencies: %w", err)
}
info.Dependencies = dependencies
err = tx.Commit(ctx)
if err != nil {
return nil, fmt.Errorf("error committing the transaction for dequeueing task %s: %w", info.Id.String(), err)
}
return info, nil
}
func (p *PgQueue) UpdatePayload(task *models.TaskInfo, payload interface{}) (*models.TaskInfo, error) {
var err error
_, err = p.Pool.Exec(context.Background(), sqlUpdatePayload, payload, task.Id.String())
return task, err
}
func (p *PgQueue) taskDependencies(ctx context.Context, tx Transaction, id uuid.UUID) ([]uuid.UUID, error) {
rows, err := tx.Query(ctx, sqlQueryDependencies, id)
if err != nil {
return nil, err
}
defer rows.Close()
dependencies := []uuid.UUID{}
for rows.Next() {
var d uuid.UUID
err = rows.Scan(&d)
if err != nil {
return nil, err
}
dependencies = append(dependencies, d)
}
if rows.Err() != nil {
return nil, err
}
return dependencies, nil
}
//nolint:unused
func (p *PgQueue) taskDependents(ctx context.Context, conn Connection, id uuid.UUID) ([]uuid.UUID, error) {
rows, err := conn.Query(ctx, sqlQueryDependents, id)
if err != nil {
return nil, err
}
defer rows.Close()
dependents := []uuid.UUID{}
for rows.Next() {
var d uuid.UUID
err = rows.Scan(&d)
if err != nil {
return nil, err
}
dependents = append(dependents, d)
}
if rows.Err() != nil {
return nil, err
}
return dependents, nil
}
func (p *PgQueue) Status(taskId uuid.UUID) (*models.TaskInfo, error) {
var err error
// Use double pointers for timestamps because they might be NULL, which would result in *time.Time == nil
var info models.TaskInfo
conn, err := p.Pool.Acquire(context.Background())
if err != nil {
return nil, err
}
defer conn.Release()
err = conn.QueryRow(context.Background(), sqlQueryTaskStatus, taskId).Scan(
&info.Id, &info.Typename, &info.Payload, &info.Queued, &info.Started, &info.Finished, &info.Status,
&info.Error, &info.OrgId, &info.RepositoryUUID, &info.Token, &info.RequestID, &info.Retries, &info.NextRetryTime,
)
if err != nil {
return nil, err
}
deps, err := p.taskDependencies(context.Background(), conn, taskId)
if err != nil {
return nil, err
}
info.Dependencies = deps
if err != nil {
return nil, err
}
return &info, nil
}
func (p *PgQueue) Finish(taskId uuid.UUID, taskError error) error {
var err error
var status string
var errMsg *string
if taskError != nil {
if errors.Is(taskError, context.Canceled) {
status = config.TaskStatusCanceled
} else {
status = config.TaskStatusFailed
}
s := strings.ToValidUTF8(taskError.Error(), "")
errMsg = &s
} else {
status = config.TaskStatusCompleted
errMsg = nil
}
tx, err := p.Pool.Begin(context.Background())
if err != nil {
return fmt.Errorf("error starting database transaction: %v", err)
}
defer func() {
err = tx.Rollback(context.Background())
if err != nil && !errors.Is(err, pgx.ErrTxClosed) {
log.Logger.Error().Err(err).Msg(fmt.Sprintf("Error rolling back finish task transaction for task %v", taskId.String()))
}
}()
info, err := p.Status(taskId)
if err != nil {
return err
}
if info.Started == nil || info.Finished != nil {
return ErrNotRunning
}
var nextRetryTime *time.Time
if status == config.TaskStatusFailed && info.Retries < MaxTaskRetries {
upperBound := config.Get().Tasking.RetryWaitUpperBound
retriesRemaining := float64(MaxTaskRetries - info.Retries)
timeToWait := time.Second * time.Duration(upperBound.Seconds()/(retriesRemaining+1))
add := time.Now().Add(timeToWait)
nextRetryTime = &add
}
// Remove from heartbeats
tag, err := tx.Exec(context.Background(), sqlDeleteHeartbeat, taskId)
if err != nil {
return fmt.Errorf("error removing task %s from heartbeats: %v", taskId, err)
}
if tag.RowsAffected() != 1 {
return ErrNotExist
}
err = tx.QueryRow(context.Background(), sqlFinishTask, status, errMsg, nextRetryTime, taskId).Scan(&info.Finished)
if err == pgx.ErrNoRows {
return ErrNotExist
}
if err != nil {
return fmt.Errorf("error finishing task %s: %v", taskId, err)
}
_, err = tx.Exec(context.Background(), sqlNotify)
if err != nil {
return fmt.Errorf("error notifying tasks channel: %v", err)
}
err = tx.Commit(context.Background())
if err != nil {
return fmt.Errorf("unable to commit database transaction: %v", err)
}
return nil
}
func (p *PgQueue) SendCancelNotification(ctx context.Context, taskId uuid.UUID) error {
conn, err := p.Pool.Acquire(ctx)
if err != nil {
return err
}
defer conn.Release()
channelName := getCancelChannelName(taskId)
_, err = conn.Exec(ctx, "select pg_notify($1, 'cancel')", channelName)
if err != nil {
return err
}
return nil
}
func (p *PgQueue) Requeue(taskId uuid.UUID) error {
var err error
tx, err := p.Pool.Begin(context.Background())
if err != nil {
return fmt.Errorf("error starting database transaction: %v", err)
}
defer func() {
err = tx.Rollback(context.Background())
if err != nil && !errors.Is(err, pgx.ErrTxClosed) {
log.Logger.Error().Err(err).Msg(fmt.Sprintf("Error rolling back retry task transaction. Task id %v", taskId.String()))
}
}()
// Use double pointers for timestamps because they might be NULL, which would result in *time.Time == nil
info, err := p.Status(taskId)
if err == pgx.ErrNoRows {
return ErrNotExist
}
if info.Started == nil || info.Finished != nil {
return ErrNotRunning
}
if info.Retries == MaxTaskRetries {
err = p.Finish(info.Id, ErrMaxRetriesExceeded)
if err != nil {
return fmt.Errorf("error finishing task")
}
err = tx.Commit(context.Background())
if err != nil {
return fmt.Errorf("unable to commit database transaction: %v", err)
}
return ErrMaxRetriesExceeded
}
// Remove from heartbeats
tag, err := tx.Exec(context.Background(), sqlDeleteHeartbeat, taskId)
if err != nil {
return fmt.Errorf("error removing task %s from heartbeats: %v", taskId, err)
}
if tag.RowsAffected() != 1 {
return ErrNotExist
}
tag, err = tx.Exec(context.Background(), sqlRequeue, taskId)
if err != nil {
return fmt.Errorf("error requeueing task %s: %v", taskId, err)
}
if tag.RowsAffected() != 1 {
return ErrNotExist
}
_, err = tx.Exec(context.Background(), sqlNotify)
if err != nil {
return fmt.Errorf("error notifying tasks channel: %v", err)
}
err = tx.Commit(context.Background())
if err != nil {
return fmt.Errorf("unable to commit database transaction: %v", err)
}
return nil
}
func (p *PgQueue) RequeueFailedTasks(taskTypes []string) error {
var err error
tx, err := p.Pool.Begin(context.Background())
if err != nil {
return fmt.Errorf("error starting database transaction: %v", err)
}
defer func() {
err = tx.Rollback(context.Background())
if err != nil && !errors.Is(err, pgx.ErrTxClosed) {
log.Logger.Error().Err(err).Msg("Error rolling back retry failed tasks transaction")
}
}()
tag, err := tx.Exec(context.Background(), sqlRequeueFailedTasks, pq.Array(taskTypes))
if err != nil {
return fmt.Errorf("error requeueing failed tasks: %w", err)
}
if tag.RowsAffected() != 0 {
log.Logger.Info().Msg("Failed tasks being requeued")
}
_, err = tx.Exec(context.Background(), sqlNotify)
if err != nil {
return fmt.Errorf("error notifying tasks channel: %v", err)
}
err = tx.Commit(context.Background())
if err != nil {
return fmt.Errorf("unable to commit database transaction: %v", err)
}
return nil
}
func (p *PgQueue) Heartbeats(olderThan time.Duration) []uuid.UUID {
var err error
rows, err := p.Pool.Query(context.Background(), sqlQueryHeartbeats, olderThan)
if err != nil {
return nil
}
defer rows.Close()
var tokens []uuid.UUID
for rows.Next() {
var t uuid.UUID
err = rows.Scan(&t)
if err != nil {
// Log the error and try to continue with the next row
log.Logger.Error().Err(err).Msg("Unable to read token from heartbeats")
continue
}
tokens = append(tokens, t)
}
if rows.Err() != nil {
log.Logger.Error().Err(rows.Err()).Msg("Error reading tokens from heartbeats")
}
return tokens
}
// Reset the last heartbeat time to time.Now()
func (p *PgQueue) RefreshHeartbeat(token uuid.UUID) error {
var err error
if token == uuid.Nil {
return nil
}
tag, err := p.Pool.Exec(context.Background(), sqlRefreshHeartbeat, token)
if err != nil {
return err
}
if tag.RowsAffected() != 1 {
_, isRunning, err := p.IdFromToken(token)
if err != nil {
return err
}
if isRunning {
tag, err := p.Pool.Exec(context.Background(), sqlRefreshHeartbeat, token)
if err != nil {
return err
}
if tag.RowsAffected() != 1 {
return ErrRowsNotAffected
}
}
}
return nil
}
func (p *PgQueue) IdFromToken(token uuid.UUID) (id uuid.UUID, isRunning bool, err error) {
var status string
conn, err := p.Pool.Acquire(context.Background())
if err != nil {
return uuid.Nil, false, err
}
defer conn.Release()
row := conn.QueryRow(context.Background(), sqlQueryIdFromToken, token)
err = row.Scan(&id, &status)
isRunning = status == config.TaskStatusRunning
if err == pgx.ErrNoRows {
return uuid.Nil, isRunning, ErrNotExist
} else if err != nil {
return uuid.Nil, isRunning, fmt.Errorf("Error retrieving id: %v", err)
}
return
}
// RemoveAllTasks used for tests, along with testTx, to clear tables before running tests
func (p *PgQueue) RemoveAllTasks() error {
_, err := p.Pool.Exec(context.Background(), sqlDeleteAllTasks)
if err != nil {
return fmt.Errorf("error removing all tasks")
}
return nil
}
func (p *PgQueue) ListenForCancel(ctx context.Context, taskID uuid.UUID, cancelFunc context.CancelCauseFunc) {
logger := zerolog.Ctx(ctx)
conn, err := p.Pool.Acquire(ctx)
if err != nil {
// If the task is finished before listen is initiated, a context canceled error is expected
if !errors.Is(ErrNotRunning, context.Cause(ctx)) {
logger.Error().Err(err).Msg("ListenForCancel: error acquiring connection")
}
return
}
defer conn.Release()
// Register a channel for the task where a notification can be sent to cancel the task
channelName := getCancelChannelName(taskID)
// TODO remove debug logs. checking if channel register is hanging
logger.Debug().Msg("ListenForCancel: preparing register channel")
_, err = conn.Conn().Exec(ctx, "listen "+channelName)
if err != nil {
if !errors.Is(ErrNotRunning, context.Cause(ctx)) {
logger.Error().Err(err).Msg("ListenForCancel: error registering channel")
}
return
}
logger.Debug().Msg("ListenForCancel: finished register channel")
// When the function returns, unregister the channel
defer func(conn *pgx.Conn) {
// TODO Go 1.21 can replace context.Background() with context.WithoutCancel()
_, err = conn.Exec(context.Background(), "unlisten "+channelName)
if err != nil {
logger.Error().Err(err).Msg("ListenForCancel: error unregistering listener")
}
}(conn.Conn())
// Wait for a notification on the channel. This blocks until the channel receives a notification.
_, err = conn.Conn().WaitForNotification(ctx)
if err != nil {
if !errors.Is(ErrNotRunning, context.Cause(ctx)) && !errors.Is(ce.ErrServerExited, context.Cause(ctx)) {
logger.Error().Err(err).Msg("ListenForCancel: error waiting for notification")
}
return
}
// Cancel context only if context has not already been canceled. If the context has already been canceled, the task has finished.
if !errors.Is(ctx.Err(), context.Canceled) {
logger.Debug().Msg("[Canceled Task]")
cancelFunc(ErrTaskCanceled)
}
}
func getCancelChannelName(taskID uuid.UUID) string {
return strings.Replace("task_"+taskID.String(), "-", "", -1)
}