baremetal: quit signal handle

This commit is contained in:
Zexi Li
2019-01-26 15:39:31 +08:00
parent cd1fcd349d
commit a57f8e46be
3 changed files with 39 additions and 6 deletions
+1 -1
View File
@@ -5,5 +5,5 @@ import (
)
func main() {
service.StartService()
service.New().StartService()
}
+26 -5
View File
@@ -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)
}
+12
View File
@@ -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)