Skip to content
Open
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
37 changes: 37 additions & 0 deletions internal/cmd/subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"watcloud-cli/internal/subscription"
)

var subscriptionCmd = &cobra.Command{
Use: "subscription [job_id] [email]",
Short: "Get an email notification when a SLURM job finishes",
Long: "Subscribe to a specific SLURM job by its ID. When the job completes, you will receive an email notification.",

Args: cobra.ExactArgs(2),

Run: func(cmd *cobra.Command, args []string) {
jobID := args[0]
email := args[1]

fmt.Printf("Attempting to subscribe %s to job %s...\n", email, jobID)
err := subscription.SubscribeToJobAPI(jobID, email)

if err != nil {
fmt.Printf("Failed to subscribe to job: %v\n", err)
} else {
fmt.Printf("Success! %s You will be emailed when job %s completes \n", email, jobID)
}
},
}

func init() {
rootCmd.AddCommand(subscriptionCmd)
}

// func subscribeToJobAPI(jobID string) error {
// return nil
// }
51 changes: 51 additions & 0 deletions internal/subscription/subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package subscription

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)

type JobSubscriptionData struct {
JobID string `json:"job_id"`
Email string `json:"email"`
}

func SubscribeToJobAPI(jobID string, email string) error {

// Create data package
payload := JobSubscriptionData{
JobID: jobID,
Email: email,
}

jsonData, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("failed to format data: %v", err)
}

apiURL := "http://localhost:8080/subscribe"

req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to create request: %v", err)
}

req.Header.Set("Content-Type", "application/json")

client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("network error: %v", err)
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("server rejected the request with status: %d", resp.StatusCode)
}

return nil
}