mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-26 17:49:15 +01:00
- Replaced all instances of apex/log with a structured logger interface in various services, including Api, Server, Config, and others, to enhance logging consistency and context. - Updated logging calls to utilize the new logger methods, improving error tracking and service monitoring. - Added logger initialization in services and controllers to ensure proper logging setup. - Improved error handling and logging messages for better clarity during service operations. - Removed unused apex/log imports and cleaned up related code for better maintainability.
100 lines
1.5 KiB
Go
100 lines
1.5 KiB
Go
package apps
|
|
|
|
import (
|
|
"fmt"
|
|
"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
|
|
|
|
// internals
|
|
interfaces.Logger
|
|
}
|
|
|
|
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() {
|
|
app.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{
|
|
Logger: utils.NewLogger("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
|
|
}
|