featurebase/core/service.go
Ben Johnson 2f1e3b6078 Refactor config & service
This commit refactors the config into a `main.Config` object instead
of a global singleton. The `core.Service` is also refactored into
the `main` package and individual pieces of the service are wired
together by the `pilosa` binary.

These two changes are required to begin to decouple packages from
one another and allow them to be individually unit tested. Previously
most top-level objects in the system could access any other top-level
object through the `core.Service` which effectively made `Service` a
singleton in the system. Each top-level object now has inline interfaces
for their dependencies so that can be set at runtime by the `main`
package or can be mocked by a test package.
2015-08-12 15:34:23 -06:00

73 lines
1.6 KiB
Go

package core
import (
"os"
"os/signal"
"syscall"
log "github.com/cihub/seelog"
"github.com/coreos/go-etcd/etcd"
"github.com/umbel/pilosa/db"
"github.com/umbel/pilosa/hold"
"github.com/umbel/pilosa/index"
"github.com/umbel/pilosa/interfaces"
"github.com/umbel/pilosa/util"
)
type Service struct {
Stopper
Id *util.GUID
Etcd *etcd.Client
Cluster *db.Cluster
TopologyMapper *TopologyMapper
ProcessMapper *ProcessMapper
ProcessMap *ProcessMap
Executor interfaces.Executorer
WebService *WebService
Index *index.FragmentContainer
Hold *hold.Holder
version string
name string
}
var Build string
func (service *Service) GetSignals() (chan os.Signal, chan os.Signal) {
hupChan := make(chan os.Signal, 1)
termChan := make(chan os.Signal, 1)
signal.Notify(hupChan, syscall.SIGHUP)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM)
return termChan, hupChan
}
func (service *Service) Run() {
log.Info("Setup service...", service.version)
service.TopologyMapper.Setup()
log.Info("Running service...", service.version)
go service.TopologyMapper.Run()
go service.ProcessMapper.Run()
go service.WebService.Run()
go service.Executor.Run()
go service.Hold.Run()
sigterm, sighup := service.GetSignals()
for {
select {
case <-sighup:
log.Info("SIGHUP! Reloading configuration...")
// TODO: reload configuration
case <-sigterm:
log.Info("SIGTERM! Cleaning up...")
service.Index.Shutdown()
service.WebService.Shutdown()
util.ShutdownStats()
service.Stop()
return
}
}
log.Info("Service stopping")
}
type Message interface {
Handle(*Service)
}