-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
handler.go
42 lines (38 loc) · 1.04 KB
/
handler.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
package handler
import (
"context"
"encoding/json"
"github.com/bitmagnet-io/bitmagnet/internal/boilerplate/lazy"
"github.com/bitmagnet-io/bitmagnet/internal/model"
"github.com/bitmagnet-io/bitmagnet/internal/processor"
"github.com/bitmagnet-io/bitmagnet/internal/queue/handler"
"go.uber.org/fx"
"go.uber.org/zap"
"time"
)
type Params struct {
fx.In
Processor lazy.Lazy[processor.Processor]
Logger *zap.SugaredLogger
}
type Result struct {
fx.Out
Handler lazy.Lazy[handler.Handler] `group:"queue_handlers"`
}
func New(p Params) Result {
return Result{
Handler: lazy.New(func() (handler.Handler, error) {
pr, err := p.Processor.Get()
if err != nil {
return handler.Handler{}, err
}
return handler.New(processor.MessageName, func(ctx context.Context, job model.QueueJob) (err error) {
msg := &processor.MessageParams{}
if err := json.Unmarshal([]byte(job.Payload), msg); err != nil {
return err
}
return pr.Process(ctx, *msg)
}, handler.JobTimeout(time.Second*60*5), handler.Concurrency(2)), nil
}),
}
}