-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
162 lines (146 loc) · 2.64 KB
/
db.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
package jobq
import (
"database/sql"
"database/sql/driver"
"fmt"
"time"
_ "github.com/dbarzdys/jobq/migrate/migrations"
)
const nullTimeLayout = "2006-01-02T15:04:05.999999999"
type nullTime struct {
Time time.Time
Valid bool
}
func (nt *nullTime) UnmarshalJSON(b []byte) error {
if string(b) == "null" {
nt.Valid = false
return nil
}
str := string(b)
str = str[1 : len(str)-1]
t, err := time.Parse(nullTimeLayout, str)
if err != nil {
return err
}
nt.Time = t
nt.Valid = true
return nil
}
func (nt nullTime) MarshalJSON() ([]byte, error) {
if !nt.Valid {
return []byte("null"), nil
}
str := nt.Time.Format(nullTimeLayout)
str = fmt.Sprintf("\"%s\"", str)
return []byte(str), nil
}
func (nt *nullTime) Scan(value interface{}) error {
nt.Time, nt.Valid = value.(time.Time)
return nil
}
func (nt nullTime) Value() (driver.Value, error) {
if !nt.Valid {
return nil, nil
}
return nt.Time, nil
}
// DBExecer makes execs
type DBExecer interface {
Exec(query string, args ...interface{}) (sql.Result, error)
}
// DBQueryer makes queries
type DBQueryer interface {
Query(query string, args ...interface{}) (*sql.Rows, error)
}
type DB interface {
DBExecer
DBQueryer
Begin() (*sql.Tx, error)
}
type Tx interface {
DBExecer
DBQueryer
Commit() error
Rollback() error
}
func queueTask(e DBExecer, row *TaskRow) error {
stmt := `
INSERT INTO jobq_tasks (
uid,
job_name,
body,
retries,
timeout,
start_at
) VALUES ($1, $2, $3, $4, $5, $6);
`
_, err := e.Exec(
stmt,
row.uid,
row.jobName,
row.body,
row.retries,
row.timeout,
row.startAt,
)
return err
}
func requeueTask(e DBExecer, row *TaskRow) error {
stmt := `
INSERT INTO jobq_tasks (
id,
uid,
job_name,
body,
retries,
timeout,
start_at
) VALUES ($1, $2, $3, $4, $5, $6, $7);
`
_, err := e.Exec(
stmt,
row.id,
row.uid,
row.jobName,
row.body,
row.retries,
row.timeout,
row.startAt,
)
return err
}
func dequeueTask(e DBQueryer, name string) (*TaskRow, error) {
row := new(TaskRow)
stmt := `
DELETE FROM jobq_tasks WHERE id = (
SELECT id FROM jobq_tasks
WHERE job_name = $1
AND (timeout IS NULL OR timeout < NOW())
AND (start_at IS NULL OR start_at < NOW())
ORDER BY id ASC
FOR UPDATE SKIP LOCKED
LIMIT 1
) RETURNING id, uid, body, retries, timeout, start_at;
`
rows, err := e.Query(stmt, name)
if err != nil {
return nil, err
}
if !rows.Next() {
return nil, sql.ErrNoRows
}
defer rows.Close()
err = rows.Scan(
&row.id,
&row.uid,
&row.body,
&row.retries,
&row.timeout,
&row.startAt,
)
if err != nil {
return nil, err
}
row.jobName = name
return row, nil
}