-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom.go
104 lines (87 loc) · 1.84 KB
/
custom.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
package main
import (
"context"
"encoding/json"
"sync"
"github.com/dipdup-net/indexer-sdk/pkg/modules"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
// CustomModule -
type CustomModule struct {
input *modules.Input
wg *sync.WaitGroup
}
// NewCustomModule -
func NewCustomModule() *CustomModule {
return &CustomModule{
input: modules.NewInput("input"),
wg: new(sync.WaitGroup),
}
}
// Start -
func (m *CustomModule) Start(ctx context.Context) {
m.wg.Add(1)
go m.listen(ctx)
}
// Input -
func (m *CustomModule) Input(name string) (*modules.Input, error) {
if name != "input" {
return nil, errors.Wrap(modules.ErrUnknownInput, name)
}
return m.input, nil
}
// MustInput -
func (m *CustomModule) MustInput(name string) *modules.Input {
input, err := m.Input(name)
if err != nil {
panic(err)
}
return input
}
// Output -
func (m *CustomModule) Output(name string) (*modules.Output, error) {
return nil, errors.Wrap(modules.ErrUnknownOutput, name)
}
// MustOutput -
func (m *CustomModule) MustOutput(name string) *modules.Output {
output, err := m.Output(name)
if err != nil {
panic(err)
}
return output
}
// AttachTo -
func (m *CustomModule) AttachTo(outputModule modules.Module, outputName, inputName string) error {
outputChannel, err := outputModule.Output(outputName)
if err != nil {
return err
}
input, err := m.Input(inputName)
if err != nil {
return err
}
outputChannel.Attach(input)
return nil
}
func (m *CustomModule) listen(ctx context.Context) {
defer m.wg.Done()
for {
select {
case <-ctx.Done():
return
case msg := <-m.input.Listen():
b, _ := json.Marshal(msg)
log.Info().Str("msg", string(b)).Msg("arrived from grpc module")
}
}
}
// Close -
func (m *CustomModule) Close() error {
m.wg.Wait()
return m.input.Close()
}
// Name -
func (*CustomModule) Name() string {
return "custom"
}