Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback command #499

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions cmd/callbacks/callbacks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package callbacks

import (
"github.com/resonatehq/resonate/pkg/client"
"github.com/spf13/cobra"
)

func NewCmd() *cobra.Command {
var (
c = client.New()
server string
username string
password string
)

cmd := &cobra.Command{
Use: "callbacks",
Aliases: []string{"callback"},
Short: "Resonate callbacks",
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if username != "" || password != "" {
c.SetBasicAuth(username, password)
}

Check warning on line 26 in cmd/callbacks/callbacks.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/callbacks.go#L8-L26

Added lines #L8 - L26 were not covered by tests

return c.Setup(server)

Check warning on line 28 in cmd/callbacks/callbacks.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/callbacks.go#L28

Added line #L28 was not covered by tests
},
}

// Add subcommands
cmd.AddCommand(CreateCallbackCmd(c))

// Flags
cmd.PersistentFlags().StringVarP(&server, "server", "", "http://localhost:8001", "resonate url")
cmd.PersistentFlags().StringVarP(&username, "username", "U", "", "basic auth username")
cmd.PersistentFlags().StringVarP(&password, "password", "P", "", "basic auth password")

return cmd

Check warning on line 40 in cmd/callbacks/callbacks.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/callbacks.go#L33-L40

Added lines #L33 - L40 were not covered by tests
}
95 changes: 95 additions & 0 deletions cmd/callbacks/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package callbacks

import (
"context"
"encoding/json"
"errors"
"time"

"github.com/resonatehq/resonate/pkg/client"
v1 "github.com/resonatehq/resonate/pkg/client/v1"
"github.com/spf13/cobra"
)

var createCallbacksExample = `
# Create a callback
resonate callback create foo --promise-id bar --root-promise-id baz --timeout 1h --recv default

# Create a callback with url
resonate callback create foo --promise-id bar --root-promise-id baz --timeout 1h --recv poll://default/6fa89b7e-4a56-40e8-ba4e-78864caa3278

# Create a callback with object
resonate callback create foo --promise-id bar --root-promise-id baz --timeout 1h --recv {"type": "poll", "data": {"group": "default", "id": "6fa89b7e-4a56-40e8-ba4e-78864caa3278"}}
`

func CreateCallbackCmd(c client.Client) *cobra.Command {
var (
promiseId string
rootPromiseId string
timeout time.Duration
recvStr string
)
cmd := &cobra.Command{
Use: "create <id>",
Short: "Create callback",
Example: createCallbacksExample,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("must specify an id")
}

Check warning on line 39 in cmd/callbacks/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/create.go#L25-L39

Added lines #L25 - L39 were not covered by tests

id := args[0]

var recv v1.Recv

if json.Valid([]byte(recvStr)) {
var recv0 v1.Recv0

if err := json.Unmarshal([]byte(recvStr), &recv0); err != nil {
return err
}
if err := recv.FromRecv0(recv0); err != nil {
return err
}
} else {
if err := recv.FromRecv1(recvStr); err != nil {
return err
}

Check warning on line 57 in cmd/callbacks/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/create.go#L41-L57

Added lines #L41 - L57 were not covered by tests
}

body := v1.CreateCallbackJSONRequestBody{
Id: id,
PromiseId: promiseId,
RootPromiseId: rootPromiseId,
Timeout: time.Now().Add(timeout).UnixMilli(),
Recv: recv,
}

res, err := c.V1().CreateCallbackWithResponse(context.TODO(), nil, body)
if err != nil {
return err
}

Check warning on line 71 in cmd/callbacks/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/create.go#L60-L71

Added lines #L60 - L71 were not covered by tests

if res.StatusCode() == 201 {
cmd.Printf("Created callback: %s\n", id)
} else if res.StatusCode() == 200 {
cmd.Printf("Created callback: %s (deduplicated)\n", id)
} else {
cmd.PrintErrln(res.Status(), string(res.Body))
}

Check warning on line 79 in cmd/callbacks/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/create.go#L73-L79

Added lines #L73 - L79 were not covered by tests

return nil

Check warning on line 81 in cmd/callbacks/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/create.go#L81

Added line #L81 was not covered by tests
},
}

cmd.Flags().StringVar(&promiseId, "promise-id", "", "promise id")
cmd.Flags().StringVar(&rootPromiseId, "root-promise-id", "", "root promise id")
cmd.Flags().DurationVar(&timeout, "timeout", 0, "task timeout")
cmd.Flags().StringVar(&recvStr, "recv", "default", "task receiver")

_ = cmd.MarkFlagRequired("promise-id")
_ = cmd.MarkFlagRequired("root-promise-id")
_ = cmd.MarkFlagRequired("timeout")

return cmd

Check warning on line 94 in cmd/callbacks/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/create.go#L85-L94

Added lines #L85 - L94 were not covered by tests
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"

"github.com/resonatehq/resonate/cmd/callbacks"
"github.com/resonatehq/resonate/cmd/dst"
"github.com/resonatehq/resonate/cmd/promises"
"github.com/resonatehq/resonate/cmd/quickstart"
Expand Down Expand Up @@ -39,6 +40,7 @@ func init() {
rootCmd.AddCommand(serve.ServeCmd())
rootCmd.AddCommand(quickstart.NewCmd())
rootCmd.AddCommand(tasks.NewCmd())
rootCmd.AddCommand(callbacks.NewCmd())

// Set default output
rootCmd.SetOut(os.Stdout)
Expand Down
8 changes: 4 additions & 4 deletions cmd/tasks/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

// Example command usage for claiming a task
var claimTasksExample = `
# Claim a task
# Claim a task
resonate tasks claim foo --counter 1 --process-id bar --ttl 1m`

// ClaimTaskCmd returns a cobra command for claiming a task.
Expand Down Expand Up @@ -74,9 +74,9 @@
}

// Define command flags
cmd.Flags().IntVarP(&counter, "counter", "c", 0, "The task counter")
cmd.Flags().StringVarP(&processId, "process-id", "p", "", "Unique process ID that identifies the claimer")
cmd.Flags().DurationVarP(&ttl, "ttl", "t", 0, "Time to live in milliseconds")
cmd.Flags().IntVarP(&counter, "counter", "c", 0, "task counter")
cmd.Flags().StringVarP(&processId, "process-id", "p", "", "unique id that identifies the claimer")
cmd.Flags().DurationVarP(&ttl, "ttl", "t", 0, "task time to live")

Check warning on line 79 in cmd/tasks/claim.go

View check run for this annotation

Codecov / codecov/patch

cmd/tasks/claim.go#L77-L79

Added lines #L77 - L79 were not covered by tests

// Mark flags as required
_ = cmd.MarkFlagRequired("id")
Expand Down
4 changes: 2 additions & 2 deletions cmd/tasks/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// Example command usage for completing a task
var completeTasksExample = `
# Complete a task
# Complete a task
resonate tasks complete foo --counter 1`

// CompleteTaskCmd returns a cobra command for completing a task.
Expand Down Expand Up @@ -63,7 +63,7 @@
}

// Define command flags
cmd.Flags().IntVarP(&counter, "counter", "c", 0, "The task counter")
cmd.Flags().IntVarP(&counter, "counter", "c", 0, "task counter")

Check warning on line 66 in cmd/tasks/complete.go

View check run for this annotation

Codecov / codecov/patch

cmd/tasks/complete.go#L66

Added line #L66 was not covered by tests

// Mark flags as required
_ = cmd.MarkFlagRequired("id")
Expand Down
8 changes: 4 additions & 4 deletions cmd/tasks/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// Example command usage for sending a heartbeat to a task
var heartbeatTasksExample = `
# Heartbeat a task
# Heartbeat a task
resonate tasks heartbeat --process-id foo`

// HeartbeatTaskCmd returns a cobra command for sending a heartbeat to a task.
Expand All @@ -22,7 +22,7 @@
// Define the cobra command
cmd := &cobra.Command{
Use: "heartbeat",
Short: "Send a heartbeat to a task",
Short: "Heartbeat tasks",

Check warning on line 25 in cmd/tasks/heartbeat.go

View check run for this annotation

Codecov / codecov/patch

cmd/tasks/heartbeat.go#L25

Added line #L25 was not covered by tests
Example: heartbeatTasksExample,
RunE: func(cmd *cobra.Command, args []string) error {
// Create parameters for the request
Expand All @@ -42,7 +42,7 @@

// Handle the response based on the status code
if res.StatusCode() == 200 {
cmd.Printf("Heartbeat tasks for process-id: %s\n", processId)
cmd.Printf("Tasks heartbeated: %d\n", *res.JSON200.TasksAffected)

Check warning on line 45 in cmd/tasks/heartbeat.go

View check run for this annotation

Codecov / codecov/patch

cmd/tasks/heartbeat.go#L45

Added line #L45 was not covered by tests
} else {
cmd.PrintErrln(res.Status(), string(res.Body))
return nil
Expand All @@ -53,7 +53,7 @@
}

// Define command flags
cmd.Flags().StringVarP(&processId, "process-id", "p", "", "Unique process ID to heartbeat tasks")
cmd.Flags().StringVarP(&processId, "process-id", "p", "", "unique id that identifies the claimer")

Check warning on line 56 in cmd/tasks/heartbeat.go

View check run for this annotation

Codecov / codecov/patch

cmd/tasks/heartbeat.go#L56

Added line #L56 was not covered by tests

// Mark flags as required
_ = cmd.MarkFlagRequired("process-id")
Expand Down