Skip to content

Commit 8cd547f

Browse files
authored
Merge pull request #21 from gitopia/handle-handler-error
Do not exit the server in case of event handler error
2 parents 8553d41 + 52edb2e commit 8cd547f

15 files changed

Lines changed: 190 additions & 849 deletions

File tree

app/consumer/persistence.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package consumer
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
8+
"github.com/pkg/errors"
9+
)
10+
11+
const (
12+
OFFSET_FILE = ".o"
13+
FILE_PERM = 0666
14+
)
15+
16+
type Client struct {
17+
f *os.File
18+
}
19+
20+
func NewClient(topic string) (Client, error) {
21+
file, err := os.OpenFile("_"+topic+OFFSET_FILE, os.O_RDWR|os.O_CREATE, FILE_PERM)
22+
if err != nil {
23+
return Client{}, errors.Wrap(err, "error opening offset file")
24+
}
25+
return Client{
26+
f: file,
27+
}, nil
28+
}
29+
30+
// write intensive use case. save all the offsets
31+
func (c Client) Commit(offset uint64) error {
32+
err := c.f.Truncate(0)
33+
if err != nil {
34+
return errors.Wrap(err, "error truncating offset file")
35+
}
36+
_, err = c.f.Seek(0, 0)
37+
if err != nil {
38+
return errors.Wrap(err, "error overwriting offset file")
39+
}
40+
_, err = fmt.Fprintf(c.f, "%d", offset)
41+
if err != nil {
42+
return errors.Wrap(err, "error formatting offset file")
43+
}
44+
// ensure all data s written to disk. no data loss at the cost of latency and CPU resources
45+
err = c.f.Sync()
46+
if err != nil {
47+
return errors.Wrap(err, "error syncing offset file")
48+
}
49+
return nil
50+
}
51+
52+
func (c Client) Offset() (uint64, error) {
53+
n := uint64(0)
54+
_, err := fmt.Fscanf(c.f, "%d", &n)
55+
if err != nil && err != io.EOF {
56+
return 0, errors.Wrap(err, "error reading offset file")
57+
}
58+
return n, nil
59+
}

app/gitopia.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/pkg/errors"
77
"github.com/spf13/viper"
88

9-
"github.com/gitopia/gitopia-ipfs-bridge/logger"
9+
"github.com/gitopia/git-server/logger"
1010
"github.com/gitopia/gitopia/x/gitopia/types"
1111
"github.com/tendermint/starport/starport/pkg/cosmosaccount"
1212
"github.com/tendermint/starport/starport/pkg/cosmosclient"

app/tm/tm.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package tm
2+
3+
import (
4+
"context"
5+
6+
"github.com/gitopia/git-server/logger"
7+
"github.com/pkg/errors"
8+
"github.com/spf13/viper"
9+
"github.com/tendermint/tendermint/rpc/jsonrpc/client"
10+
)
11+
12+
const (
13+
TM_WS_ENDPOINT = "/websocket"
14+
)
15+
16+
type Client struct {
17+
c *client.WSClient
18+
}
19+
20+
type evenHandlerFunc func(context.Context, []byte) error
21+
22+
func NewTmClient() (*Client, error) {
23+
wsc, err := client.NewWS(viper.GetString("tm_addr"), TM_WS_ENDPOINT)
24+
if err != nil {
25+
return nil, errors.Wrap(err, "error creating ws client")
26+
}
27+
err = wsc.Start()
28+
if err != nil {
29+
return nil, errors.Wrap(err, "error connecting to WS")
30+
}
31+
return &Client{
32+
c: wsc,
33+
}, nil
34+
}
35+
36+
// processes events from tm
37+
// returns error on failure
38+
// returns error when event handler returns error
39+
func (c Client) Subscribe(ctx context.Context, q string, h evenHandlerFunc) (<-chan struct{}, chan error) {
40+
e := make(chan error)
41+
ctx, cancel := context.WithCancel(ctx)
42+
go func() {
43+
defer cancel()
44+
err := c.c.Subscribe(ctx, q)
45+
if err != nil {
46+
e <- errors.Wrap(err, "error sending subscribe request")
47+
return
48+
}
49+
for {
50+
event := <-c.c.ResponsesCh
51+
if event.Error != nil {
52+
e <- errors.Wrap(err, "error reading from ws")
53+
return
54+
}
55+
56+
jsonBuf, err := event.Result.MarshalJSON()
57+
if err != nil {
58+
e <- errors.Wrap(err, "error parsing result")
59+
return
60+
}
61+
// hack: TM sends empty event to begin with. skipping
62+
if string(jsonBuf) == "{}" {
63+
logger.FromContext(ctx).Info("received empty event. continuing...")
64+
continue
65+
}
66+
err = h(ctx, jsonBuf)
67+
if err != nil {
68+
logger.FromContext(ctx).Error(errors.WithMessage(err, "error from event handler"))
69+
}
70+
}
71+
}()
72+
return ctx.Done(), e
73+
}

cmd/git-server-events/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/cosmos/cosmos-sdk/client"
99
"github.com/gitopia/git-server/app"
10-
"github.com/gitopia/gitopia-ipfs-bridge/logger"
10+
"github.com/gitopia/git-server/logger"
1111
"github.com/spf13/viper"
1212
)
1313

cmd/git-server-events/run.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"github.com/spf13/viper"
99

1010
"github.com/gitopia/git-server/app"
11+
"github.com/gitopia/git-server/app/consumer"
12+
"github.com/gitopia/git-server/app/tm"
1113
"github.com/gitopia/git-server/handler"
12-
"github.com/gitopia/gitopia-ipfs-bridge/app/consumer"
13-
"github.com/gitopia/gitopia-ipfs-bridge/app/tm"
14-
"github.com/gitopia/gitopia-ipfs-bridge/logger"
14+
"github.com/gitopia/git-server/logger"
1515
)
1616

1717
func NewRunCmd() *cobra.Command {

config_dev.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
web_server_port = 5000
12
gitopia_grpc_url = "grpc.gitopia.dev:9090"
23
git_dir = "/var/repos"
34
attachment_dir = "/var/attachments"

config_local.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
web_server_port = 5001
12
gitopia_grpc_url = "localhost:9090"
23
git_dir = "/var/repos"
34
attachment_dir = "/var/attachments"

config_prod.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
web_server_port = 5000
12
gitopia_grpc_url = "grpc.gitopia.com:9090"
23
git_dir = "/var/repos"
34
attachment_dir = "/var/attachments"

go.mod

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ require (
66
github.com/buger/jsonparser v1.1.1
77
github.com/cosmos/cosmos-sdk v0.45.1
88
github.com/gitopia/gitopia v0.13.0
9-
github.com/gitopia/gitopia-ipfs-bridge v0.0.0-20220324075926-93f790f71054
109
github.com/gitopia/go-git/v5 v5.4.3-0.20211224112515-b2efd9bec92c
1110
github.com/go-git/go-billy/v5 v5.3.1
11+
github.com/kr/pretty v0.3.0 // indirect
1212
github.com/libgit2/git2go/v33 v33.0.4
1313
github.com/pkg/errors v0.9.1
14+
github.com/prometheus/client_golang v1.12.0 // indirect
1415
github.com/rs/cors v1.8.2
16+
github.com/rs/zerolog v1.26.1 // indirect
17+
github.com/sirupsen/logrus v1.8.1
18+
github.com/spf13/afero v1.8.0 // indirect
1519
github.com/spf13/cobra v1.4.0
1620
github.com/spf13/viper v1.10.1
1721
github.com/tendermint/starport v0.19.3
22+
github.com/tendermint/tendermint v0.34.15
23+
golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce // indirect
24+
golang.org/x/mod v0.5.1 // indirect
1825
google.golang.org/grpc v1.45.0
26+
gopkg.in/ini.v1 v1.66.3 // indirect
1927
)
2028

2129
replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

0 commit comments

Comments
 (0)