fixed playlist download not setting metadata

This commit is contained in:
2024-05-13 11:04:16 +02:00
parent dc2828b884
commit 6abfb57598
2 changed files with 11 additions and 12 deletions

View File

@@ -2,6 +2,7 @@ package internal
import ( import (
"log/slog" "log/slog"
"runtime"
"github.com/marcopeocchi/yt-dlp-web-ui/server/config" "github.com/marcopeocchi/yt-dlp-web-ui/server/config"
) )
@@ -9,6 +10,7 @@ import (
type MessageQueue struct { type MessageQueue struct {
producerCh chan *Process producerCh chan *Process
consumerCh chan struct{} consumerCh chan struct{}
metadataCh chan struct{}
logger *slog.Logger logger *slog.Logger
} }
@@ -26,6 +28,7 @@ func NewMessageQueue(l *slog.Logger) *MessageQueue {
return &MessageQueue{ return &MessageQueue{
producerCh: make(chan *Process, size), producerCh: make(chan *Process, size),
consumerCh: make(chan struct{}, size), consumerCh: make(chan struct{}, size),
metadataCh: make(chan struct{}, runtime.NumCPU()),
logger: l, logger: l,
} }
} }
@@ -33,6 +36,8 @@ func NewMessageQueue(l *slog.Logger) *MessageQueue {
// Publish a message to the queue and set the task to a peding state. // Publish a message to the queue and set the task to a peding state.
func (m *MessageQueue) Publish(p *Process) { func (m *MessageQueue) Publish(p *Process) {
p.SetPending() p.SetPending()
m.metadataCh <- struct{}{}
go func() { go func() {
if err := p.SetMetadata(); err != nil { if err := p.SetMetadata(); err != nil {
m.logger.Error( m.logger.Error(
@@ -40,14 +45,9 @@ func (m *MessageQueue) Publish(p *Process) {
slog.String("err", err.Error()), slog.String("err", err.Error()),
) )
} }
<-m.metadataCh
}() }()
m.producerCh <- p
}
// Publish a message to the queue and set the task to a peding state.
// ENSURE P IS PART OF A PLAYLIST
// Needs a further review
func (m *MessageQueue) PublishPlaylistEntry(p *Process) {
m.producerCh <- p m.producerCh <- p
} }

View File

@@ -55,9 +55,7 @@ func PlaylistDetect(req DownloadRequest, mq *MessageQueue, db *MemoryDB, logger
slog.Int("count", m.Count), slog.Int("count", m.Count),
) )
for i, meta := range m.Entries { for _, meta := range m.Entries {
delta := time.Second.Microseconds() * int64(i+1)
// detect playlist title from metadata since each playlist entry will be // detect playlist title from metadata since each playlist entry will be
// treated as an individual download // treated as an individual download
req.Rename = strings.Replace( req.Rename = strings.Replace(
@@ -77,11 +75,11 @@ func PlaylistDetect(req DownloadRequest, mq *MessageQueue, db *MemoryDB, logger
} }
proc.Info.URL = meta.OriginalURL proc.Info.URL = meta.OriginalURL
proc.Info.CreatedAt = time.Now().Add(time.Duration(delta))
time.Sleep(time.Millisecond)
db.Set(proc) db.Set(proc)
proc.SetPending() mq.Publish(proc)
mq.PublishPlaylistEntry(proc)
} }
err = cmd.Wait() err = cmd.Wait()
@@ -94,6 +92,7 @@ func PlaylistDetect(req DownloadRequest, mq *MessageQueue, db *MemoryDB, logger
Logger: logger, Logger: logger,
} }
db.Set(proc)
mq.Publish(proc) mq.Publish(proc)
logger.Info("sending new process to message queue", slog.String("url", proc.Url)) logger.Info("sending new process to message queue", slog.String("url", proc.Url))