-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservice.go
81 lines (68 loc) · 2.62 KB
/
service.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
package async_sequencer
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/code-payments/code-server/pkg/code/async"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/action"
"github.com/code-payments/code-server/pkg/code/data/fulfillment"
"github.com/code-payments/code-server/pkg/code/data/intent"
)
var (
ErrInvalidFulfillmentSignature = errors.New("invalid fulfillment signature")
ErrInvalidFulfillmentStateTransition = errors.New("invalid fulfillment state transition")
ErrCouldNotGetIntentLock = errors.New("could not get intent lock")
)
type service struct {
log *logrus.Entry
conf *conf
data code_data.Provider
scheduler Scheduler
fulfillmentHandlersByType map[fulfillment.Type]FulfillmentHandler
actionHandlersByType map[action.Type]ActionHandler
intentHandlersByType map[intent.Type]IntentHandler
}
func New(data code_data.Provider, scheduler Scheduler, configProvider ConfigProvider) async.Service {
return &service{
log: logrus.StandardLogger().WithField("service", "sequencer"),
conf: configProvider(),
data: data,
scheduler: scheduler,
fulfillmentHandlersByType: getFulfillmentHandlers(data, configProvider),
actionHandlersByType: getActionHandlers(data),
intentHandlersByType: getIntentHandlers(data),
}
}
func (p *service) Start(ctx context.Context, interval time.Duration) error {
// Setup workers to watch for fulfillment state changes on the Solana side
for _, item := range []fulfillment.State{
fulfillment.StateUnknown,
fulfillment.StatePending,
// There's no executable logic for these states yet:
// fulfillment.StateConfirmed,
// fulfillment.StateFailed,
// fulfillment.StateRevoked,
} {
go func(state fulfillment.State) {
// todo: Note to our future selves that there are some components of
// the scheduler (ie. subsidizer balance checks) that won't
// work perfectly in a multi-threaded or multi-node environment.
err := p.worker(ctx, state, interval)
if err != nil && err != context.Canceled {
p.log.WithError(err).Warnf("fulfillment processing loop terminated unexpectedly for state %d", state)
}
}(item)
}
go func() {
err := p.metricsGaugeWorker(ctx)
if err != nil && err != context.Canceled {
p.log.WithError(err).Warn("fulfillment metrics gauge loop terminated unexpectedly")
}
}()
select {
case <-ctx.Done():
return ctx.Err()
}
}