streamd v0.2.2: session uptimes, egress DNS, Support, agent/Widevine
All checks were successful
release / build-and-upload (push) Successful in 7m2s
All checks were successful
release / build-and-upload (push) Successful in 7m2s
Dashboard Live uptimes + Recent runs from stream_sessions; overview.recent_sessions. WireGuard netns resolv.conf + missing-deps WARN. Support donations card. Agent start/stop/update, job_backoff default off, wvkey ensure/venv, phonecap retries, modhost YAML enrich, DRM SetCredentials requires keys.
This commit is contained in:
parent
3ae1dd58b3
commit
a091922efa
34 changed files with 2391 additions and 154 deletions
|
|
@ -36,6 +36,16 @@ type Config struct {
|
|||
// an on-demand stream, or a scheduled event about to start — jump ahead
|
||||
// of routine refreshes in the queue.
|
||||
OnDemandPriority *bool `yaml:"ondemand_priority"`
|
||||
// JobBackoff enables exponential retry delays after a failed capture
|
||||
// (1m, 2m, 4m, 8m…). Default off — a single agent should retry ASAP.
|
||||
// Set job_backoff: true when several agents share the same streams.
|
||||
JobBackoff *bool `yaml:"job_backoff"`
|
||||
// Daemon / update (streamd agent start|stop|update)
|
||||
LogFile string `yaml:"log_file"` // default agent.log next to config / install root
|
||||
PIDFile string `yaml:"pid_file"` // default agent.pid
|
||||
Channel string `yaml:"channel"` // stable|beta for agent update
|
||||
Asset string `yaml:"asset"` // optional streamd-* asset override
|
||||
KeepOld *int `yaml:"keep_old"` // archived binaries under old/ (default 10)
|
||||
}
|
||||
|
||||
// PushedCapturesArePriority reports whether pushed captures jump the queue.
|
||||
|
|
@ -44,6 +54,12 @@ func (c Config) PushedCapturesArePriority() bool {
|
|||
return c.OnDemandPriority == nil || *c.OnDemandPriority
|
||||
}
|
||||
|
||||
// JobBackoffEnabled is the exponential multi-agent retry delay.
|
||||
// Defaults to false so a lone agent retries immediately after a failure.
|
||||
func (c Config) JobBackoffEnabled() bool {
|
||||
return c.JobBackoff != nil && *c.JobBackoff
|
||||
}
|
||||
|
||||
func (c Config) PollInterval() time.Duration {
|
||||
if c.PollIntervalSec <= 0 {
|
||||
return 30 * time.Second
|
||||
|
|
@ -125,6 +141,9 @@ func applyDefaults(cfg Config, root string) Config {
|
|||
if cfg.WVD != "" && !filepath.IsAbs(cfg.WVD) {
|
||||
cfg.WVD = filepath.Join(root, cfg.WVD)
|
||||
}
|
||||
if cfg.Python != "" && !filepath.IsAbs(cfg.Python) {
|
||||
cfg.Python = filepath.Join(root, cfg.Python)
|
||||
}
|
||||
if cfg.ModulesDir == "" {
|
||||
cfg.ModulesDir = filepath.Join(cfg.DataDir, "modules")
|
||||
} else if !filepath.IsAbs(cfg.ModulesDir) {
|
||||
|
|
@ -133,6 +152,25 @@ func applyDefaults(cfg Config, root string) Config {
|
|||
if cfg.ModulesSyncIntervalSec <= 0 {
|
||||
cfg.ModulesSyncIntervalSec = 300
|
||||
}
|
||||
if cfg.LogFile == "" {
|
||||
cfg.LogFile = "agent.log"
|
||||
}
|
||||
if !filepath.IsAbs(cfg.LogFile) {
|
||||
cfg.LogFile = filepath.Join(root, cfg.LogFile)
|
||||
}
|
||||
if cfg.PIDFile == "" {
|
||||
cfg.PIDFile = "agent.pid"
|
||||
}
|
||||
if !filepath.IsAbs(cfg.PIDFile) {
|
||||
cfg.PIDFile = filepath.Join(root, cfg.PIDFile)
|
||||
}
|
||||
if cfg.Channel == "" {
|
||||
cfg.Channel = "stable"
|
||||
}
|
||||
if cfg.KeepOld == nil {
|
||||
k := 10
|
||||
cfg.KeepOld = &k
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -232,17 +232,18 @@ func (s *Store) MarkOK(id int64) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// MarkFailed increments attempts. Re-queues with backoff unless maxed out.
|
||||
// MarkFailed increments attempts. Re-queues unless maxed out.
|
||||
// waitingDevice=true keeps status queued without burning an attempt.
|
||||
func (s *Store) MarkFailed(id int64, msg string, waitingDevice bool) error {
|
||||
// backoff is how long to wait before the job is runnable again (0 = immediate).
|
||||
func (s *Store) MarkFailed(id int64, msg string, waitingDevice bool, backoff time.Duration) error {
|
||||
j, err := s.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if waitingDevice {
|
||||
_, err = s.DB.Exec(`
|
||||
UPDATE jobs SET status='queued', device_serial='', last_error=?, started_at='' WHERE id=?`,
|
||||
msg, id)
|
||||
UPDATE jobs SET status='queued', device_serial='', last_error=?, started_at='', not_before=? WHERE id=?`,
|
||||
msg, now(), id)
|
||||
return err
|
||||
}
|
||||
attempts := j.Attempts + 1
|
||||
|
|
@ -252,9 +253,8 @@ UPDATE jobs SET status='failed', attempts=?, last_error=?, finished_at=? WHERE i
|
|||
attempts, msg, now(), id)
|
||||
return err
|
||||
}
|
||||
backoff := time.Duration(1<<uint(min(attempts, 4))) * time.Minute
|
||||
if backoff > 15*time.Minute {
|
||||
backoff = 15 * time.Minute
|
||||
if backoff < 0 {
|
||||
backoff = 0
|
||||
}
|
||||
nb := time.Now().UTC().Add(backoff).Format(time.RFC3339)
|
||||
_, err = s.DB.Exec(`
|
||||
|
|
@ -263,6 +263,29 @@ UPDATE jobs SET status='queued', attempts=?, last_error=?, not_before=?, device_
|
|||
return err
|
||||
}
|
||||
|
||||
// ExponentialBackoff is the old multi-agent delay: 1m, 2m, 4m, 8m (cap 15m).
|
||||
func ExponentialBackoff(attempts int) time.Duration {
|
||||
if attempts < 1 {
|
||||
attempts = 1
|
||||
}
|
||||
d := time.Duration(1<<uint(min(attempts, 4))) * time.Minute
|
||||
if d > 15*time.Minute {
|
||||
d = 15 * time.Minute
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// ClearAllBackoffs makes every queued job runnable now (e.g. agent restart
|
||||
// after fixing modules, or when job_backoff is disabled).
|
||||
func (s *Store) ClearAllBackoffs() (int, error) {
|
||||
res, err := s.DB.Exec(`UPDATE jobs SET not_before=? WHERE status='queued'`, now())
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
return int(n), nil
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
|
|
|
|||
|
|
@ -27,7 +27,10 @@ type Worker struct {
|
|||
Python string
|
||||
WVD string
|
||||
UserAgent string
|
||||
Log *log.Logger
|
||||
// JobBackoff enables exponential delays between failed capture attempts.
|
||||
// When false (default), failed jobs are re-queued immediately.
|
||||
JobBackoff bool
|
||||
Log *log.Logger
|
||||
|
||||
lastDelayLog time.Time
|
||||
}
|
||||
|
|
@ -67,13 +70,18 @@ func (w *Worker) tick() {
|
|||
if device.IsWait(err) || isAuthErr(err) || isUnknownAppErr(err) {
|
||||
// Config/environment issues — do not burn attempts or multi-minute backoff.
|
||||
w.logf("job=%d waiting: %v", job.ID, err)
|
||||
_ = w.Queue.MarkFailed(job.ID, err.Error(), true)
|
||||
_ = w.Queue.MarkFailed(job.ID, err.Error(), true, 0)
|
||||
return
|
||||
}
|
||||
w.logf("job=%d failed: %v", job.ID, err)
|
||||
w.event(job, client.EventCaptureFailed, err.Error(),
|
||||
map[string]any{"attempts": job.Attempts + 1, "max_attempts": job.MaxAttempts})
|
||||
_ = w.Queue.MarkFailed(job.ID, err.Error(), false)
|
||||
backoff := time.Duration(0)
|
||||
if w.JobBackoff {
|
||||
backoff = queue.ExponentialBackoff(job.Attempts + 1)
|
||||
w.logf("job=%d re-queue in %s (job_backoff=true)", job.ID, backoff)
|
||||
}
|
||||
_ = w.Queue.MarkFailed(job.ID, err.Error(), false, backoff)
|
||||
return
|
||||
}
|
||||
_ = w.Queue.MarkOK(job.ID)
|
||||
|
|
@ -119,8 +127,8 @@ func (w *Worker) runJob(job queue.Job) error {
|
|||
_ = w.Queue.SetDevice(job.ID, serial)
|
||||
w.logf("job=%d device=%s package=%s", job.ID, serial, a.Package())
|
||||
|
||||
// Short TTL; /ws/agent heartbeats renew while this process is alive.
|
||||
if err := w.API.Claim(job.StreamID, w.AgentID, 90); err != nil {
|
||||
// Long enough for DRM capture retries (3×90s wait + launch). Heartbeats renew.
|
||||
if err := w.API.Claim(job.StreamID, w.AgentID, 360); err != nil {
|
||||
return fmt.Errorf("streamd claim: %w", err)
|
||||
}
|
||||
defer func() { _ = w.API.ReleaseClaim(job.StreamID, w.AgentID) }()
|
||||
|
|
@ -140,6 +148,19 @@ func (w *Worker) runJob(job queue.Job) error {
|
|||
return err
|
||||
}
|
||||
|
||||
needsKeys := true
|
||||
if km := strings.ToLower(strings.TrimSpace(a.KeyMode())); km == "none" || km == "clear" {
|
||||
needsKeys = false
|
||||
}
|
||||
hasKey := poller.ValidKeyPair(res.Key)
|
||||
if needsKeys && !hasKey {
|
||||
return fmt.Errorf("refusing to post credentials without valid Widevine key (got mpd=%q key_empty=%v)",
|
||||
trimURL(res.MPD), strings.TrimSpace(res.Key) == "")
|
||||
}
|
||||
if strings.TrimSpace(res.MPD) == "" {
|
||||
return fmt.Errorf("refusing to post credentials without mpd")
|
||||
}
|
||||
|
||||
cred := client.Credentials{
|
||||
MPD: res.MPD,
|
||||
Key: res.Key,
|
||||
|
|
@ -150,15 +171,25 @@ func (w *Worker) runJob(job queue.Job) error {
|
|||
if err := w.API.PostCredentials(job.StreamID, cred); err != nil {
|
||||
return fmt.Errorf("post credentials: %w", err)
|
||||
}
|
||||
w.logf("job=%d posted credentials to streamd has_key=%v key_valid=%v mpd=%s",
|
||||
job.ID, strings.TrimSpace(res.Key) != "", hasKey, trimURL(res.MPD))
|
||||
// Report what was captured, never the key itself.
|
||||
w.event(job, client.EventCaptureOK, "credentials captured and posted", map[string]any{
|
||||
"device": serial,
|
||||
"has_key": strings.TrimSpace(res.Key) != "",
|
||||
"key_valid": poller.ValidKeyPair(res.Key),
|
||||
"key_valid": hasKey,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func trimURL(s string) string {
|
||||
s = strings.TrimSpace(s)
|
||||
if len(s) <= 96 {
|
||||
return s
|
||||
}
|
||||
return s[:96] + "…"
|
||||
}
|
||||
|
||||
// event posts a stream event to streamd. A timeline entry is never worth
|
||||
// failing a capture over, so errors are logged and dropped.
|
||||
func (w *Worker) event(job queue.Job, kind, message string, detail map[string]any) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue