adding scheduler
This commit is contained in:
@@ -157,7 +157,7 @@ func (c *Client) rpc(ctx context.Context, body []byte) ([]byte, error) {
|
||||
|
||||
// torrentFields requested from Transmission (per AGENTS.md).
|
||||
var torrentFields = []string{
|
||||
"id", "name", "totalSize", "downloadedEver", "uploadedEver", "uploadRatio",
|
||||
"id", "hashString", "name", "totalSize", "downloadedEver", "uploadedEver", "uploadRatio",
|
||||
"percentDone", "status", "eta", "error", "errorString", "doneDate", "isFinished",
|
||||
"rateDownload", "rateUpload", "peersConnected", "peersGettingFromUs", "peersSendingToUs",
|
||||
"availability", "peers",
|
||||
@@ -283,6 +283,51 @@ func (c *Client) TorrentAction(ctx context.Context, method string, ids []int) er
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) SchedulerTorrents(ctx context.Context) ([]schedulerTorrent, error) {
|
||||
reqBody, _ := json.Marshal(rpcRequest{
|
||||
Method: "torrent-get",
|
||||
Arguments: rpcArgs{Fields: []string{"hashString", "name", "percentDone", "status", "uploadedEver", "error"}},
|
||||
})
|
||||
data, err := c.rpc(ctx, reqBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var resp struct {
|
||||
Result string `json:"result"`
|
||||
Args struct {
|
||||
Torrents []schedulerTorrent `json:"torrents"`
|
||||
} `json:"arguments"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.Result != "success" {
|
||||
return nil, fmt.Errorf("rpc result: %s", resp.Result)
|
||||
}
|
||||
return resp.Args.Torrents, nil
|
||||
}
|
||||
|
||||
func (c *Client) TorrentActionHash(ctx context.Context, method, hash string) error {
|
||||
reqBody, _ := json.Marshal(rpcRequest{
|
||||
Method: method,
|
||||
Arguments: struct {
|
||||
IDs []string `json:"ids"`
|
||||
}{IDs: []string{hash}},
|
||||
})
|
||||
data, err := c.rpc(ctx, reqBody)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var resp rpcResponse
|
||||
if err := json.Unmarshal(data, &resp); err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.Result != "success" {
|
||||
return fmt.Errorf("rpc result: %s", resp.Result)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Check(ctx context.Context) error {
|
||||
_, err := c.Stats(ctx)
|
||||
return err
|
||||
@@ -559,6 +604,14 @@ func main() {
|
||||
} else {
|
||||
log.Printf("transmission RPC target: %s", redactedURL(client.rpcURL))
|
||||
}
|
||||
scheduler, err := NewScheduler(client, os.Getenv("SCHEDULER_DB_PATH"))
|
||||
if err != nil {
|
||||
log.Fatalf("scheduler database: %v", err)
|
||||
}
|
||||
defer scheduler.Close()
|
||||
if client != nil {
|
||||
go scheduler.Run(context.Background())
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
@@ -607,6 +660,10 @@ func main() {
|
||||
writeJSON(w, args)
|
||||
})
|
||||
mux.HandleFunc("/api/session/alternative-speed", handleAlternativeSpeed(client, setupErr))
|
||||
mux.HandleFunc("/api/scheduler", scheduler.handleState)
|
||||
mux.HandleFunc("/api/scheduler/settings", scheduler.handleSettings)
|
||||
mux.HandleFunc("/api/scheduler/history", scheduler.handleHistory)
|
||||
mux.HandleFunc("/api/scheduler/torrents/", scheduler.handleOptIn)
|
||||
|
||||
// Static files from embedded web/ directory
|
||||
webRoot, err := fs.Sub(webFS, "web")
|
||||
|
||||
Reference in New Issue
Block a user