Files
Media-download-telegram-bot/internal/sched/local.go
T
MichilisandClaude Fable 5.2 3b361c17a1 Phase 1: core bot
Link in, card, download, send, in private chats and groups.

- cmd/core with an embedded download worker
- Telegram front end on telego v1.13.0 (Bot API 10.4): link card with
  thumbnail and format buttons, one live status message, /dl /audio /thumb
  /info /queue /cancel, group support with requester-only buttons
- Database backed queue: paid and free lanes, leases with heartbeats, one
  automatic retry, cancel, queue position and wait estimate
- yt-dlp and ffprobe behind a Runner interface with classified, redacted
  errors mapped to friendly messages
- SQLite via modernc with goose migrations, portable schema
- Local file storage behind a Storage interface
- go-i18n with an English catalog, no literal text in handlers
- Free tier limits enforced before queueing and again on the real file
- Bot token and webhook secret are stripped from all log output
- Tests: URL parsing, job lifecycle, worker, yt-dlp wrapper, and end to end
  flows against a fake Bot API server; opt-in test with the real yt-dlp

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 07:33:32 +00:00

80 lines
2.0 KiB
Go

package sched
import (
"context"
"errors"
"os"
"time"
"github.com/noderunners/dlbot/internal/storage"
"github.com/noderunners/dlbot/internal/workerproto"
)
// Local is the in-process coordinator used by the embedded worker. It
// satisfies worker.Coordinator without any network hop.
type Local struct {
Sched *Scheduler
Storage storage.Storage
}
// Claim waits up to req.WaitSeconds for a job.
func (l *Local) Claim(ctx context.Context, req workerproto.ClaimRequest) (*workerproto.Job, error) {
deadline := time.Now().Add(time.Duration(req.WaitSeconds) * time.Second)
for {
job, err := l.Sched.Claim(ctx, req)
if err != nil || job != nil {
return job, err
}
left := time.Until(deadline)
if left <= 0 || ctx.Err() != nil {
return nil, ctx.Err()
}
l.Sched.WaitWork(ctx, left)
}
}
// Heartbeat forwards to the scheduler.
func (l *Local) Heartbeat(ctx context.Context, req workerproto.HeartbeatRequest) (workerproto.HeartbeatResponse, error) {
resp, err := l.Sched.Heartbeat(ctx, req)
return resp, wrapLease(err)
}
// Complete forwards to the scheduler.
func (l *Local) Complete(ctx context.Context, req workerproto.CompleteRequest) error {
return wrapLease(l.Sched.Complete(ctx, req))
}
// Fail forwards to the scheduler.
func (l *Local) Fail(ctx context.Context, req workerproto.FailRequest) error {
return wrapLease(l.Sched.Fail(ctx, req))
}
// Upload copies the finished file into storage.
func (l *Local) Upload(ctx context.Context, job *workerproto.Job, path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
st, err := f.Stat()
if err != nil {
return err
}
return l.Storage.Put(ctx, job.UploadKey, f, st.Size())
}
// leaseError marks ErrLeaseLost for the worker package, which cannot import
// this one.
type leaseError struct{ error }
func (leaseError) LeaseLost() bool { return true }
func (e leaseError) Unwrap() error { return e.error }
func wrapLease(err error) error {
if errors.Is(err, ErrLeaseLost) {
return leaseError{err}
}
return err
}