Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package workflow
import (
"context"
"syscall"
"ascend-common/common-utils/hwlog"
"container-manager/pkg/common"
)
type Module interface {
Name() string
Init() error
Work(ctx context.Context)
ShutDown()
}
type ModuleMgr struct {
modules []Module
}
func NewModuleMgr() *ModuleMgr {
return &ModuleMgr{}
}
func (mm *ModuleMgr) Register(module Module) {
mm.modules = append(mm.modules, module)
}
func (mm *ModuleMgr) Init() error {
for _, module := range mm.modules {
if err := module.Init(); err != nil {
return err
}
}
return nil
}
func (mm *ModuleMgr) Work(ctx context.Context) {
for _, module := range mm.modules {
go module.Work(ctx)
}
}
func (mm *ModuleMgr) ShutDown() {
osSignChan := common.NewSignWatcher(syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)
if osSignChan == nil {
hwlog.RunLog.Error("the stop signal is not initialized")
return
}
select {
case s, signEnd := <-osSignChan:
if signEnd == false {
hwlog.RunLog.Info("catch stop signal channel is closed")
return
}
hwlog.RunLog.Infof("received signal: %s, shutting down", s.String())
for _, module := range mm.modules {
module.ShutDown()
}
}
}