Skip to content

Commit

Permalink
(choria-io#2209) Adds basic executor command with signal and info
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar committed Jan 24, 2025
1 parent a836388 commit 1f979cd
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 2 deletions.
4 changes: 2 additions & 2 deletions client/discovery/cli_helper.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2021-2025, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -216,7 +216,7 @@ func (o *StandardOptions) isPiped() bool {
return (fi.Mode() & os.ModeCharDevice) == 0
}

// SetDefaultsFromChoria sets the defaults based on cfg
// SetDefaultsFromChoria sets the defaults based on choria
func (o *StandardOptions) SetDefaultsFromChoria(fw inter.Framework) {
o.SetDefaultsFromConfig(fw.Configuration())
}
Expand Down
30 changes: 30 additions & 0 deletions cmd/executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2025, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

package cmd

import "sync"

type execCommand struct {
command
}

func (c *execCommand) Setup() (err error) {
c.cmd = cli.app.Command("executor", "Long running process executor management").Alias("exec")

return nil
}

func (c *execCommand) Configure() error {
return nil
}
func (c *execCommand) Run(wg *sync.WaitGroup) (err error) {
defer wg.Done()

return nil
}

func init() {
cli.commands = append(cli.commands, &execCommand{})
}
74 changes: 74 additions & 0 deletions cmd/executor_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2025, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"github.com/choria-io/go-choria/client/discovery"
"github.com/choria-io/go-choria/client/executorclient"
"os"
"sync"
)

type executorInfoCommand struct {
command

fo *discovery.StandardOptions

id string
json bool
}

func init() {
cli.commands = append(cli.commands, &executorInfoCommand{})
}

func (n *executorInfoCommand) Setup() error {
if exec, ok := cmdWithFullCommand("executor"); ok {
n.cmd = exec.Cmd().Command("info", "Retrieves Job Information")
n.cmd.Arg("id", "The Job ID to retrieves information for").Required().StringVar(&n.id)
n.cmd.Flag("json", "Renders result in JSON format").UnNegatableBoolVar(&n.json)

n.fo = discovery.NewStandardOptions()
n.fo.AddFilterFlags(n.cmd)
n.fo.AddFlatFileFlags(n.cmd)
n.fo.AddSelectionFlags(n.cmd)
}

return nil
}

func (n *executorInfoCommand) Configure() (err error) {
return commonConfigure()
}

func (n *executorInfoCommand) Run(wg *sync.WaitGroup) (err error) {
defer wg.Done()

n.fo.SetDefaultsFromChoria(c)
log := c.Logger("executor")

opts := []executorclient.InitializationOption{
executorclient.Logger(log), executorclient.Discovery(executorclient.NewMetaNS(n.fo, true)),
}
if !n.json {
opts = append(opts, executorclient.Progress())
}

ec, err := executorclient.New(c, opts...)
if err != nil {
return err
}

res, err := ec.Status(n.id).Do(ctx)
if err != nil {
return err
}

format := executorclient.TextFormat
if n.json {
format = executorclient.JSONFormat
}
return res.RenderResults(os.Stdout, format, executorclient.DisplayDDL, debug, false, true, log)
}
76 changes: 76 additions & 0 deletions cmd/executor_signal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2025, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"github.com/choria-io/go-choria/client/discovery"
"github.com/choria-io/go-choria/client/executorclient"
"os"
"sync"
)

type executorSignalCommand struct {
command

fo *discovery.StandardOptions

id string
sig uint
json bool
}

func init() {
cli.commands = append(cli.commands, &executorSignalCommand{})
}

func (s *executorSignalCommand) Setup() error {
if exec, ok := cmdWithFullCommand("executor"); ok {
s.cmd = exec.Cmd().Command("signal", "Retrieves Job Information").Alias("sig").Alias("kill")
s.cmd.Arg("signal", "The signal to send").Required().UintVar(&s.sig)
s.cmd.Arg("id", "The Job ID to signal").Required().StringVar(&s.id)
s.cmd.Flag("json", "Renders result in JSON format").UnNegatableBoolVar(&s.json)

s.fo = discovery.NewStandardOptions()
s.fo.AddFilterFlags(s.cmd)
s.fo.AddFlatFileFlags(s.cmd)
s.fo.AddSelectionFlags(s.cmd)
}

return nil
}

func (s *executorSignalCommand) Configure() (err error) {
return commonConfigure()
}

func (s *executorSignalCommand) Run(wg *sync.WaitGroup) (err error) {
defer wg.Done()

s.fo.SetDefaultsFromChoria(c)
log := c.Logger("executor")

opts := []executorclient.InitializationOption{
executorclient.Logger(log), executorclient.Discovery(executorclient.NewMetaNS(s.fo, true)),
}
if !s.json {
opts = append(opts, executorclient.Progress())
}

ec, err := executorclient.New(c, opts...)
if err != nil {
return err
}

res, err := ec.Signal(s.id, int64(s.sig)).Do(ctx)
if err != nil {
return err
}

format := executorclient.TextFormat
if s.json {
format = executorclient.JSONFormat
}
return res.RenderResults(os.Stdout, format, executorclient.DisplayDDL, debug, false, true, log)
}

0 comments on commit 1f979cd

Please sign in to comment.