Files
CLIProxyAPI/internal/home/plugin_status.go
Luis Pater 3a13865db8 refactor(home): relocate and rename home plugin status reporting logic
- Moved `reportHomePluginStatus` from `cmd/server` to `internal/home` as `ReportPluginStatus`.
- Updated interface and variable names for consistency.
- Adjusted imports and references in `main.go` and test files to align with the changes.
- Improved code structure for better modularity and reuse.
2026-06-23 23:27:11 +08:00

43 lines
1.2 KiB
Go

package home
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/router-for-me/CLIProxyAPI/v7/internal/homeplugins"
)
const pluginStatusReportTimeout = 10 * time.Second
// PluginStatusClient defines the interface for pushing plugin status reports.
type PluginStatusClient interface {
RPushPluginStatus(ctx context.Context, payload []byte) error
}
// ReportPluginStatus marshals the given report, sets NodeID and UpdatedAt,
// and pushes it to the provided client with a timeout.
func ReportPluginStatus(ctx context.Context, client PluginStatusClient, nodeID string, report homeplugins.SyncReport) error {
if client == nil {
return fmt.Errorf("home plugin status client is unavailable")
}
nodeID = strings.TrimSpace(nodeID)
if nodeID == "" {
return fmt.Errorf("home plugin status node id is empty")
}
report.NodeID = nodeID
report.UpdatedAt = time.Now().UTC()
raw, errMarshal := json.Marshal(report)
if errMarshal != nil {
return errMarshal
}
if ctx == nil {
ctx = context.Background()
}
reportCtx, cancel := context.WithTimeout(ctx, pluginStatusReportTimeout)
defer cancel()
return client.RPushPluginStatus(reportCtx, raw)
}