Fix saveTofile; Add a global panic, Add a recoverer for everything functions
This commit is contained in:
@@ -2,6 +2,7 @@ package decypharr
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"github.com/sirrobot01/debrid-blackhole/internal/config"
|
"github.com/sirrobot01/debrid-blackhole/internal/config"
|
||||||
"github.com/sirrobot01/debrid-blackhole/internal/logger"
|
"github.com/sirrobot01/debrid-blackhole/internal/logger"
|
||||||
"github.com/sirrobot01/debrid-blackhole/pkg/proxy"
|
"github.com/sirrobot01/debrid-blackhole/pkg/proxy"
|
||||||
@@ -11,7 +12,7 @@ import (
|
|||||||
"github.com/sirrobot01/debrid-blackhole/pkg/version"
|
"github.com/sirrobot01/debrid-blackhole/pkg/version"
|
||||||
"github.com/sirrobot01/debrid-blackhole/pkg/web"
|
"github.com/sirrobot01/debrid-blackhole/pkg/web"
|
||||||
"github.com/sirrobot01/debrid-blackhole/pkg/worker"
|
"github.com/sirrobot01/debrid-blackhole/pkg/worker"
|
||||||
"log"
|
"runtime/debug"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,41 +37,51 @@ func Start(ctx context.Context) error {
|
|||||||
srv.Mount("/", webRoutes)
|
srv.Mount("/", webRoutes)
|
||||||
srv.Mount("/api/v2", qbitRoutes)
|
srv.Mount("/api/v2", qbitRoutes)
|
||||||
|
|
||||||
if cfg.Proxy.Enabled {
|
safeGo := func(f func() error) {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
if err := proxy.NewProxy().Start(ctx); err != nil {
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
stack := debug.Stack()
|
||||||
|
_log.Error().
|
||||||
|
Interface("panic", r).
|
||||||
|
Str("stack", string(stack)).
|
||||||
|
Msg("Recovered from panic in goroutine")
|
||||||
|
|
||||||
|
// Send error to channel so the main goroutine is aware
|
||||||
|
errChan <- fmt.Errorf("panic: %v", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := f(); err != nil {
|
||||||
errChan <- err
|
errChan <- err
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Add(1)
|
if cfg.Proxy.Enabled {
|
||||||
go func() {
|
safeGo(func() error {
|
||||||
defer wg.Done()
|
return proxy.NewProxy().Start(ctx)
|
||||||
if err := srv.Start(ctx); err != nil {
|
})
|
||||||
errChan <- err
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}()
|
safeGo(func() error {
|
||||||
|
return srv.Start(ctx)
|
||||||
|
})
|
||||||
|
|
||||||
wg.Add(1)
|
safeGo(func() error {
|
||||||
go func() {
|
return worker.Start(ctx)
|
||||||
defer wg.Done()
|
})
|
||||||
if err := worker.Start(ctx); err != nil {
|
|
||||||
errChan <- err
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if cfg.Repair.Enabled {
|
if cfg.Repair.Enabled {
|
||||||
wg.Add(1)
|
safeGo(func() error {
|
||||||
go func() {
|
err := svc.Repair.Start(ctx)
|
||||||
defer wg.Done()
|
if err != nil {
|
||||||
if err := svc.Repair.Start(ctx); err != nil {
|
_log.Error().Err(err).Msg("Error during repair")
|
||||||
log.Printf("Error during repair: %v", err)
|
|
||||||
}
|
}
|
||||||
}()
|
return nil // Not propagating repair errors to terminate the app
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
|||||||
7
main.go
7
main.go
@@ -6,9 +6,16 @@ import (
|
|||||||
"github.com/sirrobot01/debrid-blackhole/cmd/decypharr"
|
"github.com/sirrobot01/debrid-blackhole/cmd/decypharr"
|
||||||
"github.com/sirrobot01/debrid-blackhole/internal/config"
|
"github.com/sirrobot01/debrid-blackhole/internal/config"
|
||||||
"log"
|
"log"
|
||||||
|
"runtime/debug"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
log.Printf("FATAL: Recovered from panic in main: %v\n", r)
|
||||||
|
debug.PrintStack()
|
||||||
|
}
|
||||||
|
}()
|
||||||
var configPath string
|
var configPath string
|
||||||
flag.StringVar(&configPath, "config", "/data", "path to the data folder")
|
flag.StringVar(&configPath, "config", "/data", "path to the data folder")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|||||||
@@ -219,14 +219,14 @@ func (ts *TorrentStorage) DeleteMultiple(hashes []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TorrentStorage) Save() error {
|
func (ts *TorrentStorage) Save() error {
|
||||||
ts.mu.RLock()
|
|
||||||
defer ts.mu.RUnlock()
|
|
||||||
return ts.saveToFile()
|
return ts.saveToFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
// saveToFile is a helper function to write the current state to the JSON file
|
// saveToFile is a helper function to write the current state to the JSON file
|
||||||
func (ts *TorrentStorage) saveToFile() error {
|
func (ts *TorrentStorage) saveToFile() error {
|
||||||
|
ts.mu.RLock()
|
||||||
data, err := json.MarshalIndent(ts.torrents, "", " ")
|
data, err := json.MarshalIndent(ts.torrents, "", " ")
|
||||||
|
ts.mu.RUnlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user