[wip] daemon separation

This commit is contained in:
royalcat 2024-11-24 20:33:44 +03:00
parent 98ee1dc6f1
commit fa084118c3
48 changed files with 48 additions and 35 deletions

View file

@ -27,11 +27,11 @@ models:
Torrent: Torrent:
extraFields: extraFields:
T: T:
type: "*git.kmsign.ru/royalcat/tstor/src/daemons/torrent.Controller" type: "*git.kmsign.ru/royalcat/tstor/daemons/torrent.Controller"
TorrentFile: TorrentFile:
extraFields: extraFields:
F: F:
type: "*git.kmsign.ru/royalcat/tstor/src/daemons/torrent.FileController" type: "*git.kmsign.ru/royalcat/tstor/daemons/torrent.FileController"
TorrentPeer: TorrentPeer:
extraFields: extraFields:
F: F:
@ -45,7 +45,7 @@ models:
TorrentFS: TorrentFS:
extraFields: extraFields:
FS: FS:
type: "*git.kmsign.ru/royalcat/tstor/src/daemons/torrent.TorrentFS" type: "*git.kmsign.ru/royalcat/tstor/daemons/torrent.TorrentFS"
ResolverFS: ResolverFS:
extraFields: extraFields:
FS: FS:

View file

@ -6,6 +6,7 @@ COPY go.mod ./
COPY go.sum ./ COPY go.sum ./
RUN --mount=type=cache,mode=0777,target=/go/pkg/mod go mod download all RUN --mount=type=cache,mode=0777,target=/go/pkg/mod go mod download all
COPY ./daemons ./daemons
COPY ./pkg ./pkg COPY ./pkg ./pkg
COPY ./src ./src COPY ./src ./src
COPY ./cmd ./cmd COPY ./cmd ./cmd

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/fs"
"log/slog" "log/slog"
"net" "net"
@ -13,13 +14,13 @@ import (
"os/signal" "os/signal"
"syscall" "syscall"
"git.kmsign.ru/royalcat/tstor/daemons/qbittorrent"
"git.kmsign.ru/royalcat/tstor/daemons/ytdlp"
"git.kmsign.ru/royalcat/tstor/pkg/ctxbilly" "git.kmsign.ru/royalcat/tstor/pkg/ctxbilly"
wnfs "git.kmsign.ru/royalcat/tstor/pkg/go-nfs" wnfs "git.kmsign.ru/royalcat/tstor/pkg/go-nfs"
"git.kmsign.ru/royalcat/tstor/pkg/rlog" "git.kmsign.ru/royalcat/tstor/pkg/rlog"
"git.kmsign.ru/royalcat/tstor/src/config" "git.kmsign.ru/royalcat/tstor/src/config"
"git.kmsign.ru/royalcat/tstor/src/daemons" "git.kmsign.ru/royalcat/tstor/src/daemons"
"git.kmsign.ru/royalcat/tstor/src/daemons/qbittorrent"
"git.kmsign.ru/royalcat/tstor/src/daemons/ytdlp"
"git.kmsign.ru/royalcat/tstor/src/delivery" "git.kmsign.ru/royalcat/tstor/src/delivery"
"git.kmsign.ru/royalcat/tstor/src/telemetry" "git.kmsign.ru/royalcat/tstor/src/telemetry"
"git.kmsign.ru/royalcat/tstor/src/vfs" "git.kmsign.ru/royalcat/tstor/src/vfs"
@ -118,6 +119,10 @@ func run(configPath string) error {
return err return err
} }
vfs.Walk(ctx, sfs, "/", func(path string, info fs.FileInfo, err error) error {
return nil
})
if conf.Mounts.Fuse.Enabled { if conf.Mounts.Fuse.Enabled {
mh := fuse.NewHandler(conf.Mounts.Fuse.AllowOther, conf.Mounts.Fuse.Path) mh := fuse.NewHandler(conf.Mounts.Fuse.AllowOther, conf.Mounts.Fuse.Path)
err := mh.Mount(sfs) err := mh.Mount(sfs)

View file

@ -12,7 +12,7 @@ import (
"git.kmsign.ru/royalcat/tstor/pkg/rlog" "git.kmsign.ru/royalcat/tstor/pkg/rlog"
) )
func (d *Daemon) Cleanup(ctx context.Context, dryRun bool) ([]string, error) { func (d *Daemon) Cleanup(ctx context.Context, run bool) ([]string, error) {
d.log.Info(ctx, "cleanup started") d.log.Info(ctx, "cleanup started")
torrentInfos, err := d.client.qb.Torrent().GetTorrents(ctx, &qbittorrent.TorrentOption{}) torrentInfos, err := d.client.qb.Torrent().GetTorrents(ctx, &qbittorrent.TorrentOption{})
@ -37,7 +37,7 @@ func (d *Daemon) Cleanup(ctx context.Context, dryRun bool) ([]string, error) {
slog.Any("infohashes", torrentToDelete), slog.Any("infohashes", torrentToDelete),
) )
if dryRun { if !run {
d.log.Info(ctx, "dry run, skipping deletion") d.log.Info(ctx, "dry run, skipping deletion")
return torrentToDelete, nil return torrentToDelete, nil
} }

View file

@ -26,7 +26,7 @@ import (
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
) )
var trace = otel.Tracer("git.kmsign.ru/royalcat/tstor/src/daemons/qbittorrent") var trace = otel.Tracer("git.kmsign.ru/royalcat/tstor/daemons/qbittorrent")
type Daemon struct { type Daemon struct {
proc *os.Process proc *os.Process
@ -231,6 +231,9 @@ func (d *Daemon) syncTorrentState(ctx context.Context, file vfs.File, ih metainf
if err == nil { if err == nil {
break break
} }
if errors.Is(err, context.DeadlineExceeded) {
return err
}
log.Error(ctx, "waiting for torrent to be added", rlog.Error(err)) log.Error(ctx, "waiting for torrent to be added", rlog.Error(err))
time.Sleep(time.Millisecond * 15) time.Sleep(time.Millisecond * 15)
} }

View file

@ -1,5 +1,5 @@
type QBitTorrentDaemonMutation { type QBitTorrentDaemonMutation {
cleanup(dryRun: Boolean!): QBitCleanupResponse! @resolver cleanup(run: Boolean!): QBitCleanupResponse! @resolver
} }
type QBitCleanupResponse { type QBitCleanupResponse {

View file

@ -3,8 +3,8 @@ package daemons
import ( import (
"context" "context"
"git.kmsign.ru/royalcat/tstor/src/daemons/qbittorrent" "git.kmsign.ru/royalcat/tstor/daemons/qbittorrent"
"git.kmsign.ru/royalcat/tstor/src/daemons/ytdlp" "git.kmsign.ru/royalcat/tstor/daemons/ytdlp"
"git.kmsign.ru/royalcat/tstor/src/vfs" "git.kmsign.ru/royalcat/tstor/src/vfs"
) )

View file

@ -92,7 +92,7 @@ type ComplexityRoot struct {
} }
QBitTorrentDaemonMutation struct { QBitTorrentDaemonMutation struct {
Cleanup func(childComplexity int, dryRun bool) int Cleanup func(childComplexity int, run bool) int
} }
QBitTorrentDaemonQuery struct { QBitTorrentDaemonQuery struct {
@ -233,7 +233,7 @@ type MutationResolver interface {
DedupeStorage(ctx context.Context) (int64, error) DedupeStorage(ctx context.Context) (int64, error)
} }
type QBitTorrentDaemonMutationResolver interface { type QBitTorrentDaemonMutationResolver interface {
Cleanup(ctx context.Context, obj *model.QBitTorrentDaemonMutation, dryRun bool) (*model.QBitCleanupResponse, error) Cleanup(ctx context.Context, obj *model.QBitTorrentDaemonMutation, run bool) (*model.QBitCleanupResponse, error)
} }
type QBitTorrentDaemonQueryResolver interface { type QBitTorrentDaemonQueryResolver interface {
Torrents(ctx context.Context, obj *model.QBitTorrentDaemonQuery) ([]*model.QTorrent, error) Torrents(ctx context.Context, obj *model.QBitTorrentDaemonQuery) ([]*model.QTorrent, error)
@ -398,7 +398,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
return 0, false return 0, false
} }
return e.complexity.QBitTorrentDaemonMutation.Cleanup(childComplexity, args["dryRun"].(bool)), true return e.complexity.QBitTorrentDaemonMutation.Cleanup(childComplexity, args["run"].(bool)), true
case "QBitTorrentDaemonQuery.torrents": case "QBitTorrentDaemonQuery.torrents":
if e.complexity.QBitTorrentDaemonQuery.Torrents == nil { if e.complexity.QBitTorrentDaemonQuery.Torrents == nil {
@ -1105,7 +1105,7 @@ interface Progress {
total: Int! total: Int!
}`, BuiltIn: false}, }`, BuiltIn: false},
{Name: "../../../graphql/sources/qbittorrent_mutation.graphql", Input: `type QBitTorrentDaemonMutation { {Name: "../../../graphql/sources/qbittorrent_mutation.graphql", Input: `type QBitTorrentDaemonMutation {
cleanup(dryRun: Boolean!): QBitCleanupResponse! @resolver cleanup(run: Boolean!): QBitCleanupResponse! @resolver
} }
type QBitCleanupResponse { type QBitCleanupResponse {
@ -1384,28 +1384,28 @@ func (ec *executionContext) field_Mutation_uploadFile_argsFile(
func (ec *executionContext) field_QBitTorrentDaemonMutation_cleanup_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { func (ec *executionContext) field_QBitTorrentDaemonMutation_cleanup_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
var err error var err error
args := map[string]interface{}{} args := map[string]interface{}{}
arg0, err := ec.field_QBitTorrentDaemonMutation_cleanup_argsDryRun(ctx, rawArgs) arg0, err := ec.field_QBitTorrentDaemonMutation_cleanup_argsRun(ctx, rawArgs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
args["dryRun"] = arg0 args["run"] = arg0
return args, nil return args, nil
} }
func (ec *executionContext) field_QBitTorrentDaemonMutation_cleanup_argsDryRun( func (ec *executionContext) field_QBitTorrentDaemonMutation_cleanup_argsRun(
ctx context.Context, ctx context.Context,
rawArgs map[string]interface{}, rawArgs map[string]interface{},
) (bool, error) { ) (bool, error) {
// We won't call the directive if the argument is null. // We won't call the directive if the argument is null.
// Set call_argument_directives_with_null to true to call directives // Set call_argument_directives_with_null to true to call directives
// even if the argument is null. // even if the argument is null.
_, ok := rawArgs["dryRun"] _, ok := rawArgs["run"]
if !ok { if !ok {
var zeroVal bool var zeroVal bool
return zeroVal, nil return zeroVal, nil
} }
ctx = graphql.WithPathContext(ctx, graphql.NewPathWithField("dryRun")) ctx = graphql.WithPathContext(ctx, graphql.NewPathWithField("run"))
if tmp, ok := rawArgs["dryRun"]; ok { if tmp, ok := rawArgs["run"]; ok {
return ec.unmarshalNBoolean2bool(ctx, tmp) return ec.unmarshalNBoolean2bool(ctx, tmp)
} }
@ -2476,7 +2476,7 @@ func (ec *executionContext) _QBitTorrentDaemonMutation_cleanup(ctx context.Conte
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
directive0 := func(rctx context.Context) (interface{}, error) { directive0 := func(rctx context.Context) (interface{}, error) {
ctx = rctx // use context from middleware stack in children ctx = rctx // use context from middleware stack in children
return ec.resolvers.QBitTorrentDaemonMutation().Cleanup(rctx, obj, fc.Args["dryRun"].(bool)) return ec.resolvers.QBitTorrentDaemonMutation().Cleanup(rctx, obj, fc.Args["run"].(bool))
} }
directive1 := func(ctx context.Context) (interface{}, error) { directive1 := func(ctx context.Context) (interface{}, error) {

View file

@ -3,7 +3,7 @@ package model
import ( import (
"context" "context"
"git.kmsign.ru/royalcat/tstor/src/daemons/torrent" "git.kmsign.ru/royalcat/tstor/daemons/torrent"
"git.kmsign.ru/royalcat/tstor/src/vfs" "git.kmsign.ru/royalcat/tstor/src/vfs"
) )

View file

@ -3,7 +3,7 @@ package model
import ( import (
"context" "context"
"git.kmsign.ru/royalcat/tstor/src/daemons/torrent" "git.kmsign.ru/royalcat/tstor/daemons/torrent"
atorrent "github.com/anacrolix/torrent" atorrent "github.com/anacrolix/torrent"
) )

View file

@ -5,7 +5,7 @@ package model
import ( import (
"time" "time"
"git.kmsign.ru/royalcat/tstor/src/daemons/torrent" "git.kmsign.ru/royalcat/tstor/daemons/torrent"
"git.kmsign.ru/royalcat/tstor/src/vfs" "git.kmsign.ru/royalcat/tstor/src/vfs"
torrent1 "github.com/anacrolix/torrent" torrent1 "github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/types" "github.com/anacrolix/torrent/types"

View file

@ -12,8 +12,8 @@ import (
) )
// Cleanup is the resolver for the cleanup field. // Cleanup is the resolver for the cleanup field.
func (r *qBitTorrentDaemonMutationResolver) Cleanup(ctx context.Context, obj *model.QBitTorrentDaemonMutation, dryRun bool) (*model.QBitCleanupResponse, error) { func (r *qBitTorrentDaemonMutationResolver) Cleanup(ctx context.Context, obj *model.QBitTorrentDaemonMutation, run bool) (*model.QBitCleanupResponse, error) {
hahses, err := r.QBitTorrentDaemon.Cleanup(ctx, dryRun) hahses, err := r.QBitTorrentDaemon.Cleanup(ctx, run)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -1,8 +1,8 @@
package resolver package resolver
import ( import (
"git.kmsign.ru/royalcat/tstor/src/daemons/qbittorrent" "git.kmsign.ru/royalcat/tstor/daemons/qbittorrent"
"git.kmsign.ru/royalcat/tstor/src/daemons/torrent" "git.kmsign.ru/royalcat/tstor/daemons/torrent"
"git.kmsign.ru/royalcat/tstor/src/vfs" "git.kmsign.ru/royalcat/tstor/src/vfs"
"github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5"
) )

View file

@ -10,7 +10,7 @@ import (
"strings" "strings"
"time" "time"
"git.kmsign.ru/royalcat/tstor/src/daemons/torrent" "git.kmsign.ru/royalcat/tstor/daemons/torrent"
graph "git.kmsign.ru/royalcat/tstor/src/delivery/graphql" graph "git.kmsign.ru/royalcat/tstor/src/delivery/graphql"
"git.kmsign.ru/royalcat/tstor/src/delivery/graphql/model" "git.kmsign.ru/royalcat/tstor/src/delivery/graphql/model"
tinfohash "github.com/anacrolix/torrent/types/infohash" tinfohash "github.com/anacrolix/torrent/types/infohash"

View file

@ -5,10 +5,10 @@ import (
"log/slog" "log/slog"
"net/http" "net/http"
"git.kmsign.ru/royalcat/tstor/daemons/qbittorrent"
"git.kmsign.ru/royalcat/tstor/daemons/torrent"
"git.kmsign.ru/royalcat/tstor/pkg/rlog" "git.kmsign.ru/royalcat/tstor/pkg/rlog"
"git.kmsign.ru/royalcat/tstor/src/config" "git.kmsign.ru/royalcat/tstor/src/config"
"git.kmsign.ru/royalcat/tstor/src/daemons/qbittorrent"
"git.kmsign.ru/royalcat/tstor/src/daemons/torrent"
"git.kmsign.ru/royalcat/tstor/src/vfs" "git.kmsign.ru/royalcat/tstor/src/vfs"
echopprof "github.com/labstack/echo-contrib/pprof" echopprof "github.com/labstack/echo-contrib/pprof"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"

View file

@ -4,8 +4,8 @@ import (
"context" "context"
"net/http" "net/http"
"git.kmsign.ru/royalcat/tstor/src/daemons/qbittorrent" "git.kmsign.ru/royalcat/tstor/daemons/qbittorrent"
"git.kmsign.ru/royalcat/tstor/src/daemons/torrent" "git.kmsign.ru/royalcat/tstor/daemons/torrent"
graph "git.kmsign.ru/royalcat/tstor/src/delivery/graphql" graph "git.kmsign.ru/royalcat/tstor/src/delivery/graphql"
"git.kmsign.ru/royalcat/tstor/src/delivery/graphql/resolver" "git.kmsign.ru/royalcat/tstor/src/delivery/graphql/resolver"
"git.kmsign.ru/royalcat/tstor/src/vfs" "git.kmsign.ru/royalcat/tstor/src/vfs"

View file

@ -57,8 +57,12 @@ interface Progress {
current: Int! current: Int!
total: Int! total: Int!
} }
type QBitCleanupResponse {
count: Int!
hashes: [String!]!
}
type QBitTorrentDaemonMutation { type QBitTorrentDaemonMutation {
cleanup(dryRun: Boolean!): Int! @resolver cleanup(run: Boolean!): QBitCleanupResponse! @resolver
} }
type QBitTorrentDaemonQuery { type QBitTorrentDaemonQuery {
torrents: [QTorrent!]! @resolver torrents: [QTorrent!]! @resolver