mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-31 01:35:56 +08:00
baremetal: quit signal handle
This commit is contained in:
@@ -5,5 +5,5 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
service.StartService()
|
||||
service.New().StartService()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"yunion.io/x/log"
|
||||
@@ -8,25 +9,45 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/baremetal"
|
||||
"yunion.io/x/onecloud/pkg/baremetal/handler"
|
||||
o "yunion.io/x/onecloud/pkg/baremetal/options"
|
||||
"yunion.io/x/onecloud/pkg/baremetal/tasks"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/service"
|
||||
)
|
||||
|
||||
type BaremetalService struct {
|
||||
service.SServiceBase
|
||||
isExiting bool
|
||||
}
|
||||
|
||||
func New() *BaremetalService {
|
||||
return &BaremetalService{}
|
||||
}
|
||||
|
||||
func (s *BaremetalService) StartService() {
|
||||
cloudcommon.ParseOptions(&o.Options, os.Args, "baremetal.conf", "baremetal")
|
||||
cloudcommon.InitAuth(&o.Options.CommonOptions, s.startAgent)
|
||||
|
||||
s.RegisterSIGUSR1()
|
||||
|
||||
s.RegisterQuitSignals(func() {
|
||||
log.Infof("Baremetal agent quit !!!")
|
||||
})
|
||||
app := cloudcommon.InitApp(&o.Options.CommonOptions, false)
|
||||
handler.InitHandlers(app)
|
||||
|
||||
s.RegisterSIGUSR1()
|
||||
s.RegisterQuitSignals(func() {
|
||||
log.Infof("Baremetal agent quit !!!")
|
||||
if s.isExiting {
|
||||
return
|
||||
} else {
|
||||
s.isExiting = true
|
||||
}
|
||||
|
||||
if app.IsInServe() {
|
||||
if err := app.ShowDown(context.Background()); err != nil {
|
||||
log.Errorf("App shutdown err: %v", err)
|
||||
}
|
||||
}
|
||||
tasks.OnStop()
|
||||
os.Exit(0)
|
||||
})
|
||||
|
||||
cloudcommon.ServeForever(app, &o.Options.CommonOptions)
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
@@ -18,6 +19,17 @@ func init() {
|
||||
baremetalTaskWorkerMan = appsrv.NewWorkerManager("BaremetalTaskWorkerManager", 8, 1024, false)
|
||||
}
|
||||
|
||||
func GetWorkManager() *appsrv.SWorkerManager {
|
||||
return baremetalTaskWorkerMan
|
||||
}
|
||||
|
||||
func OnStop() {
|
||||
for GetWorkManager().ActiveWorkerCount() > 0 {
|
||||
log.Warningf("Busy workers count %d, waiting them finish", GetWorkManager().ActiveWorkerCount())
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func ExecuteTask(task ITask, args interface{}) {
|
||||
baremetalTaskWorkerMan.Run(func() {
|
||||
executeTask(task, args)
|
||||
|
||||
Reference in New Issue
Block a user