Skip to content

Commit

Permalink
renamed TaskParameters to JobParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarrosop committed Jun 4, 2019
1 parent 412fc84 commit 83a249a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 40 deletions.
8 changes: 4 additions & 4 deletions examples/3_grouped_simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
type checkMemoryAndCPU struct {
}

func (c *checkMemoryAndCPU) Run(ctx context.Context, wg *sync.WaitGroup, tp *gornir.TaskParameters, jobResult chan *gornir.JobResult) {
func (c *checkMemoryAndCPU) Run(ctx context.Context, wg *sync.WaitGroup, jp *gornir.JobParameters, jobResult chan *gornir.JobResult) {
// We instantiate a new object
result := gornir.NewJobResult(ctx, tp)
result := gornir.NewJobResult(ctx, jp)

defer wg.Done() // flag as completed

Expand All @@ -33,11 +33,11 @@ func (c *checkMemoryAndCPU) Run(ctx context.Context, wg *sync.WaitGroup, tp *gor
swg.Add(2)

// We call the first subtask and store the subresult
(&task.RemoteCommand{Command: "free -m"}).Run(ctx, swg, tp, sr)
(&task.RemoteCommand{Command: "free -m"}).Run(ctx, swg, jp, sr)
result.AddSubResult(<-sr)

// We call the second subtask and store the subresult
(&task.RemoteCommand{Command: "uptime"}).Run(ctx, swg, tp, sr)
(&task.RemoteCommand{Command: "uptime"}).Run(ctx, swg, jp, sr)
result.AddSubResult(<-sr)

jobResult <- result
Expand Down
4 changes: 2 additions & 2 deletions pkg/gornir/gornir.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (gr *Gornir) RunSync(title string, runner Runner, task Task) (chan *JobResu
context.Background(),
task,
gr.Inventory.Hosts,
NewTaskParameters(title, logger),
NewJobParameters(title, logger),
results,
)
if err != nil {
Expand All @@ -56,7 +56,7 @@ func (gr *Gornir) RunAsync(ctx context.Context, title string, runner Runner, tas
ctx,
task,
gr.Inventory.Hosts,
NewTaskParameters(title, logger),
NewJobParameters(title, logger),
results,
)
if err != nil {
Expand Down
46 changes: 23 additions & 23 deletions pkg/gornir/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ import (
"sync"
)

type TaskParameters struct {
type JobParameters struct {
title string
logger Logger
host *Host
}

func NewTaskParameters(title string, logger Logger) *TaskParameters {
return &TaskParameters{
func NewJobParameters(title string, logger Logger) *JobParameters {
return &JobParameters{
title: title,
logger: logger,
}
}

func (tp *TaskParameters) Title() string {
return tp.title
func (jp *JobParameters) Title() string {
return jp.title
}

func (tp *TaskParameters) Logger() Logger {
return tp.logger
func (jp *JobParameters) Logger() Logger {
return jp.logger
}

func (tp *TaskParameters) Host() *Host {
return tp.host
func (jp *JobParameters) Host() *Host {
return jp.host
}

func (tp *TaskParameters) ForHost(host *Host) *TaskParameters {
return &TaskParameters{
title: tp.title,
logger: tp.logger.WithField("host", host.Hostname),
func (jp *JobParameters) ForHost(host *Host) *JobParameters {
return &JobParameters{
title: jp.title,
logger: jp.logger.WithField("host", host.Hostname),
host: host,
}
}
Expand All @@ -42,32 +42,32 @@ func (tp *TaskParameters) ForHost(host *Host) *TaskParameters {
// the task is responsible to indicate its completion
// by calling sync.WaitGroup.Done()
type Task interface {
Run(context.Context, *sync.WaitGroup, *TaskParameters, chan *JobResult)
Run(context.Context, *sync.WaitGroup, *JobParameters, chan *JobResult)
}

// Runner is the interface of a struct that can implement a strategy
// to run tasks over hosts
type Runner interface {
Run(context.Context, Task, map[string]*Host, *TaskParameters, chan *JobResult) error // Run executes the task over the hosts
Close() error // Close closes and cleans all objects associated with the runner
Wait() error // Wait blocks until all the hosts are done executing the task
Run(context.Context, Task, map[string]*Host, *JobParameters, chan *JobResult) error // Run executes the task over the hosts
Close() error // Close closes and cleans all objects associated with the runner
Wait() error // Wait blocks until all the hosts are done executing the task
}

// JobResult is the result of running a task over a host.
type JobResult struct {
ctx context.Context
tp *TaskParameters
jp *JobParameters
err error
changed bool
data interface{}
subResults []*JobResult
}

// NewJobResult instantiates a new JobResult
func NewJobResult(ctx context.Context, tp *TaskParameters) *JobResult {
func NewJobResult(ctx context.Context, jp *JobParameters) *JobResult {
return &JobResult{
ctx: ctx,
tp: tp,
jp: jp,
}
}

Expand All @@ -76,8 +76,8 @@ func (r *JobResult) Context() context.Context {
return r.ctx
}

func (r *JobResult) TaskParameters() *TaskParameters {
return r.tp
func (r *JobResult) JobParameters() *JobParameters {
return r.jp
}

// Err returns the error the task set, otherwise nil
Expand All @@ -102,7 +102,7 @@ func (r *JobResult) AnyErr() error {
// SetErr stores the error and also propagates it to the associated Host
func (r *JobResult) SetErr(err error) {
r.err = err
r.TaskParameters().Host().setErr(err)
r.JobParameters().Host().setErr(err)
}

// Changed will return whether the task changed something or not
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/output/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func renderResult(wr io.Writer, result *gornir.JobResult, renderHost bool, color
default:
colorFunc = yellow
}
if _, err := wr.Write([]byte(colorFunc(fmt.Sprintf("@ %s\n", result.TaskParameters().Host().Hostname), color))); err != nil {
if _, err := wr.Write([]byte(colorFunc(fmt.Sprintf("@ %s\n", result.JobParameters().Host().Hostname), color))); err != nil {
return err
}
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func renderResult(wr io.Writer, result *gornir.JobResult, renderHost bool, color
func RenderResults(wr io.Writer, results chan *gornir.JobResult, color bool) error {
r := <-results

title := blue(fmt.Sprintf("# %s\n", r.TaskParameters().Title()), color)
title := blue(fmt.Sprintf("# %s\n", r.JobParameters().Title()), color)
if _, err := wr.Write([]byte(title)); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/plugins/runner/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func Parallel() *ParallelRunner {
}
}

func (r ParallelRunner) Run(ctx context.Context, task gornir.Task, hosts map[string]*gornir.Host, tp *gornir.TaskParameters, results chan *gornir.JobResult) error {
logger := tp.Logger().WithField("runner", "Parallel")
func (r ParallelRunner) Run(ctx context.Context, task gornir.Task, hosts map[string]*gornir.Host, jp *gornir.JobParameters, results chan *gornir.JobResult) error {
logger := jp.Logger().WithField("runner", "Parallel")
logger.Debug("starting runner")

if len(hosts) == 0 {
Expand All @@ -30,7 +30,7 @@ func (r ParallelRunner) Run(ctx context.Context, task gornir.Task, hosts map[str

for hostname, host := range hosts {
logger.WithField("host", hostname).Debug("calling function")
go task.Run(ctx, r.wg, tp.ForHost(host), results)
go task.Run(ctx, r.wg, jp.ForHost(host), results)
}
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/plugins/runner/serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func Sorted() *SortedRunner {
return &SortedRunner{}
}

func (r SortedRunner) Run(ctx context.Context, task gornir.Task, hosts map[string]*gornir.Host, tp *gornir.TaskParameters, results chan *gornir.JobResult) error {
logger := tp.Logger().WithField("runner", "Sorted")
func (r SortedRunner) Run(ctx context.Context, task gornir.Task, hosts map[string]*gornir.Host, jp *gornir.JobParameters, results chan *gornir.JobResult) error {
logger := jp.Logger().WithField("runner", "Sorted")
logger.Debug("starting runner")

if len(hosts) == 0 {
Expand All @@ -39,7 +39,7 @@ func (r SortedRunner) Run(ctx context.Context, task gornir.Task, hosts map[strin
for _, hostname := range sortedHostnames {
host := hosts[hostname]
logger.WithField("host", hostname).Debug("calling function")
task.Run(ctx, wg, tp.ForHost(host), results)
task.Run(ctx, wg, jp.ForHost(host), results)
}
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/plugins/task/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ type RemoteCommandResults struct {
Stderr []byte // Stderr written by the command
}

func (r *RemoteCommand) Run(ctx context.Context, wg *sync.WaitGroup, tp *gornir.TaskParameters, jobResult chan *gornir.JobResult) {
func (r *RemoteCommand) Run(ctx context.Context, wg *sync.WaitGroup, jp *gornir.JobParameters, jobResult chan *gornir.JobResult) {
defer wg.Done()
host := tp.Host()
result := gornir.NewJobResult(ctx, tp)
host := jp.Host()
result := gornir.NewJobResult(ctx, jp)

sshConfig := &ssh.ClientConfig{
User: host.Username,
Expand Down

0 comments on commit 83a249a

Please sign in to comment.