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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ Run:
| start/run | Starts the rootless Docker Daemon. |
| status | Lists all non-interactive background user processes (daemons). |

### watcloud subscription <job_id> [email]

Get notified when a SLURM job finishes.

| Usage | Description |
|-------|-------------|
| `watcloud subscription <job_id> <email>` | Email notification when the job completes |
| `watcloud subscription <job_id> --discord <webhook_url>` | Discord notification via a webhook URL |

To get a Discord webhook URL, in your Discord channel: **Edit Channel → Integrations → Webhooks → New Webhook → Copy Webhook URL**.

---

For help and usage examples, run:
Expand Down
37 changes: 27 additions & 10 deletions internal/cmd/subscription.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
package cmd

import (
import (
"fmt"

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

var discordWebhook string

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),
Short: "Get notified when a SLURM job finishes",
Long: "Subscribe to a specific SLURM job by its ID. When the job completes you will be " +
"notified by email, or on Discord with --discord <webhook_url>.",

Args: cobra.RangeArgs(1, 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)
var channel, target string
switch {
case discordWebhook != "":
channel = "discord"
target = discordWebhook
case len(args) == 2:
channel = "email"
target = args[1]
default:
fmt.Println("Error: provide an email address or use --discord <webhook_url>")
return
}

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

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)
} else {
fmt.Printf("Success! You will be notified via %s when job %s completes.\n", channel, jobID)
}
},
}

func init() {
subscriptionCmd.Flags().StringVar(&discordWebhook, "discord", "", "Discord webhook URL to notify instead of email")
rootCmd.AddCommand(subscriptionCmd)
}
23 changes: 14 additions & 9 deletions internal/subscription/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ import (
)

type JobSubscriptionData struct {
JobID string `json:"job_id"`
Email string `json:"email"`
JobID string `json:"job_id"`
Channel string `json:"channel"`
Target string `json:"target"`
}

func SubscribeToJobAPI(jobID string, email string) error {

// SubscribeToJobAPI registers a subscription so the user is notified when the
// SLURM job finishes. channel is "email" or "discord"; target is the email
// address or the Discord webhook URL respectively.
func SubscribeToJobAPI(jobID string, target string, channel string) error {

// Create data package
payload := JobSubscriptionData{
JobID: jobID,
Email: email,
JobID: jobID,
Channel: channel,
Target: target,
}

jsonData, err := json.Marshal(payload)
Expand All @@ -32,7 +37,7 @@ func SubscribeToJobAPI(jobID string, email string) error {
if err != nil {
return fmt.Errorf("failed to create request: %v", err)
}

req.Host = "slurm-email-monitor.cluster.watonomous.ca"
req.Header.Set("Content-Type", "application/json")

Expand All @@ -41,12 +46,12 @@ func SubscribeToJobAPI(jobID string, email string) error {
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
}
}