Skip to content

Commit

Permalink
Merge pull request #34 from kaleido-io/graceful-shutdown
Browse files Browse the repository at this point in the history
Gracefully terminate LevelDB on shutdown

Signed-off-by: Vinod Damle <vinod.damle@kaleido.io>
  • Loading branch information
vdamle authored Aug 19, 2019
2 parents 61e7ea3 + 7d2f139 commit b95f98e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions internal/kldcontracts/rest2eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (m *mockABILoader) checkNameAvailable(name string) error {
func (m *mockABILoader) PreDeploy(msg *kldmessages.DeployContract) error { return nil }
func (m *mockABILoader) PostDeploy(msg *kldmessages.TransactionReceipt) error { return nil }
func (m *mockABILoader) AddRoutes(router *httprouter.Router) { return }
func (m *mockABILoader) Shutdown() { return }

type mockRPC struct {
capturedMethod string
Expand Down
8 changes: 8 additions & 0 deletions internal/kldcontracts/smartcontractgw.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type SmartContractGateway interface {
PreDeploy(msg *kldmessages.DeployContract) error
PostDeploy(msg *kldmessages.TransactionReceipt) error
AddRoutes(router *httprouter.Router)
Shutdown()
}

type smartContractGatewayInt interface {
Expand Down Expand Up @@ -1073,3 +1074,10 @@ func (g *smartContractGW) writeHTMLForUI(prefix, id, fromQuery string, factory b
res.WriteHeader(200)
res.Write([]byte(html))
}

// Shutdown performs a clean shutdown
func (g *smartContractGW) Shutdown() {
if g.sm != nil {
g.sm.Close()
}
}
1 change: 1 addition & 0 deletions internal/kldcontracts/smartcontractgw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,7 @@ func TestAddStreamOK(t *testing.T) {
json.NewDecoder(res.Body).Decode(&newSpec)
assert.Equal(200, res.Result().StatusCode)
assert.Equal("webhook", newSpec.Type)
s.Shutdown()
}

func TestAddStreamBadData(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions internal/kldevents/submanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ func (s *subscriptionMGR) recoverSubscriptions() {
}

func (s *subscriptionMGR) Close() {
log.Infof("Event stream subscription manager shutting down")
if s.db != nil {
s.db.Close()
s.db = nil
}
}
3 changes: 2 additions & 1 deletion internal/kldkafka/kafkacommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"
"strings"
"sync"
"syscall"
"time"

"github.com/Shopify/sarama"
Expand Down Expand Up @@ -278,7 +279,7 @@ func (k *kafkaCommon) Start() (err error) {
}

k.signals = make(chan os.Signal, 1)
signal.Notify(k.signals, os.Interrupt)
signal.Notify(k.signals, syscall.SIGTERM, syscall.SIGINT)
for {
select {
case <-k.signals:
Expand Down
13 changes: 12 additions & 1 deletion internal/kldrest/restgateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"fmt"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"

"github.com/kaleido-io/ethconnect/internal/kldcontracts"
Expand Down Expand Up @@ -280,15 +282,24 @@ func (g *RESTGateway) Start() (err error) {
time.Sleep(250 * time.Millisecond)
}
readyToListen <- true
// Complete the main routine if any child ends

// Clean up on SIGINT
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)
// Complete the main routine if any child ends, or SIGINT
select {
case err = <-gwDone:
break
case err = <-svrDone:
break
case <-signals:
break
}

// Ensure we shutdown the server
if g.smartContractGW != nil {
g.smartContractGW.Shutdown()
}
log.Infof("Shutting down HTTP server")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
g.srv.Shutdown(ctx)
Expand Down
2 changes: 2 additions & 0 deletions internal/kldrest/webhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func (m *mockContractGW) PostDeploy(*kldmessages.TransactionReceipt) error { ret

func (m *mockContractGW) AddRoutes(*httprouter.Router) {}

func (m *mockContractGW) Shutdown() {}

type mockHandler struct{}

func (*mockHandler) sendWebhookMsg(key, msgID string, msg map[string]interface{}, ack bool) (msgAck string, statusCode int, err error) {
Expand Down

0 comments on commit b95f98e

Please sign in to comment.