Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions services/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package services

import (
"os"
"os/signal"
"syscall"

"github.com/smartcontractkit/chainlink-go/logger"
"github.com/smartcontractkit/chainlink-go/models"
)

Expand All @@ -9,6 +14,8 @@ type Store struct {
Scheduler *Scheduler
Config Config
KeyStore *KeyStore
sigs chan os.Signal
Exiter func(int)
}

func NewStore(config Config) *Store {
Expand All @@ -18,14 +25,23 @@ func NewStore(config Config) *Store {
Scheduler: NewScheduler(orm),
Config: config,
KeyStore: NewKeyStore(config.KeysDir()),
Exiter: os.Exit,
}
}

func (self *Store) Start() error {
self.sigs = make(chan os.Signal, 1)
signal.Notify(self.sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-self.sigs
self.Close()
self.Exiter(1)
}()
return self.Scheduler.Start()
}

func (self *Store) Close() {
logger.Info("Gracefully exiting...")
self.Scheduler.Stop()
self.ORM.Close()
}
Expand Down
28 changes: 28 additions & 0 deletions services/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package services_test

import (
"syscall"
"testing"

. "github.com/onsi/gomega"
"github.com/smartcontractkit/chainlink-go/internal/cltest"
)

func TestGracefulShutdown(t *testing.T) {
t.Parallel()
RegisterTestingT(t)
store := cltest.Store()
defer store.Close()

var completed bool
store.Exiter = func(code int) {
completed = true
}

store.Start()
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)

Eventually(func() bool {
return completed
}).Should(BeTrue())
}