code refactoring, enabled memory db persist to fs.

This commit is contained in:
2023-05-25 11:13:46 +02:00
parent acfc5aa064
commit fd0b40ac46
10 changed files with 82 additions and 17 deletions

View File

@@ -108,4 +108,14 @@ func (m *MemoryDB) Restore() {
feed, _ := os.ReadFile("session.dat")
session := Session{}
json.Unmarshal(feed, &session)
for _, proc := range session.Processes {
db.table.Store(proc.Id, &Process{
id: proc.Id,
url: proc.Info.URL,
Info: proc.Info,
Progress: proc.Progress,
mem: m,
})
}
}

View File

@@ -171,14 +171,17 @@ func (p *Process) Kill() error {
// has been spawned with setPgid = true. To properly kill
// all subprocesses a SIGTERM need to be sent to the correct
// process group
pgid, err := syscall.Getpgid(p.proc.Pid)
if err != nil {
if p.proc != nil {
pgid, err := syscall.Getpgid(p.proc.Pid)
if err != nil {
return err
}
err = syscall.Kill(-pgid, syscall.SIGTERM)
log.Println("Killed process", p.id)
return err
}
err = syscall.Kill(-pgid, syscall.SIGTERM)
log.Println("Killed process", p.id)
return err
return nil
}
// Returns the available format for this URL

View File

@@ -21,6 +21,13 @@ type DirectoryEntry struct {
SHASum string `json:"shaSum"`
}
func isValidEntry(d fs.DirEntry) bool {
return !d.IsDir() &&
!strings.HasPrefix(d.Name(), ".") &&
!strings.HasSuffix(d.Name(), ".part") &&
!strings.HasSuffix(d.Name(), ".ytdl")
}
func walkDir(root string) (*[]DirectoryEntry, error) {
files := []DirectoryEntry{}
@@ -28,7 +35,7 @@ func walkDir(root string) (*[]DirectoryEntry, error) {
if err != nil {
return err
}
if !d.IsDir() && !strings.HasPrefix(d.Name(), ".") {
if isValidEntry(d) {
h := sha256.New()
h.Write([]byte(path))

View File

@@ -1,12 +1,17 @@
package server
import (
"context"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"net/rpc"
"os"
"os/signal"
"syscall"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
@@ -18,6 +23,8 @@ import (
var db MemoryDB
func RunBlocking(port int, frontend fs.FS) {
db.Restore()
service := new(Service)
rpc.Register(service)
@@ -73,5 +80,34 @@ func RunBlocking(port int, frontend fs.FS) {
app.Server().StreamRequestBody = true
go periodicallyPersist()
go gracefulShutdown(app)
log.Fatal(app.Listen(fmt.Sprintf(":%d", port)))
}
func gracefulShutdown(app *fiber.App) {
ctx, stop := signal.NotifyContext(context.Background(),
os.Interrupt,
syscall.SIGTERM,
syscall.SIGQUIT,
)
go func() {
<-ctx.Done()
log.Println("shutdown signal received")
defer func() {
db.Persist()
stop()
app.ShutdownWithTimeout(time.Second * 5)
}()
}()
}
func periodicallyPersist() {
for {
db.Persist()
time.Sleep(time.Minute * 5)
}
}

View File

@@ -72,12 +72,15 @@ func (t *Service) Running(args NoArgs, running *Running) error {
func (t *Service) Kill(args string, killed *string) error {
log.Println("Trying killing process with id", args)
proc, err := db.Get(args)
if err != nil {
return err
}
if proc != nil {
err = proc.Kill()
}
db.Delete(proc.id)
return err
}