diff --git a/cmd/decypharr/main.go b/cmd/decypharr/main.go index 30d5f72..2bfa3e9 100644 --- a/cmd/decypharr/main.go +++ b/cmd/decypharr/main.go @@ -75,7 +75,7 @@ func Start(ctx context.Context) error { done := make(chan struct{}) go func(ctx context.Context) { - if err := startServices(ctx, wd, srv); err != nil { + if err := startServices(ctx, cancelSvc, wd, srv); err != nil { _log.Error().Err(err).Msg("Error starting services") cancelSvc() } @@ -107,7 +107,7 @@ func Start(ctx context.Context) error { } } -func startServices(ctx context.Context, wd *webdav.WebDav, srv *server.Server) error { +func startServices(ctx context.Context, cancelSvc context.CancelFunc, wd *webdav.WebDav, srv *server.Server) error { var wg sync.WaitGroup errChan := make(chan error) @@ -177,7 +177,11 @@ func startServices(ctx context.Context, wd *webdav.WebDav, srv *server.Server) e for err := range errChan { if err != nil { _log.Error().Err(err).Msg("Service error detected") - // Don't shut down the whole app + // If the error is critical, return it to stop the main loop + if ctx.Err() == nil { + _log.Error().Msg("Stopping services due to error") + cancelSvc() // Cancel the service context to stop all services + } } } }() diff --git a/internal/config/config.go b/internal/config/config.go index f0c10db..a28c62e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -10,7 +10,6 @@ import ( "runtime" "strings" "sync" - "time" ) var ( @@ -78,20 +77,20 @@ type Config struct { URLBase string `json:"url_base,omitempty"` Port string `json:"port,omitempty"` - LogLevel string `json:"log_level,omitempty"` - Debrids []Debrid `json:"debrids,omitempty"` - QBitTorrent QBitTorrent `json:"qbittorrent,omitempty"` - Arrs []Arr `json:"arrs,omitempty"` - Repair Repair `json:"repair,omitempty"` - WebDav WebDav `json:"webdav,omitempty"` - AllowedExt []string `json:"allowed_file_types,omitempty"` - MinFileSize string `json:"min_file_size,omitempty"` // Minimum file size to download, 10MB, 1GB, etc - MaxFileSize string `json:"max_file_size,omitempty"` // Maximum file size to download (0 means no limit) - Path string `json:"-"` // Path to save the config file - UseAuth bool `json:"use_auth,omitempty"` - Auth *Auth `json:"-"` - DiscordWebhook string `json:"discord_webhook_url,omitempty"` - RemoveStalledAfter time.Duration `json:"remove_stalled_after,omitempty"` + LogLevel string `json:"log_level,omitempty"` + Debrids []Debrid `json:"debrids,omitempty"` + QBitTorrent QBitTorrent `json:"qbittorrent,omitempty"` + Arrs []Arr `json:"arrs,omitempty"` + Repair Repair `json:"repair,omitempty"` + WebDav WebDav `json:"webdav,omitempty"` + AllowedExt []string `json:"allowed_file_types,omitempty"` + MinFileSize string `json:"min_file_size,omitempty"` // Minimum file size to download, 10MB, 1GB, etc + MaxFileSize string `json:"max_file_size,omitempty"` // Maximum file size to download (0 means no limit) + Path string `json:"-"` // Path to save the config file + UseAuth bool `json:"use_auth,omitempty"` + Auth *Auth `json:"-"` + DiscordWebhook string `json:"discord_webhook_url,omitempty"` + RemoveStalledAfter string `json:"remove_stalled_after,omitzero"` } func (c *Config) JsonFile() string { diff --git a/pkg/debrid/store/repair.go b/pkg/debrid/store/repair.go index 53cd0ab..6d7875e 100644 --- a/pkg/debrid/store/repair.go +++ b/pkg/debrid/store/repair.go @@ -258,6 +258,7 @@ func (c *Cache) reInsertTorrent(ct *CachedTorrent) (*CachedTorrent, error) { } func (c *Cache) resetInvalidLinks() { + c.logger.Debug().Msgf("Resetting accounts") c.invalidDownloadLinks = sync.Map{} c.client.Accounts().Reset() // Reset the active download keys } diff --git a/pkg/store/store.go b/pkg/store/store.go index 99226c3..28dd752 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -40,16 +40,21 @@ func Get() *Store { qbitCfg := cfg.QBitTorrent instance = &Store{ - repair: repair.New(arrs, deb), - arr: arrs, - debrid: deb, - torrents: newTorrentStorage(cfg.TorrentsFile()), - logger: logger.Default(), // Use default logger [decypharr] - refreshInterval: time.Duration(cmp.Or(qbitCfg.RefreshInterval, 10)) * time.Minute, - skipPreCache: qbitCfg.SkipPreCache, - downloadSemaphore: make(chan struct{}, cmp.Or(qbitCfg.MaxDownloads, 5)), - importsQueue: NewImportQueue(context.Background(), 1000), - removeStalledAfter: cfg.RemoveStalledAfter, + repair: repair.New(arrs, deb), + arr: arrs, + debrid: deb, + torrents: newTorrentStorage(cfg.TorrentsFile()), + logger: logger.Default(), // Use default logger [decypharr] + refreshInterval: time.Duration(cmp.Or(qbitCfg.RefreshInterval, 10)) * time.Minute, + skipPreCache: qbitCfg.SkipPreCache, + downloadSemaphore: make(chan struct{}, cmp.Or(qbitCfg.MaxDownloads, 5)), + importsQueue: NewImportQueue(context.Background(), 1000), + } + if cfg.RemoveStalledAfter != "" { + removeStalledAfter, err := time.ParseDuration(cfg.RemoveStalledAfter) + if err == nil { + instance.removeStalledAfter = removeStalledAfter + } } }) return instance