add option for global stop on error
This commit is contained in:
parent
6ae734956b
commit
b3c2c40fe8
@ -32,6 +32,7 @@ const (
|
||||
|
||||
type deployer struct {
|
||||
ctx context.Context
|
||||
fnCancel context.CancelFunc
|
||||
|
||||
type_ DeployerType
|
||||
project *models.Project
|
||||
@ -57,6 +58,13 @@ func newDeployer(ctx context.Context, type_ DeployerType, project *models.Projec
|
||||
func (d *deployer) setDone(err error) {
|
||||
d.chDone <- struct{}{}
|
||||
d.errFlag = err
|
||||
if err != nil && d.fnCancel != nil {
|
||||
d.fnCancel()
|
||||
}
|
||||
}
|
||||
|
||||
func (d *deployer) SetCancellationFunc(fnCancel context.CancelFunc) {
|
||||
d.fnCancel = fnCancel
|
||||
}
|
||||
|
||||
func (d *deployer) Type() DeployerType {
|
||||
|
||||
33
main.go
33
main.go
@ -37,6 +37,18 @@ var (
|
||||
ErrGenerateTasksTree = errors.New("unable to generate tasks tree")
|
||||
)
|
||||
|
||||
type Option struct {
|
||||
fnCancel context.CancelFunc
|
||||
}
|
||||
|
||||
type InitOption func(o *Option)
|
||||
|
||||
func WithGlobalCancellation(fnCancel context.CancelFunc) InitOption {
|
||||
return func(o *Option) {
|
||||
o.fnCancel = fnCancel
|
||||
}
|
||||
}
|
||||
|
||||
func initLogger() {
|
||||
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
||||
log.Logger = log.With().Caller().Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||
@ -71,6 +83,7 @@ func initDeployers(
|
||||
ctx context.Context,
|
||||
hmmap *models.HMMap,
|
||||
project *models.Project,
|
||||
options ...InitOption,
|
||||
) ([]deployers.IDeployer, error) {
|
||||
swarmNet := hmmap.GetSwarmNetInfo()
|
||||
if swarmNet == nil {
|
||||
@ -83,7 +96,7 @@ func initDeployers(
|
||||
return nil, fmt.Errorf("%w, unable to init swarm deployer, err=%v", ErrDeployerInit, err)
|
||||
}
|
||||
|
||||
var nd deployers.IDeployer
|
||||
var nd deployers.NginxDeployer
|
||||
if project.Deps.NginxFile != "" {
|
||||
nginxNet := hmmap.GetNginxNetInfo()
|
||||
if nginxNet == nil {
|
||||
@ -99,10 +112,20 @@ func initDeployers(
|
||||
)
|
||||
}
|
||||
|
||||
nd = &d
|
||||
nd = d
|
||||
}
|
||||
|
||||
return []deployers.IDeployer{&sd, nd}, nil
|
||||
var opt Option
|
||||
for _, o := range options {
|
||||
o(&opt)
|
||||
}
|
||||
|
||||
if opt.fnCancel != nil {
|
||||
sd.SetCancellationFunc(opt.fnCancel)
|
||||
nd.SetCancellationFunc(opt.fnCancel)
|
||||
}
|
||||
|
||||
return []deployers.IDeployer{&sd, &nd}, nil
|
||||
}
|
||||
|
||||
func generateTasksTree(deployers []deployers.IDeployer) ([]*scheduler.Task, error) {
|
||||
@ -158,7 +181,7 @@ func waitForCompletion(deployers []deployers.IDeployer, s *scheduler.Scheduler)
|
||||
}
|
||||
|
||||
func main() { //nolint: funlen // TODO: to rework
|
||||
ctx, _ := signal.NotifyContext(
|
||||
ctx, fnCancel := signal.NotifyContext(
|
||||
context.Background(),
|
||||
os.Interrupt,
|
||||
os.Kill,
|
||||
@ -184,7 +207,7 @@ func main() { //nolint: funlen // TODO: to rework
|
||||
Str("name", project.Name).
|
||||
Msg("project initialized with success")
|
||||
|
||||
deployers, err := initDeployers(ctx, &hmmap, &project)
|
||||
deployers, err := initDeployers(ctx, &hmmap, &project, WithGlobalCancellation(fnCancel))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("unable to init deployers")
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user