Skip to content

Commit 2225856

Browse files
authored
adds stopper module (#6)
1 parent d8035a8 commit 2225856

File tree

9 files changed

+155
-8
lines changed

9 files changed

+155
-8
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go.sum merge=union
2+
**/mock/* linguist-generated=true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*.dll
55
*.so
66
*.dylib
7+
.DS_Store
78

89
# Test binary, built with `go test -c`
910
*.test

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
-include .env
22
export $(shell sed 's/=.*//' .env)
33

4+
init:
5+
chmod +x init.dev.sh && ./init.dev.sh
6+
47
lint:
58
golangci-lint run
69

@@ -21,4 +24,6 @@ generate:
2124
-p github.com/dipdup-io/uniswap
2225

2326
test:
24-
go test ./...
27+
go test ./...
28+
29+
.PHONY: init lint generate test

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module github.com/dipdup-net/indexer-sdk
22

33
go 1.20
44

5-
65
require (
6+
github.com/dipdup-io/workerpool v0.0.4
77
github.com/dipdup-net/go-lib v0.3.0
88
github.com/ethereum/go-ethereum v1.12.0
99
github.com/go-testfixtures/testfixtures/v3 v3.9.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5il
4646
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs=
4747
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
4848
github.com/denisenkom/go-mssqldb v0.12.3 h1:pBSGx9Tq67pBOTLmxNuirNTeB8Vjmf886Kx+8Y+8shw=
49+
github.com/dipdup-io/workerpool v0.0.4 h1:m58fuFY3VIPRc+trWpjw2Lsm4FvIgtjP/4VRe79r+/s=
50+
github.com/dipdup-io/workerpool v0.0.4/go.mod h1:m6YMqx7M+fORTyabHD/auKymBRpbDax0t1aPZ1i7GZA=
4951
github.com/dipdup-net/go-lib v0.3.0 h1:56OImCLIHyLL4Da7UI5H2xyTP65i2FUXSO6Gyxll460=
5052
github.com/dipdup-net/go-lib v0.3.0/go.mod h1:oBDOSsM/F8fEnmuDnaJ6QA/cHH4lne49ASbsh8WXDe4=
5153
github.com/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8=

init.dev.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
# VCS config
4+
git config --local core.attributesfile ./.gitattributes
5+
6+
# Install third tools
7+
#go install go.uber.org/mock/mockgen@main

pkg/modules/cron/README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Cron
22

3-
Cron is the module which implements a cron specific parser and notifications about scheduler events.
3+
Cron is the module that implements a cron specific parser and notifications about scheduler events.
44

55
## Usage
66

7-
Usage of cron module is described by the [example](/examples/cron/).
7+
The example [describes usage of cron module](/examples/cron/).
88

9-
To import module in your code write following line:
9+
To import module in your code, write the following line:
1010

1111
```go
1212
import "github.com/dipdup-net/indexer-sdk/pkg/modules/cron"
@@ -33,7 +33,9 @@ if err := cronModule.Close(); err != nil {
3333

3434
## Config
3535

36-
Default yaml config of cron module contains only one field `jobs`. It's a map of job names to cron pattern. Job names is used like subscription id in inner-message communication.
36+
Default yaml config of cron module contains only one field `jobs`.
37+
It's a map of job names to a cron pattern.
38+
Job names are used like subscription id in inner-message communication.
3739

3840
```yaml
3941
cron:
@@ -46,7 +48,9 @@ cron:
4648
4749
## Output
4850
49-
Module sends to its outputs empty struct which notifies all connected modules about scheduled event. Each job of cron module has own output with names pointed in configuration file. So if your module should execute some work on `every_second` scheduled events from example you should connect it:
51+
Module sends to its outputs empty struct which notifies all connected modules about scheduled event.
52+
Each job of cron module has its own output with names pointed in the configuration file.
53+
So if your module should execute some work on `every_second` scheduled events from example you should connect it:
5054

5155
```go
5256
// with helper function
@@ -62,7 +66,7 @@ if err := cronModule.AttachTo("every_second", customModule.everySecond); err !=
6266
}
6367
```
6468

65-
Example of handling message from cron's outputs:
69+
Example of a handling message from cron's outputs:
6670

6771
```go
6872
for {

pkg/modules/stopper/stopper.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package stopper
2+
3+
import (
4+
"context"
5+
"github.com/dipdup-io/workerpool"
6+
"github.com/dipdup-net/indexer-sdk/pkg/modules"
7+
"github.com/pkg/errors"
8+
"github.com/rs/zerolog"
9+
"github.com/rs/zerolog/log"
10+
)
11+
12+
const (
13+
InputName = "signal"
14+
)
15+
16+
// Module - cancels context of all application if get signal.
17+
//
18+
// |----------------|
19+
// | |
20+
// -- struct{} -> | MODULE |
21+
// | |
22+
// |----------------|
23+
type Module struct {
24+
input *modules.Input
25+
stop context.CancelFunc
26+
log zerolog.Logger
27+
g workerpool.Group
28+
}
29+
30+
func NewModule(cancelFunc context.CancelFunc) Module {
31+
m := Module{
32+
input: modules.NewInput(InputName),
33+
stop: cancelFunc,
34+
g: workerpool.NewGroup(),
35+
}
36+
m.log = log.With().Str("module", m.Name()).Logger()
37+
38+
return m
39+
}
40+
41+
func (*Module) Name() string {
42+
return "stopper"
43+
}
44+
45+
// Start -
46+
func (s *Module) Start(ctx context.Context) {
47+
s.g.GoCtx(ctx, s.listen)
48+
}
49+
50+
func (s *Module) listen(ctx context.Context) {
51+
for {
52+
select {
53+
case <-ctx.Done():
54+
return
55+
case <-s.input.Listen():
56+
log.Info().Msg("stop signal received")
57+
if s.stop != nil {
58+
log.Info().Msg("cancelling context...")
59+
s.stop()
60+
return
61+
}
62+
}
63+
}
64+
}
65+
66+
// Close -
67+
func (s *Module) Close() error {
68+
s.g.Wait()
69+
return s.input.Close()
70+
}
71+
72+
// Output -
73+
func (*Module) Output(name string) (*modules.Output, error) {
74+
return nil, errors.Wrap(modules.ErrUnknownOutput, name)
75+
}
76+
77+
// Input -
78+
func (s *Module) Input(name string) (*modules.Input, error) {
79+
if name != InputName {
80+
return nil, errors.Wrap(modules.ErrUnknownInput, name)
81+
}
82+
return s.input, nil
83+
}
84+
85+
// AttachTo -
86+
func (s *Module) AttachTo(name string, input *modules.Input) error {
87+
output, err := s.Output(name)
88+
if err != nil {
89+
return err
90+
}
91+
92+
output.Attach(input)
93+
return nil
94+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package stopper
2+
3+
import (
4+
"context"
5+
"github.com/stretchr/testify/assert"
6+
"testing"
7+
)
8+
9+
func TestStopperCallsStop(t *testing.T) {
10+
stopWasCalled := false
11+
12+
var stopFunc context.CancelFunc = func() {
13+
stopWasCalled = true
14+
}
15+
16+
ctx, cancel := context.WithCancel(context.Background())
17+
defer cancel()
18+
19+
stopperModule := NewModule(stopFunc)
20+
stopperModule.Start(ctx)
21+
22+
stopperInput, err := stopperModule.Input(InputName)
23+
assert.NoError(t, err)
24+
25+
// Act: send stop signal to stopper
26+
stopperInput.Push(struct{}{})
27+
28+
err = stopperModule.Close()
29+
assert.NoError(t, err)
30+
31+
assert.True(t, stopWasCalled, "stop was never called")
32+
}

0 commit comments

Comments
 (0)