-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdelivery_attempts.go
155 lines (139 loc) · 5.1 KB
/
delivery_attempts.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
// apcore is a server framework for implementing an ActivityPub application.
// Copyright (C) 2020 Cory Slep
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"database/sql"
"net/url"
"time"
"github.com/go-fed/apcore/util"
)
// These constants are used to mark the simple state of the delivery attempt.
const (
newDeliveryAttempt = "new"
successDeliveryAttempt = "success"
failedDeliveryAttempt = "failed"
abandonedDeliveryAttempt = "abandoned"
)
var _ Model = &DeliveryAttempts{}
// DeliveryAttempts is a Model that provides additional database methods for
// delivery attempts.
type DeliveryAttempts struct {
insertDeliveryAttempt *sql.Stmt
markDeliveryAttemptSuccessful *sql.Stmt
markDeliveryAttemptFailed *sql.Stmt
markDeliveryAttemptAbandoned *sql.Stmt
firstRetryablePage *sql.Stmt
nextRetryablePage *sql.Stmt
}
func (d *DeliveryAttempts) Prepare(db *sql.DB, s SqlDialect) error {
return prepareStmtPairs(db,
stmtPairs{
{&(d.insertDeliveryAttempt), s.InsertAttempt()},
{&(d.markDeliveryAttemptSuccessful), s.MarkSuccessfulAttempt()},
{&(d.markDeliveryAttemptFailed), s.MarkFailedAttempt()},
{&(d.markDeliveryAttemptAbandoned), s.MarkAbandonedAttempt()},
{&(d.firstRetryablePage), s.FirstPageRetryableFailures()},
{&(d.nextRetryablePage), s.NextPageRetryableFailures()},
})
}
func (d *DeliveryAttempts) CreateTable(t *sql.Tx, s SqlDialect) error {
_, err := t.Exec(s.CreateDeliveryAttemptsTable())
return err
}
func (d *DeliveryAttempts) Close() {
d.insertDeliveryAttempt.Close()
d.markDeliveryAttemptSuccessful.Close()
d.markDeliveryAttemptFailed.Close()
}
// Create a new delivery attempt.
func (d *DeliveryAttempts) Create(c util.Context, tx *sql.Tx, from string, toActor *url.URL, payload []byte) (id string, err error) {
var rows *sql.Rows
rows, err = tx.Stmt(d.insertDeliveryAttempt).QueryContext(c,
from,
toActor.String(),
payload,
newDeliveryAttempt)
if err != nil {
return
}
defer rows.Close()
return id, enforceOneRow(rows, "DeliveryAttempts.Create", func(r SingleRow) error {
return r.Scan(&(id))
})
}
// MarkSuccessful marks a delivery attempt as successful.
func (d *DeliveryAttempts) MarkSuccessful(c util.Context, tx *sql.Tx, id string) error {
r, err := tx.Stmt(d.markDeliveryAttemptSuccessful).ExecContext(c,
id,
successDeliveryAttempt)
return mustChangeOneRow(r, err, "DeliveryAttempts.MarkSuccessful")
}
// MarkFailed marks a delivery attempt as failed.
func (d *DeliveryAttempts) MarkFailed(c util.Context, tx *sql.Tx, id string) error {
r, err := tx.Stmt(d.markDeliveryAttemptFailed).ExecContext(c,
id,
failedDeliveryAttempt)
return mustChangeOneRow(r, err, "DeliveryAttempts.MarkFailed")
}
// MarkAbandoned marks a delivery attempt as abandoned.
func (d *DeliveryAttempts) MarkAbandoned(c util.Context, tx *sql.Tx, id string) error {
r, err := tx.Stmt(d.markDeliveryAttemptAbandoned).ExecContext(c,
id,
abandonedDeliveryAttempt)
return mustChangeOneRow(r, err, "DeliveryAttempts.Abandoned")
}
type RetryableFailure struct {
ID string
UserID string
DeliverTo URL
Payload []byte
NAttempts int
LastAttempt time.Time
}
// FirstPageFailures obtains the first page of retryable failures.
func (d *DeliveryAttempts) FirstPageFailures(c util.Context, tx *sql.Tx, fetchTime time.Time, n int) (rf []RetryableFailure, err error) {
var rows *sql.Rows
rows, err = tx.Stmt(d.firstRetryablePage).QueryContext(c, failedDeliveryAttempt, fetchTime, n)
if err != nil {
return
}
defer rows.Close()
return rf, doForRows(rows, "DeliveryAttempts.FirstPageFailures", func(r SingleRow) error {
var rt RetryableFailure
if err := r.Scan(&(rt.ID), &(rt.UserID), &(rt.DeliverTo), &(rt.Payload), &(rt.NAttempts), &(rt.LastAttempt)); err != nil {
return err
}
rf = append(rf, rt)
return nil
})
}
// NextPageFailures obtains the next page of retryable failures.
func (d *DeliveryAttempts) NextPageFailures(c util.Context, tx *sql.Tx, prevID string, fetchTime time.Time, n int) (rf []RetryableFailure, err error) {
var rows *sql.Rows
rows, err = tx.Stmt(d.nextRetryablePage).QueryContext(c, failedDeliveryAttempt, fetchTime, n, prevID)
if err != nil {
return
}
defer rows.Close()
return rf, doForRows(rows, "DeliveryAttempts.NextPageFailures", func(r SingleRow) error {
var rt RetryableFailure
if err := r.Scan(&(rt.ID), &(rt.UserID), &(rt.DeliverTo), &(rt.Payload), &(rt.NAttempts), &(rt.LastAttempt)); err != nil {
return err
}
rf = append(rf, rt)
return nil
})
}