forked from ajvb/kala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres.go
135 lines (121 loc) · 3.49 KB
/
postgres.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
package postgres
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/lib/pq"
"github.com/primedata-ai/kala/job"
log "github.com/sirupsen/logrus"
)
var (
TableName = "jobs"
)
type DB struct {
conn *sql.DB
}
// New instantiates a new DB.
func New(dsn string) *DB {
connection, err := sql.Open("postgres", dsn)
if err != nil {
log.Fatal(err)
}
// passive attempt to create table
_, _ = connection.Exec(fmt.Sprintf(`create table %s (job jsonb);`, TableName))
return &DB{
conn: connection,
}
}
// GetAll returns all persisted Jobs.
func (d DB) GetAll() ([]*job.Job, error) {
query := fmt.Sprintf(`with clean as (
select ctid ,
(job -> 'metadata' -> 'last_attempted_run') as last_run, row_number() over(partition by (job->'id') order by (job -> 'metadata' -> 'last_attempted_run') desc) as num
from %s where CAST(job ->> 'is_done' as boolean) = false
)
select coalesce(json_agg(j.job), '[]'::json) from %s j where ctid in (select ctid from clean where num = 1);`, TableName, TableName)
var r sql.NullString
err := d.conn.QueryRow(query).Scan(&r)
if err != nil && err != sql.ErrNoRows {
return nil, err
}
err = nil
var jobs []*job.Job
if r.Valid {
err = json.Unmarshal([]byte(r.String), &jobs)
}
result := make([]*job.Job, 0, len(jobs))
for _, j := range jobs {
if err = j.InitDelayDuration(false); err != nil {
break
}
result = append(result, j)
}
return result, err
}
// Get returns a persisted Job.
func (d DB) Get(id string) (*job.Job, error) {
template := `select to_jsonb(j.job) from (select * from %[1]s where job -> 'id' = $1) as j;`
query := fmt.Sprintf(template, TableName)
var r sql.NullString
err := d.conn.QueryRow(query, id).Scan(&r)
if err != nil {
return nil, err
}
result := &job.Job{}
if r.Valid {
err = json.Unmarshal([]byte(r.String), &result)
}
return result, err
}
// Delete deletes a persisted Job.
func (d DB) Delete(id string) error {
query := fmt.Sprintf(`delete from %v where job -> = 'id' = $1;`, TableName)
_, err := d.conn.Exec(query, id)
return err
}
// Delete jobs state is done
func (d DB) DeleteIsDone() error {
query := fmt.Sprintf(`delete from %s where (job->'id') in (SELECT distinct (job->'id') as id FROM jobs WHERE CAST(job->'is_done' as boolean) = true);`, TableName)
_, err := d.conn.Exec(query)
return err
}
// Delete jobs duplicate
func (d DB) DeleteDuplicateJobs() error {
query := fmt.Sprintf(`with clean as (
select (job->'id') as id, ctid as ctid,
(job -> 'metadata' -> 'last_attempted_run') as last_run, row_number() over(partition by (job->'id') order by (job -> 'metadata' -> 'last_attempted_run') desc) as num
from %s where CAST(job ->> 'is_done' as boolean) = false
)
delete from %s where ctid in(select ctid from clean where num <> 1);`, TableName, TableName)
_, err := d.conn.Exec(query)
return err
}
// Save persists a Job.
func (d DB) Save(j *job.Job) error {
template := `insert into %[1]s (job) values($1);`
query := fmt.Sprintf(template, TableName)
r, err := json.Marshal(j)
if err != nil {
return err
}
transaction, err := d.conn.Begin()
if err != nil {
return err
}
statement, err := transaction.Prepare(query)
if err != nil {
transaction.Rollback() //nolint:errcheck // adding insult to injury
return err
}
defer statement.Close()
_, err = statement.Exec(string(r))
if err != nil {
transaction.Rollback() //nolint:errcheck // adding insult to injury
return err
}
return transaction.Commit()
}
// Close closes the connection to Postgres.
func (d DB) Close() error {
return d.conn.Close()
}