util: ansible: session manager manages runnable

This commit is contained in:
Yousong Zhou
2019-10-18 03:22:29 +00:00
parent 9a8a9a3980
commit bbc2a50dff
+10 -6
View File
@@ -18,12 +18,16 @@ import (
"context"
)
type Runnable interface {
Run(context.Context) error
}
// Session is a container for execution of playbook
type Session struct {
// Ctx is the context under which the playbook will run
Ctx context.Context
// Playbook is the ansible playbook to be run
Playbook *Playbook
// Runnable is the task to be run
Runnable Runnable
// cancelFunc can be called to cancel the running playbook
cancelFunc context.CancelFunc
@@ -38,11 +42,11 @@ func (sm SessionManager) Has(id string) bool {
return ok
}
// Add adds a Playbook to the manager keyed with the specified id
func (sm SessionManager) Add(id string, pb *Playbook) *Session {
// Add adds a Runnable to the manager keyed with the specified id
func (sm SessionManager) Add(id string, runnable Runnable) *Session {
ctx, cancelFunc := context.WithCancel(context.Background())
session := &Session{
Playbook: pb,
Runnable: runnable,
Ctx: ctx,
cancelFunc: cancelFunc,
}
@@ -63,7 +67,7 @@ func (sm SessionManager) Run(id string) error {
if !ok {
return nil
}
return s.Playbook.Run(s.Ctx)
return s.Runnable.Run(s.Ctx)
}
// Stop stops the playbook keyed with specified id