mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-22 17:31:03 +01:00
96 lines
1.5 KiB
Go
96 lines
1.5 KiB
Go
package apps
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/apex/log"
|
|
"github.com/crawlab-team/crawlab/core/interfaces"
|
|
"github.com/crawlab-team/crawlab/core/node/service"
|
|
"github.com/crawlab-team/crawlab/core/utils"
|
|
"github.com/spf13/viper"
|
|
"net/http"
|
|
_ "net/http/pprof"
|
|
"sync"
|
|
)
|
|
|
|
type Server struct {
|
|
// modules
|
|
nodeSvc interfaces.NodeService
|
|
api *Api
|
|
}
|
|
|
|
func (app *Server) Init() {
|
|
// log node info
|
|
app.logNodeInfo()
|
|
|
|
// pprof
|
|
app.initPprof()
|
|
}
|
|
|
|
func (app *Server) Start() {
|
|
if utils.IsMaster() {
|
|
// start api
|
|
go start(app.api)
|
|
}
|
|
|
|
// start node service
|
|
go app.nodeSvc.Start()
|
|
}
|
|
|
|
func (app *Server) Wait() {
|
|
utils.DefaultWait()
|
|
}
|
|
|
|
func (app *Server) Stop() {
|
|
app.api.Stop()
|
|
}
|
|
|
|
func (app *Server) GetApi() ApiApp {
|
|
return app.api
|
|
}
|
|
|
|
func (app *Server) GetNodeService() interfaces.NodeService {
|
|
return app.nodeSvc
|
|
}
|
|
|
|
func (app *Server) logNodeInfo() {
|
|
log.Infof("current node type: %s", utils.GetNodeType())
|
|
}
|
|
|
|
func (app *Server) initPprof() {
|
|
if viper.GetBool("pprof") {
|
|
go func() {
|
|
fmt.Println(http.ListenAndServe("0.0.0.0:6060", nil))
|
|
}()
|
|
}
|
|
}
|
|
|
|
func newServer() App {
|
|
// server
|
|
svr := &Server{}
|
|
|
|
// master modules
|
|
if utils.IsMaster() {
|
|
// api
|
|
svr.api = GetApi()
|
|
}
|
|
|
|
// node service
|
|
if utils.IsMaster() {
|
|
svr.nodeSvc = service.GetMasterService()
|
|
} else {
|
|
svr.nodeSvc = service.GetWorkerService()
|
|
}
|
|
|
|
return svr
|
|
}
|
|
|
|
var server App
|
|
var serverOnce sync.Once
|
|
|
|
func GetServer() App {
|
|
serverOnce.Do(func() {
|
|
server = newServer()
|
|
})
|
|
return server
|
|
}
|