Some checks failed
CI / Backend (push) Failing after 11s
Release / Check ui (push) Failing after 51s
Release / Docker / ui (push) Has been skipped
CI / UI (push) Failing after 55s
Release / Test backend (push) Failing after 1m9s
Release / Docker / backend (push) Has been skipped
Release / Docker / runner (push) Has been skipped
Release / Docker / caddy (push) Failing after 28s
Release / Gitea Release (push) Has been skipped
CI / UI (pull_request) Failing after 42s
CI / Backend (pull_request) Successful in 3m45s
- LibreTranslate client (chunks on blank lines, ≤4500 chars, 3-goroutine semaphore)
- Runner translation task loop (OTel, heartbeat, MinIO storage)
- PocketBase translation_jobs collection support (create/claim/finish/list)
- Per-chapter language switcher on chapter reader (EN/RU/ID/PT/FR, polls until done)
- Admin /admin/translation page: bulk enqueue form + live-polling jobs table
- New backend routes: POST /api/translation/{slug}/{n}, GET /api/translation/status,
GET /api/translation/{slug}/{n}, GET /api/admin/translation/jobs,
POST /api/admin/translation/bulk
- ListTranslationTasks added to taskqueue.Reader interface + store impl
- All builds and tests pass; svelte-check: 0 errors
97 lines
2.9 KiB
Go
97 lines
2.9 KiB
Go
package asynqqueue
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/hibiken/asynq"
|
|
"github.com/libnovel/backend/internal/taskqueue"
|
|
)
|
|
|
|
// Producer dual-writes every task: first to PocketBase (via pb, for audit /
|
|
// UI status), then to Redis via Asynq so the runner picks it up immediately.
|
|
type Producer struct {
|
|
pb taskqueue.Producer // underlying PocketBase producer
|
|
client *asynq.Client
|
|
}
|
|
|
|
// NewProducer wraps an existing PocketBase Producer with Asynq dispatch.
|
|
func NewProducer(pb taskqueue.Producer, redisOpt asynq.RedisConnOpt) *Producer {
|
|
return &Producer{
|
|
pb: pb,
|
|
client: asynq.NewClient(redisOpt),
|
|
}
|
|
}
|
|
|
|
// Close shuts down the underlying Asynq client connection.
|
|
func (p *Producer) Close() error {
|
|
return p.client.Close()
|
|
}
|
|
|
|
// CreateScrapeTask creates a PocketBase record then enqueues an Asynq job.
|
|
func (p *Producer) CreateScrapeTask(ctx context.Context, kind, targetURL string, fromChapter, toChapter int) (string, error) {
|
|
id, err := p.pb.CreateScrapeTask(ctx, kind, targetURL, fromChapter, toChapter)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
payload := ScrapePayload{
|
|
PBTaskID: id,
|
|
Kind: kind,
|
|
TargetURL: targetURL,
|
|
FromChapter: fromChapter,
|
|
ToChapter: toChapter,
|
|
}
|
|
taskType := TypeScrapeBook
|
|
if kind == "catalogue" {
|
|
taskType = TypeScrapeCatalogue
|
|
}
|
|
if err := p.enqueue(ctx, taskType, payload); err != nil {
|
|
// Non-fatal: PB record exists; runner will pick it up on next poll.
|
|
return id, fmt.Errorf("asynq enqueue scrape (task still in PB): %w", err)
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
// CreateAudioTask creates a PocketBase record then enqueues an Asynq job.
|
|
func (p *Producer) CreateAudioTask(ctx context.Context, slug string, chapter int, voice string) (string, error) {
|
|
id, err := p.pb.CreateAudioTask(ctx, slug, chapter, voice)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
payload := AudioPayload{
|
|
PBTaskID: id,
|
|
Slug: slug,
|
|
Chapter: chapter,
|
|
Voice: voice,
|
|
}
|
|
if err := p.enqueue(ctx, TypeAudioGenerate, payload); err != nil {
|
|
return id, fmt.Errorf("asynq enqueue audio (task still in PB): %w", err)
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
// CreateTranslationTask creates a PocketBase record. Translation tasks are
|
|
// not currently dispatched via Asynq — the runner picks them up via polling.
|
|
func (p *Producer) CreateTranslationTask(ctx context.Context, slug string, chapter int, lang string) (string, error) {
|
|
return p.pb.CreateTranslationTask(ctx, slug, chapter, lang)
|
|
}
|
|
|
|
// CancelTask delegates to PocketBase; Asynq jobs may already be running and
|
|
// cannot be reliably cancelled, so we only update the audit record.
|
|
func (p *Producer) CancelTask(ctx context.Context, id string) error {
|
|
return p.pb.CancelTask(ctx, id)
|
|
}
|
|
|
|
// enqueue serialises payload and dispatches it to Asynq.
|
|
func (p *Producer) enqueue(_ context.Context, taskType string, payload any) error {
|
|
b, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return fmt.Errorf("marshal payload: %w", err)
|
|
}
|
|
_, err = p.client.Enqueue(asynq.NewTask(taskType, b))
|
|
return err
|
|
}
|