Add projects
This commit is contained in:
parent
2d3a9ad623
commit
8b607dd700
1802 changed files with 503346 additions and 2 deletions
42
backend/internal/home/plugin_status.go
Normal file
42
backend/internal/home/plugin_status.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
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)
|
||||
}
|
||||
Loading…
Reference in a new issue