Skip to content

Commit

Permalink
Rate Limiting.
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Ortel <[email protected]>
  • Loading branch information
jortel committed Dec 8, 2023
1 parent e1f9ae6 commit d7bcea0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
14 changes: 14 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/konveyor/tackle2-addon/command"
hub "github.com/konveyor/tackle2-hub/addon"
)

Expand All @@ -10,6 +11,19 @@ var (

func main() {
addon.Run(func() (err error) {
for n := -2; n < 2; n++ {
addon.Activity("VERBOSITY: %d", n)
cmd := command.Command{Path: "ps"}
cmd.Options.Add("-ef")
cmd.Reporter.Verbosity = n
err = cmd.Run()

addon.Activity("VERBOSITY: %d (Error)", n)
cmd = command.Command{Path: "ps"}
cmd.Options.Add("hello")
cmd.Reporter.Verbosity = n
err = cmd.Run()
}
return
})
}
6 changes: 3 additions & 3 deletions command/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func (r *Command) RunWith(ctx context.Context) (err error) {
} else {
r.Reporter.Succeeded(r.Path)
}
r.writer.End()
r.Reporter.End()
}()
cmd := exec.CommandContext(ctx, r.Path, r.Options...)
cmd.Dir = r.Dir
cmd.Stdout = &r.writer
cmd.Stderr = &r.writer
err = cmd.Start()
if err == nil {
defer r.writer.Close()
} else {
if err != nil {
return
}
err = cmd.Wait()
Expand Down
41 changes: 35 additions & 6 deletions command/reporter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package command

import "strings"
import (
"strings"
"fmt"
"time"
)

const (
// Disabled reports: NOTHING.
Expand All @@ -17,6 +21,7 @@ const (
// Reporter activity reporter.
type Reporter struct {
Verbosity int
queue chan string
}

//
Expand All @@ -27,7 +32,7 @@ func (r *Reporter) Run(path string, options Options) {
case Error:
case Default,
LiveOutput:
addon.Activity(
r.report(
"[CMD] Running: %s %s",
path,
strings.Join(options, " "))
Expand All @@ -42,7 +47,7 @@ func (r *Reporter) Succeeded(path string) {
case Error:
case Default,
LiveOutput:
addon.Activity("[CMD] %s succeeded.", path)
r.report("[CMD] %s succeeded.", path)
}
}

Expand All @@ -53,13 +58,13 @@ func (r *Reporter) Error(path string, err error, output []byte) {
case Disabled:
case Error,
Default:
addon.Activity(
r.report(
"[CMD] %s failed: %s.\n%s",
path,
err.Error(),
output)
case LiveOutput:
addon.Activity(
r.report(
"[CMD] %s failed: %s.",
path,
err.Error())
Expand All @@ -74,6 +79,30 @@ func (r *Reporter) Output(output string) {
case Error:
case Default:
case LiveOutput:
addon.Activity("> %s", output)
r.report("> %s", output)
}
}

//
// report activity in task Report.Activity.
// Rate limited.
func (r *Reporter) report(entry string, v ...interface{}) {
if r.queue == nil {
r.queue = make(chan string)
go func() {
for entry := range r.queue {
time.Sleep(time.Second)
addon.Activity(entry)
}
}()
}
r.queue <- fmt.Sprintf(entry, v...)
}

//
// End of reporting.
func (r *Reporter) End() {
if r.queue != nil {
close(r.queue)
}
}
8 changes: 5 additions & 3 deletions command/writer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package command

import "strings"
import (
"strings"
)

//
// Writer records command output.
Expand Down Expand Up @@ -28,9 +30,9 @@ func (w *Writer) Write(p []byte) (n int, err error) {
}

//
// Close the writer.
// End of writing.
// When verbosity > 0, output is reported in task Report.Activity.
func (w *Writer) Close() {
func (w *Writer) End() {
batch := string(w.buffer[w.index:])
if len(batch) > 0 {
w.reporter.Output(batch)
Expand Down

0 comments on commit d7bcea0

Please sign in to comment.