-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
188 lines (147 loc) · 3.73 KB
/
client.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
package archer
import (
"bytes"
"context"
"database/sql"
"log/slog"
"sync"
"time"
"github.com/dyaksa/archer/job"
"github.com/dyaksa/archer/store"
"golang.org/x/sync/errgroup"
_ "github.com/lib/pq"
)
type Options struct {
Addr string
User string
Password string
SSL string
DBName string
MaxIdleConns int
MaxOpenConns int
}
type Client struct {
wrapper wrapperTx
tx func(*sql.Tx) Tx
register
spawn Spawner
mutate *Mutate
errChan chan error
errHandler func(error)
shutdown func()
sleepInterval time.Duration
reaperInterval time.Duration
queue func(name string) *Queue
coRoutines []func() error
}
type wrapperTx interface {
WrapTx(ctx context.Context, fn func(ctx context.Context, tx *sql.Tx) (any, error)) (any, error)
}
func NewClient(opt *Options, options ...ClientOptionFunc) *Client {
dsn := bytes.Buffer{}
dsn.WriteString("postgres://")
dsn.WriteString(opt.User)
dsn.WriteString(":")
dsn.WriteString(opt.Password)
dsn.WriteString("@")
dsn.WriteString(opt.Addr)
dsn.WriteString("/")
dsn.WriteString(opt.DBName)
dsn.WriteString("?sslmode=disable")
db, err := sql.Open("postgres", dsn.String())
if err != nil {
panic(err)
}
db.SetMaxIdleConns(opt.MaxIdleConns)
db.SetMaxOpenConns(opt.MaxOpenConns)
errChan := make(chan error)
ctx, cancel := context.WithCancel(context.Background())
c := &Client{
register: newRegister(),
wrapper: store.NewWrapperTx(db),
spawn: newSpawner(ctx, errChan),
errChan: errChan,
shutdown: cancel,
tx: func(tx *sql.Tx) Tx {
return newTx(tx)
},
queue: func(name string) *Queue {
return NewQueue(db, name)
},
mutate: newMutate(db),
sleepInterval: time.Second * 2,
reaperInterval: time.Second * 10,
errHandler: defaultErrorHandler,
}
for _, opt := range options {
c = opt(c)
}
return c
}
func (c *Client) WithTx(tx *sql.Tx) Tx {
return c.tx(tx)
}
func (c *Client) Schedule(ctx context.Context, id string, queueName string, arguments interface{}, options ...FnOptions) (any, error) {
return c.wrapper.WrapTx(ctx, func(ctx context.Context, tx *sql.Tx) (any, error) {
return nil, c.tx(tx).Schedule(ctx, id, queueName, arguments, options...)
})
}
func (c *Client) Cancel(ctx context.Context, id string) (any, error) {
return c.wrapper.WrapTx(ctx, func(ctx context.Context, tx *sql.Tx) (any, error) {
return nil, c.tx(tx).Cancel(ctx, id)
})
}
func (c *Client) ScheduleNow(ctx context.Context, id string) (any, error) {
return c.wrapper.WrapTx(ctx, func(ctx context.Context, tx *sql.Tx) (any, error) {
return nil, c.tx(tx).ScheduleNow(ctx, id)
})
}
func (c *Client) Get(ctx context.Context, id string) (any, error) {
res, err := c.wrapper.WrapTx(ctx, func(ctx context.Context, tx *sql.Tx) (any, error) {
return c.tx(tx).Get(ctx, id)
})
if err != nil {
return nil, err
}
return res.(*job.Job), nil
}
func (c *Client) Stop() {
c.spawn.Shutdown()
}
func (c *Client) Start() error {
g := new(errgroup.Group)
for _, co := range c.coRoutines {
g.Go(co)
}
g.Go(func() error {
c.start()
return nil
})
return g.Wait()
}
func (c *Client) start() {
errwg := &sync.WaitGroup{}
errwg.Add(1)
go errorRoutine(c.errChan, c.errHandler, errwg)
for name, config := range c.register.getWorkers() {
q := c.queue(name)
for i := 0; i < config.instances; i++ {
s := newPool(q, c.mutate, config.w, c.sleepInterval)
c.spawn.Spawn(s)
}
r := newReaper(q, c.reaperInterval, config.timeout)
c.spawn.Spawn(r)
}
c.spawn.Wait()
close(c.errChan)
errwg.Wait()
}
func errorRoutine(errChan <-chan error, errorHandler func(error), wg *sync.WaitGroup) {
defer wg.Done()
for err := range errChan {
errorHandler(err)
}
}
func defaultErrorHandler(err error) {
slog.Info("error in worker pool", "err", err)
}