Skip to content

Commit

Permalink
✨ Add raw methods for error and activity reporting by addons. (#457)
Browse files Browse the repository at this point in the history
Add _raw_ methods used by addons to report both errors and activity. The
reason is to reduce the number of API calls when reporting large
numbers.
For example, the _addon.command_ package reports stdout/stderr for
failed commands which can be hundreds (or thousands of lines). Calling
Activity() hundreds of times result in hundreds of API calls.
Another example is the reported analyzer errors.

Another aspect is to formalize multi-line activity. This PR codifies the
convention of:
```
- This is a list of:
- > One
- > Two
- > Three
```
and, simplifies the API.

The tackle2-addon _command_ package will need to be updated once this
merges to use RawActivity() for stdout/stderr. reporting.
The tackle2-addon-analyzer will need to updated to use both methods in
select places as well.

---------

Signed-off-by: Jeff Ortel <jortel@redhat.com>
  • Loading branch information
jortel authored Jul 25, 2023
1 parent 533bd55 commit d1683e4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
1 change: 1 addition & 0 deletions addon/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (h *Adapter) Run(addon func() error) {
}()
//
// Report addon started.
h.Load()
h.Started()
//
// Run addon.
Expand Down
75 changes: 57 additions & 18 deletions addon/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/konveyor/tackle2-hub/api"
"github.com/konveyor/tackle2-hub/binding"
"github.com/konveyor/tackle2-hub/task"
"gopkg.in/yaml.v2"
"strings"
)

//
Expand Down Expand Up @@ -67,7 +69,6 @@ func (h *Task) Variant() string {
//
// Started report addon started.
func (h *Task) Started() {
h.Load()
h.deleteReport()
h.report.Status = task.Running
h.pushReport()
Expand All @@ -88,8 +89,8 @@ func (h *Task) Succeeded() {
//
// Failed report addon failed.
// The reason can be a printf style format.
func (h *Task) Failed(reason string, x ...interface{}) {
reason = fmt.Sprintf(reason, x...)
func (h *Task) Failed(reason string, v ...interface{}) {
reason = fmt.Sprintf(reason, v...)
h.report.Status = task.Failed
h.report.Errors = append(
h.report.Errors,
Expand All @@ -108,32 +109,70 @@ func (h *Task) Failed(reason string, x ...interface{}) {
//
// Error report addon error.
// The description can be a printf style format.
func (h *Task) Error(severity, description string, x ...interface{}) {
h.report.Status = task.Failed
description = fmt.Sprintf(description, x...)
h.report.Errors = append(
h.report.Errors,
func (h *Task) Error(severity, description string, v ...interface{}) {
description = fmt.Sprintf(description, v...)
h.RawError(
api.TaskError{
Severity: severity,
Description: description,
})
h.pushReport()
return
}

//
// Activity report addon activity.
// The description can be a printf style format.
func (h *Task) Activity(entry string, x ...interface{}) {
entry = fmt.Sprintf(entry, x...)
h.report.Activity = append(
h.report.Activity,
entry)
func (h *Task) Activity(entry string, v ...interface{}) {
entry = fmt.Sprintf(entry, v...)
h.RawActivity(entry)
return
}

//
// RawError report addon error.
func (h *Task) RawError(error ...api.TaskError) {
h.report.Status = task.Failed
for i := range error {
h.report.Errors = append(
h.report.Errors,
error[i])
Log.Info(
"Addon reported: error.",
"error",
error[i])
}
h.pushReport()
return
}

//
// RawActivity report addon activity.
func (h *Task) RawActivity(object interface{}) {
switch object.(type) {
case string:
s := object.(string)
h.RawActivity(strings.Split(s, "\n"))
case []string:
prefix := ""
list := object.([]string)
if len(list) > 1 {
prefix = "> "
}
for i := range list {
entry := prefix + list[i]
h.report.Activity = append(
h.report.Activity,
entry)
Log.Info(
"Addon reported: activity.",
"object",
entry)
}
default:
b, _ := yaml.Marshal(object)
h.RawActivity(string(b))
}
h.pushReport()
Log.Info(
"Addon reported: activity.",
"activity",
h.report.Activity)
return
}

Expand Down
2 changes: 0 additions & 2 deletions hack/cmd/addon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ type SoftError = hub.SoftError
// main
func main() {
addon.Run(func() (err error) {
_, _ = addon.Identity.List()
_, _ = addon.Identity.Get(1)
//
// Get the addon data associated with the task.
d := &Data{}
Expand Down

0 comments on commit d1683e4

Please sign in to comment.