Skip to content

Commit

Permalink
Merge pull request #171 from sonroyaalmerol/ws-response
Browse files Browse the repository at this point in the history
add response handler for mount handler
  • Loading branch information
sonroyaalmerol authored Feb 17, 2025
2 parents 31da1cb + eaba3e0 commit 7499048
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
7 changes: 6 additions & 1 deletion internal/backend/mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/sonroyaalmerol/pbs-plus/internal/store/constants"
"github.com/sonroyaalmerol/pbs-plus/internal/store/proxmox"
"github.com/sonroyaalmerol/pbs-plus/internal/store/types"
"github.com/sonroyaalmerol/pbs-plus/internal/syslog"
"github.com/sonroyaalmerol/pbs-plus/internal/utils"
)

Expand Down Expand Up @@ -168,10 +169,14 @@ func (a *AgentMount) CloseMount() {
targetHostnameEnc := base32.StdEncoding.EncodeToString([]byte(a.Hostname))
agentDriveEnc := base32.StdEncoding.EncodeToString([]byte(a.Drive))

_ = proxmox.Session.ProxmoxHTTPRequest(
syslog.L.Infof("CloseMount: Sending request for %s/%s", a.Hostname, a.Drive)
err := proxmox.Session.ProxmoxHTTPRequest(
http.MethodDelete,
fmt.Sprintf("https://localhost:8008/plus/mount/%s/%s", targetHostnameEnc, agentDriveEnc),
nil,
nil,
)
if err != nil {
syslog.L.Errorf("CloseMount: error sending unmount request -> %v", err)
}
}
42 changes: 41 additions & 1 deletion internal/proxy/controllers/plus/plus.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/sonroyaalmerol/pbs-plus/internal/store"
"github.com/sonroyaalmerol/pbs-plus/internal/syslog"
"github.com/sonroyaalmerol/pbs-plus/internal/utils"
"github.com/sonroyaalmerol/pbs-plus/internal/websockets"
)
Expand Down Expand Up @@ -80,10 +81,49 @@ func MountHandler(storeInstance *store.Store) http.HandlerFunc {
}

if r.Method == http.MethodDelete {
_ = storeInstance.WSHub.SendToClient(targetHostname, websockets.Message{
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

// Create response channel and register handler
respChan := make(chan *websockets.Message, 1)
errChan := make(chan *websockets.Message, 1)
cleanup := storeInstance.WSHub.RegisterHandler("response-backup_close", func(handlerCtx context.Context, msg *websockets.Message) error {
if msg.Content == "Acknowledged: "+agentDrive && msg.ClientID == targetHostname {
respChan <- msg
}
return nil
})
defer cleanup()
cleanupErr := storeInstance.WSHub.RegisterHandler("error-backup_close", func(handlerCtx context.Context, msg *websockets.Message) error {
if strings.Contains(msg.Content, agentDrive+": ") && msg.ClientID == targetHostname {
errChan <- msg
}
return nil
})
defer cleanupErr()

err := storeInstance.WSHub.SendToClient(targetHostname, websockets.Message{
Type: "backup_close",
Content: agentDrive,
})
if err != nil {
syslog.L.Errorf("MountHandler: Failed to send backup request to target -> %v", err)
http.Error(w, fmt.Sprintf("MountHandler: Failed to send backup request to target -> %v", err), http.StatusInternalServerError)
return
}

// Wait for either response or timeout
select {
case <-respChan:
case err := <-errChan:
syslog.L.Errorf("MountHandler: %s", err.Content)
http.Error(w, fmt.Sprintf("MountHandler: %s", err.Content), http.StatusInternalServerError)
return
case <-ctx.Done():
syslog.L.Error("MountHandler: Timeout waiting for backup acknowledgement from target")
http.Error(w, "MountHandler: Timeout waiting for backup acknowledgement from target", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "true"})
Expand Down

0 comments on commit 7499048

Please sign in to comment.