feat: download lifecycle — stalled and failed downloads are given up and searched again; imported ones seed, then go (M502 #5523)
release / govulncheck (push) Successful in 17s
release / web (push) Failing after 17s
release / go (push) Successful in 1m11s
release / integration (push) Successful in 4m50s
release / android (push) Successful in 4m54s
release / Build signed APK (releases and dev) (push) Successful in 5m9s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped

Ported from Fabled Librarian's System 104. A new lifecycle poller, every
ten minutes:
- a download the client fails is given up at once;
- one nobody shares, or still fetching its file list, past the stall
  setting (6 h) is asked for again three times, then given up;
- giving up removes the download with its files, holds the release back
  for a week (release_blocks.expires_at), ends the grab and searches the
  album again, with no limit per album;
- a running download the client stops listing for half an hour ends and is
  searched again, not held back; an empty list is not taken as gone;
- an imported download seeds to ratio 2.0 or 14 days, then is removed;
- a download the client would not remove is asked again every pass.

Every condition and act is one open row per (client, download, condition)
in download_interventions. Activity shows what is wrong with a download and
how often it was retried, why one ended, its seeding, and a banner for a
client that does not answer. A running download can be given up by hand
(set aside for good). The seed ratio, seed days and stall hours are on the
search settings card. The download client lookup moves to
acquire.DownloadClientFor, shared by the importer and the lifecycle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-10 15:27:22 -04:00
co-authored by Claude Opus 5.5
parent d9e00d8bc2
commit 8245f4406e
34 changed files with 2362 additions and 106 deletions
+84 -32
View File
@@ -16,7 +16,7 @@ UPDATE grabs
SET import_state = 'importing', import_attempts = import_attempts + 1,
import_updated_at = now(), progress = 1
WHERE id = $1 AND ended_at IS NULL AND import_state IN ('', 'downloading', 'waiting')
RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination
RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at
`
// Takes a finished download for import. The update is the lock: a second
@@ -52,6 +52,9 @@ func (q *Queries) ClaimGrabImport(ctx context.Context, id pgtype.UUID) (Grab, er
&i.ImportedAt,
&i.ImportedFiles,
&i.Destination,
&i.Seeding,
&i.Ratio,
&i.SeedReleasedAt,
)
return i, err
}
@@ -60,7 +63,7 @@ const forceGrabImport = `-- name: ForceGrabImport :one
UPDATE grabs
SET import_state = '', import_forced = true, import_reason = '', import_updated_at = now()
WHERE id = $1 AND ended_at IS NULL AND import_state IN ('review', 'failed')
RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination
RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at
`
// "Import anyway": the next pass imports the download whatever the check said.
@@ -95,12 +98,15 @@ func (q *Queries) ForceGrabImport(ctx context.Context, id pgtype.UUID) (Grab, er
&i.ImportedAt,
&i.ImportedFiles,
&i.Destination,
&i.Seeding,
&i.Ratio,
&i.SeedReleasedAt,
)
return i, err
}
const getGrab = `-- name: GetGrab :one
SELECT id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination FROM grabs WHERE id = $1
SELECT id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at FROM grabs WHERE id = $1
`
func (q *Queries) GetGrab(ctx context.Context, id pgtype.UUID) (Grab, error) {
@@ -134,6 +140,9 @@ func (q *Queries) GetGrab(ctx context.Context, id pgtype.UUID) (Grab, error) {
&i.ImportedAt,
&i.ImportedFiles,
&i.Destination,
&i.Seeding,
&i.Ratio,
&i.SeedReleasedAt,
)
return i, err
}
@@ -202,9 +211,16 @@ const listGrabs = `-- name: ListGrabs :many
SELECT g.id, g.want_id, g.name, g.indexer, g.client, g.quality, g.origin, g.forced,
g.grabbed_at, g.ended_at, g.end_reason, g.end_detail, g.progress, g.import_state,
g.import_reason, g.import_detail, g.import_updated_at, g.imported_at, g.imported_files,
g.destination, w.title AS want_title, w.artist_name AS want_artist
g.destination, g.seeding, g.ratio, g.seed_released_at,
w.title AS want_title, w.artist_name AS want_artist,
di.trigger AS condition, di.detail AS condition_detail, di.remediations AS condition_retries,
di.first_seen AS condition_since
FROM grabs g
LEFT JOIN wants w ON w.id = g.want_id
LEFT JOIN download_interventions di
ON di.id = (SELECT x.id FROM download_interventions x
WHERE x.grab_id = g.id AND x.outcome = 'open'
ORDER BY x.last_seen DESC LIMIT 1)
ORDER BY (g.ended_at IS NULL AND g.import_state IN ('review', 'failed')) DESC,
g.ended_at IS NULL DESC, g.grabbed_at DESC, g.id
LIMIT $2 OFFSET $1
@@ -216,32 +232,40 @@ type ListGrabsParams struct {
}
type ListGrabsRow struct {
ID pgtype.UUID
WantID pgtype.UUID
Name string
Indexer string
Client string
Quality string
Origin string
Forced bool
GrabbedAt pgtype.Timestamptz
EndedAt pgtype.Timestamptz
EndReason string
EndDetail string
Progress float32
ImportState string
ImportReason string
ImportDetail []byte
ImportUpdatedAt pgtype.Timestamptz
ImportedAt pgtype.Timestamptz
ImportedFiles int32
Destination string
WantTitle *string
WantArtist *string
ID pgtype.UUID
WantID pgtype.UUID
Name string
Indexer string
Client string
Quality string
Origin string
Forced bool
GrabbedAt pgtype.Timestamptz
EndedAt pgtype.Timestamptz
EndReason string
EndDetail string
Progress float32
ImportState string
ImportReason string
ImportDetail []byte
ImportUpdatedAt pgtype.Timestamptz
ImportedAt pgtype.Timestamptz
ImportedFiles int32
Destination string
Seeding bool
Ratio float32
SeedReleasedAt pgtype.Timestamptz
WantTitle *string
WantArtist *string
Condition *string
ConditionDetail *string
ConditionRetries *int32
ConditionSince pgtype.Timestamptz
}
// The Wanted view's Activity tab: downloads waiting on a person first, then
// the rest still running, then the ended ones newest first.
// the rest still running, then the ended ones newest first. Each carries
// what is wrong with it now, if anything (#5523).
func (q *Queries) ListGrabs(ctx context.Context, arg ListGrabsParams) ([]ListGrabsRow, error) {
rows, err := q.db.Query(ctx, listGrabs, arg.Skip, arg.MaxRows)
if err != nil {
@@ -272,8 +296,15 @@ func (q *Queries) ListGrabs(ctx context.Context, arg ListGrabsParams) ([]ListGra
&i.ImportedAt,
&i.ImportedFiles,
&i.Destination,
&i.Seeding,
&i.Ratio,
&i.SeedReleasedAt,
&i.WantTitle,
&i.WantArtist,
&i.Condition,
&i.ConditionDetail,
&i.ConditionRetries,
&i.ConditionSince,
); err != nil {
return nil, err
}
@@ -287,7 +318,7 @@ func (q *Queries) ListGrabs(ctx context.Context, arg ListGrabsParams) ([]ListGra
const listLiveGrabs = `-- name: ListLiveGrabs :many
SELECT g.id, g.want_id, g.info_hash, g.name, g.key, g.indexer, g.source, g.client, g.save_path, g.quality, g.origin, g.forced, g.grabbed_by, g.grabbed_at, g.ended_at, g.end_reason, g.end_detail, g.progress, g.import_state, g.import_reason, g.import_detail, g.import_forced, g.import_attempts, g.import_updated_at, g.imported_at, g.imported_files, g.destination, w.release_group_mbid, w.artist_mbid, w.artist_name, w.title AS want_title,
SELECT g.id, g.want_id, g.info_hash, g.name, g.key, g.indexer, g.source, g.client, g.save_path, g.quality, g.origin, g.forced, g.grabbed_by, g.grabbed_at, g.ended_at, g.end_reason, g.end_detail, g.progress, g.import_state, g.import_reason, g.import_detail, g.import_forced, g.import_attempts, g.import_updated_at, g.imported_at, g.imported_files, g.destination, g.seeding, g.ratio, g.seed_released_at, w.release_group_mbid, w.artist_mbid, w.artist_name, w.title AS want_title,
w.reason AS want_reason, w.album_id AS want_album_id
FROM grabs g
LEFT JOIN wants w ON w.id = g.want_id
@@ -323,6 +354,9 @@ type ListLiveGrabsRow struct {
ImportedAt pgtype.Timestamptz
ImportedFiles int32
Destination string
Seeding bool
Ratio float32
SeedReleasedAt pgtype.Timestamptz
ReleaseGroupMbid *string
ArtistMbid *string
ArtistName *string
@@ -370,6 +404,9 @@ func (q *Queries) ListLiveGrabs(ctx context.Context) ([]ListLiveGrabsRow, error)
&i.ImportedAt,
&i.ImportedFiles,
&i.Destination,
&i.Seeding,
&i.Ratio,
&i.SeedReleasedAt,
&i.ReleaseGroupMbid,
&i.ArtistMbid,
&i.ArtistName,
@@ -388,10 +425,11 @@ func (q *Queries) ListLiveGrabs(ctx context.Context) ([]ListLiveGrabsRow, error)
}
const listReleaseBlocksPage = `-- name: ListReleaseBlocksPage :many
SELECT b.info_hash, b.name, b.reason, b.detail, b.blocked_at, w.title AS want_title,
SELECT b.info_hash, b.name, b.reason, b.detail, b.blocked_at, b.expires_at, w.title AS want_title,
w.artist_name AS want_artist
FROM release_blocks b
LEFT JOIN wants w ON w.id = b.want_id
WHERE b.expires_at IS NULL OR b.expires_at > now()
ORDER BY b.blocked_at DESC, b.info_hash
LIMIT $2 OFFSET $1
`
@@ -407,6 +445,7 @@ type ListReleaseBlocksPageRow struct {
Reason string
Detail string
BlockedAt pgtype.Timestamptz
ExpiresAt pgtype.Timestamptz
WantTitle *string
WantArtist *string
}
@@ -426,6 +465,7 @@ func (q *Queries) ListReleaseBlocksPage(ctx context.Context, arg ListReleaseBloc
&i.Reason,
&i.Detail,
&i.BlockedAt,
&i.ExpiresAt,
&i.WantTitle,
&i.WantArtist,
); err != nil {
@@ -522,13 +562,16 @@ UPDATE grabs
import_updated_at = now(),
import_forced = false,
imported_at = CASE WHEN $1::text = 'imported' THEN now() ELSE imported_at END,
-- The files were copied, so an imported download goes on sharing
-- until the lifecycle removes it (#5523).
seeding = $1::text = 'imported',
imported_files = $4,
destination = $5,
ended_at = CASE WHEN $6::text <> '' THEN now() ELSE ended_at END,
end_reason = $6,
end_detail = CASE WHEN $6::text <> '' THEN $2::text ELSE end_detail END
WHERE id = $7
RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination
RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at
`
type RecordImportParams struct {
@@ -582,6 +625,9 @@ func (q *Queries) RecordImport(ctx context.Context, arg RecordImportParams) (Gra
&i.ImportedAt,
&i.ImportedFiles,
&i.Destination,
&i.Seeding,
&i.Ratio,
&i.SeedReleasedAt,
)
return i, err
}
@@ -591,7 +637,7 @@ UPDATE grabs g
SET want_id = $1
WHERE g.id = $2 AND g.ended_at IS NULL
AND NOT EXISTS (SELECT 1 FROM grabs o WHERE o.want_id = $1 AND o.ended_at IS NULL)
RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination
RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at
`
type RehomeGrabParams struct {
@@ -632,6 +678,9 @@ func (q *Queries) RehomeGrab(ctx context.Context, arg RehomeGrabParams) (Grab, e
&i.ImportedAt,
&i.ImportedFiles,
&i.Destination,
&i.Seeding,
&i.Ratio,
&i.SeedReleasedAt,
)
return i, err
}
@@ -640,7 +689,7 @@ const retryGrabImport = `-- name: RetryGrabImport :one
UPDATE grabs
SET import_state = '', import_reason = '', import_updated_at = now()
WHERE id = $1 AND ended_at IS NULL AND import_state = 'failed'
RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination
RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at
`
func (q *Queries) RetryGrabImport(ctx context.Context, id pgtype.UUID) (Grab, error) {
@@ -674,6 +723,9 @@ func (q *Queries) RetryGrabImport(ctx context.Context, id pgtype.UUID) (Grab, er
&i.ImportedAt,
&i.ImportedFiles,
&i.Destination,
&i.Seeding,
&i.Ratio,
&i.SeedReleasedAt,
)
return i, err
}
+389
View File
@@ -0,0 +1,389 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: lifecycle.sql
package dbq
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const countRemediation = `-- name: CountRemediation :one
UPDATE download_interventions
SET remediations = remediations + 1,
action = $1,
last_seen = now(),
outcome = CASE WHEN $1::text IN ('given_up', 'removed') THEN 'resolved' ELSE outcome END,
settled_at = CASE WHEN $1::text IN ('given_up', 'removed') THEN now() ELSE settled_at END
WHERE client = $2 AND info_hash = $3
AND trigger = $4 AND outcome = 'open'
RETURNING remediations
`
type CountRemediationParams struct {
Action string
Client string
InfoHash string
Trigger string
}
// Minstrel acted on an open condition. A retry climbs the ladder; giving up
// or removing ends the condition, since what it was about is gone. Written
// before the act, so a retry that happened is never uncounted.
func (q *Queries) CountRemediation(ctx context.Context, arg CountRemediationParams) (int32, error) {
row := q.db.QueryRow(ctx, countRemediation,
arg.Action,
arg.Client,
arg.InfoHash,
arg.Trigger,
)
var remediations int32
err := row.Scan(&remediations)
return remediations, err
}
const deleteExpiredBlocks = `-- name: DeleteExpiredBlocks :execrows
DELETE FROM release_blocks WHERE expires_at IS NOT NULL AND expires_at <= now()
`
// A release given up on is held back for a while; after that the search may
// take it again.
func (q *Queries) DeleteExpiredBlocks(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, deleteExpiredBlocks)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const giveUpGrab = `-- name: GiveUpGrab :one
UPDATE grabs
SET ended_at = now(), end_reason = $1, end_detail = $2,
import_updated_at = now()
WHERE id = $3 AND ended_at IS NULL AND import_state = ANY($4::text[])
RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at
`
type GiveUpGrabParams struct {
EndReason string
EndDetail string
ID pgtype.UUID
States []string
}
// Ends a running grab, given up on. Only from the states named: the update
// is the lock against an import that claimed the download meanwhile.
func (q *Queries) GiveUpGrab(ctx context.Context, arg GiveUpGrabParams) (Grab, error) {
row := q.db.QueryRow(ctx, giveUpGrab,
arg.EndReason,
arg.EndDetail,
arg.ID,
arg.States,
)
var i Grab
err := row.Scan(
&i.ID,
&i.WantID,
&i.InfoHash,
&i.Name,
&i.Key,
&i.Indexer,
&i.Source,
&i.Client,
&i.SavePath,
&i.Quality,
&i.Origin,
&i.Forced,
&i.GrabbedBy,
&i.GrabbedAt,
&i.EndedAt,
&i.EndReason,
&i.EndDetail,
&i.Progress,
&i.ImportState,
&i.ImportReason,
&i.ImportDetail,
&i.ImportForced,
&i.ImportAttempts,
&i.ImportUpdatedAt,
&i.ImportedAt,
&i.ImportedFiles,
&i.Destination,
&i.Seeding,
&i.Ratio,
&i.SeedReleasedAt,
)
return i, err
}
const listClientsDown = `-- name: ListClientsDown :many
SELECT client, detail, first_seen FROM download_interventions
WHERE trigger = 'client_unreachable' AND outcome = 'open'
ORDER BY client
`
type ListClientsDownRow struct {
Client string
Detail string
FirstSeen pgtype.Timestamptz
}
// Download clients not answering now, for the Activity tab.
func (q *Queries) ListClientsDown(ctx context.Context) ([]ListClientsDownRow, error) {
rows, err := q.db.Query(ctx, listClientsDown)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListClientsDownRow
for rows.Next() {
var i ListClientsDownRow
if err := rows.Scan(&i.Client, &i.Detail, &i.FirstSeen); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listLeftovers = `-- name: ListLeftovers :many
SELECT id, client, info_hash, grab_id, name, trigger, action, outcome, detail, observations, remediations, first_seen, last_seen, settled_at FROM download_interventions
WHERE trigger = 'not_removed' AND outcome = 'open' AND info_hash <> ''
ORDER BY first_seen
`
// Downloads given up on that the client would not remove; each pass asks
// again.
func (q *Queries) ListLeftovers(ctx context.Context) ([]DownloadIntervention, error) {
rows, err := q.db.Query(ctx, listLeftovers)
if err != nil {
return nil, err
}
defer rows.Close()
var items []DownloadIntervention
for rows.Next() {
var i DownloadIntervention
if err := rows.Scan(
&i.ID,
&i.Client,
&i.InfoHash,
&i.GrabID,
&i.Name,
&i.Trigger,
&i.Action,
&i.Outcome,
&i.Detail,
&i.Observations,
&i.Remediations,
&i.FirstSeen,
&i.LastSeen,
&i.SettledAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listSeedingGrabs = `-- name: ListSeedingGrabs :many
SELECT id, client, info_hash, name, imported_at FROM grabs
WHERE seeding AND seed_released_at IS NULL
ORDER BY imported_at, id
`
type ListSeedingGrabsRow struct {
ID pgtype.UUID
Client string
InfoHash string
Name string
ImportedAt pgtype.Timestamptz
}
// Imported downloads still sharing, oldest import first.
func (q *Queries) ListSeedingGrabs(ctx context.Context) ([]ListSeedingGrabsRow, error) {
rows, err := q.db.Query(ctx, listSeedingGrabs)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListSeedingGrabsRow
for rows.Next() {
var i ListSeedingGrabsRow
if err := rows.Scan(
&i.ID,
&i.Client,
&i.InfoHash,
&i.Name,
&i.ImportedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const liveGrabOnHash = `-- name: LiveGrabOnHash :one
SELECT EXISTS (SELECT 1 FROM grabs WHERE info_hash = $1 AND ended_at IS NULL)::bool
`
func (q *Queries) LiveGrabOnHash(ctx context.Context, infoHash string) (bool, error) {
row := q.db.QueryRow(ctx, liveGrabOnHash, infoHash)
var column_1 bool
err := row.Scan(&column_1)
return column_1, err
}
const raiseIntervention = `-- name: RaiseIntervention :one
INSERT INTO download_interventions (client, info_hash, grab_id, name, trigger, detail)
VALUES ($1, $2, $3, $4,
$5, $6)
ON CONFLICT (client, info_hash, trigger) WHERE outcome = 'open' DO UPDATE SET
observations = download_interventions.observations + 1,
last_seen = now(),
detail = EXCLUDED.detail,
grab_id = COALESCE(EXCLUDED.grab_id, download_interventions.grab_id),
name = CASE WHEN EXCLUDED.name <> '' THEN EXCLUDED.name ELSE download_interventions.name END
RETURNING id, client, info_hash, grab_id, name, trigger, action, outcome, detail, observations, remediations, first_seen, last_seen, settled_at
`
type RaiseInterventionParams struct {
Client string
InfoHash string
GrabID pgtype.UUID
Name string
Trigger string
Detail string
}
// The download lifecycle (M502 #5523): the intervention ledger, giving a
// download up, and seeding after import.
// A condition seen, or seen again: a second pass lands on the open row, so
// a download stuck for a week is one row that says how long. The name only
// improves, and the detail is the latest word.
func (q *Queries) RaiseIntervention(ctx context.Context, arg RaiseInterventionParams) (DownloadIntervention, error) {
row := q.db.QueryRow(ctx, raiseIntervention,
arg.Client,
arg.InfoHash,
arg.GrabID,
arg.Name,
arg.Trigger,
arg.Detail,
)
var i DownloadIntervention
err := row.Scan(
&i.ID,
&i.Client,
&i.InfoHash,
&i.GrabID,
&i.Name,
&i.Trigger,
&i.Action,
&i.Outcome,
&i.Detail,
&i.Observations,
&i.Remediations,
&i.FirstSeen,
&i.LastSeen,
&i.SettledAt,
)
return i, err
}
const releaseSeed = `-- name: ReleaseSeed :exec
UPDATE grabs SET seed_released_at = now() WHERE id = $1 AND seed_released_at IS NULL
`
// The seeding download was removed, or found gone.
func (q *Queries) ReleaseSeed(ctx context.Context, id pgtype.UUID) error {
_, err := q.db.Exec(ctx, releaseSeed, id)
return err
}
const setGrabRatio = `-- name: SetGrabRatio :exec
UPDATE grabs SET ratio = $1 WHERE id = $2
`
type SetGrabRatioParams struct {
Ratio float32
ID pgtype.UUID
}
func (q *Queries) SetGrabRatio(ctx context.Context, arg SetGrabRatioParams) error {
_, err := q.db.Exec(ctx, setGrabRatio, arg.Ratio, arg.ID)
return err
}
const settleEndedGrabs = `-- name: SettleEndedGrabs :execrows
UPDATE download_interventions di
SET outcome = 'resolved', settled_at = now(), last_seen = now()
FROM grabs g
WHERE g.id = di.grab_id AND g.ended_at IS NOT NULL
AND di.outcome = 'open' AND di.trigger <> 'not_removed'
`
// What was wrong with a download whose grab has ended (imported, refused,
// given up) is over, except a download left in the client to remove.
func (q *Queries) SettleEndedGrabs(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, settleEndedGrabs)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const settleIntervention = `-- name: SettleIntervention :execrows
UPDATE download_interventions
SET outcome = 'resolved', settled_at = now(), last_seen = now()
WHERE client = $1 AND info_hash = $2
AND trigger = $3 AND outcome = 'open'
`
type SettleInterventionParams struct {
Client string
InfoHash string
Trigger string
}
// The condition cleared.
func (q *Queries) SettleIntervention(ctx context.Context, arg SettleInterventionParams) (int64, error) {
result, err := q.db.Exec(ctx, settleIntervention, arg.Client, arg.InfoHash, arg.Trigger)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const settleInterventionsFor = `-- name: SettleInterventionsFor :execrows
UPDATE download_interventions
SET outcome = 'resolved', settled_at = now(), last_seen = now()
WHERE client = $1 AND info_hash = $2 AND outcome = 'open'
`
type SettleInterventionsForParams struct {
Client string
InfoHash string
}
// Everything open about one download is over: it is gone from the client.
func (q *Queries) SettleInterventionsFor(ctx context.Context, arg SettleInterventionsForParams) (int64, error) {
result, err := q.db.Exec(ctx, settleInterventionsFor, arg.Client, arg.InfoHash)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
+24
View File
@@ -226,6 +226,9 @@ type AcquisitionSetting struct {
LastPass []byte
UpdatedAt pgtype.Timestamptz
ImportRoot string
SeedRatio float32
SeedDays int32
StallHours int32
}
type Album struct {
@@ -355,6 +358,23 @@ type DiscoverTuning struct {
UpdatedAt pgtype.Timestamptz
}
type DownloadIntervention struct {
ID pgtype.UUID
Client string
InfoHash string
GrabID pgtype.UUID
Name string
Trigger string
Action string
Outcome string
Detail string
Observations int32
Remediations int32
FirstSeen pgtype.Timestamptz
LastSeen pgtype.Timestamptz
SettledAt pgtype.Timestamptz
}
type DuplicateGroup struct {
ID pgtype.UUID
MemberKey string
@@ -459,6 +479,9 @@ type Grab struct {
ImportedAt pgtype.Timestamptz
ImportedFiles int32
Destination string
Seeding bool
Ratio float32
SeedReleasedAt pgtype.Timestamptz
}
type LibraryChange struct {
@@ -701,6 +724,7 @@ type ReleaseBlock struct {
WantID pgtype.UUID
BlockedBy pgtype.UUID
BlockedAt pgtype.Timestamptz
ExpiresAt pgtype.Timestamptz
}
type ReleaseTypeProfile struct {
+41 -8
View File
@@ -12,11 +12,13 @@ import (
)
const blockRelease = `-- name: BlockRelease :exec
INSERT INTO release_blocks (info_hash, name, reason, detail, want_id, blocked_by)
INSERT INTO release_blocks (info_hash, name, reason, detail, want_id, blocked_by, expires_at)
VALUES ($1, $2, $3, $4,
$5, $6)
$5, $6, $7)
ON CONFLICT (info_hash) DO UPDATE
SET reason = EXCLUDED.reason, detail = EXCLUDED.detail, blocked_at = now()
SET reason = EXCLUDED.reason, detail = EXCLUDED.detail, blocked_at = now(),
expires_at = EXCLUDED.expires_at, blocked_by = EXCLUDED.blocked_by
WHERE release_blocks.expires_at IS NOT NULL OR EXCLUDED.expires_at IS NULL
`
type BlockReleaseParams struct {
@@ -26,8 +28,12 @@ type BlockReleaseParams struct {
Detail string
WantID pgtype.UUID
BlockedBy pgtype.UUID
ExpiresAt pgtype.Timestamptz
}
// A release set aside. expires_at NULL is for good. A block for good is never
// shortened by a timed one: only a timed block, or another for good, is
// replaced.
func (q *Queries) BlockRelease(ctx context.Context, arg BlockReleaseParams) error {
_, err := q.db.Exec(ctx, blockRelease,
arg.InfoHash,
@@ -36,6 +42,7 @@ func (q *Queries) BlockRelease(ctx context.Context, arg BlockReleaseParams) erro
arg.Detail,
arg.WantID,
arg.BlockedBy,
arg.ExpiresAt,
)
return err
}
@@ -89,7 +96,7 @@ func (q *Queries) EndGrab(ctx context.Context, arg EndGrabParams) error {
const getAcquisitionSettings = `-- name: GetAcquisitionSettings :one
SELECT id, auto_grab, search_interval_hours, allowed_qualities, last_pass_at, last_pass, updated_at, import_root FROM acquisition_settings WHERE id = true
SELECT id, auto_grab, search_interval_hours, allowed_qualities, last_pass_at, last_pass, updated_at, import_root, seed_ratio, seed_days, stall_hours FROM acquisition_settings WHERE id = true
`
// Searching for wants and grabbing releases (M502 #5521).
@@ -105,12 +112,15 @@ func (q *Queries) GetAcquisitionSettings(ctx context.Context) (AcquisitionSettin
&i.LastPass,
&i.UpdatedAt,
&i.ImportRoot,
&i.SeedRatio,
&i.SeedDays,
&i.StallHours,
)
return i, err
}
const getLiveGrab = `-- name: GetLiveGrab :one
SELECT id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination FROM grabs WHERE want_id = $1 AND ended_at IS NULL
SELECT id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at FROM grabs WHERE want_id = $1 AND ended_at IS NULL
`
func (q *Queries) GetLiveGrab(ctx context.Context, wantID pgtype.UUID) (Grab, error) {
@@ -144,6 +154,9 @@ func (q *Queries) GetLiveGrab(ctx context.Context, wantID pgtype.UUID) (Grab, er
&i.ImportedAt,
&i.ImportedFiles,
&i.Destination,
&i.Seeding,
&i.Ratio,
&i.SeedReleasedAt,
)
return i, err
}
@@ -230,7 +243,7 @@ INSERT INTO grabs (want_id, info_hash, name, key, indexer, source, client, save_
VALUES ($1, $2, $3, $4, $5,
$6, $7, $8, $9,
$10, $11, $12)
RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination
RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at
`
type InsertGrabParams struct {
@@ -292,6 +305,9 @@ func (q *Queries) InsertGrab(ctx context.Context, arg InsertGrabParams) (Grab, e
&i.ImportedAt,
&i.ImportedFiles,
&i.Destination,
&i.Seeding,
&i.Ratio,
&i.SeedReleasedAt,
)
return i, err
}
@@ -350,9 +366,13 @@ func (q *Queries) InsertWantCandidate(ctx context.Context, arg InsertWantCandida
}
const listReleaseBlocks = `-- name: ListReleaseBlocks :many
SELECT info_hash, name, reason, detail, want_id, blocked_by, blocked_at FROM release_blocks ORDER BY blocked_at DESC
SELECT info_hash, name, reason, detail, want_id, blocked_by, blocked_at, expires_at FROM release_blocks
WHERE expires_at IS NULL OR expires_at > now()
ORDER BY blocked_at DESC
`
// Every release set aside and still held back: a stalled one lifts by itself
// at expires_at (#5523).
func (q *Queries) ListReleaseBlocks(ctx context.Context) ([]ReleaseBlock, error) {
rows, err := q.db.Query(ctx, listReleaseBlocks)
if err != nil {
@@ -370,6 +390,7 @@ func (q *Queries) ListReleaseBlocks(ctx context.Context) ([]ReleaseBlock, error)
&i.WantID,
&i.BlockedBy,
&i.BlockedAt,
&i.ExpiresAt,
); err != nil {
return nil, err
}
@@ -582,9 +603,12 @@ UPDATE acquisition_settings
search_interval_hours = $2,
allowed_qualities = $3,
import_root = $4,
seed_ratio = $5,
seed_days = $6,
stall_hours = $7,
updated_at = now()
WHERE id = true
RETURNING id, auto_grab, search_interval_hours, allowed_qualities, last_pass_at, last_pass, updated_at, import_root
RETURNING id, auto_grab, search_interval_hours, allowed_qualities, last_pass_at, last_pass, updated_at, import_root, seed_ratio, seed_days, stall_hours
`
type UpdateAcquisitionSettingsParams struct {
@@ -592,6 +616,9 @@ type UpdateAcquisitionSettingsParams struct {
SearchIntervalHours int32
AllowedQualities []string
ImportRoot string
SeedRatio float32
SeedDays int32
StallHours int32
}
func (q *Queries) UpdateAcquisitionSettings(ctx context.Context, arg UpdateAcquisitionSettingsParams) (AcquisitionSetting, error) {
@@ -600,6 +627,9 @@ func (q *Queries) UpdateAcquisitionSettings(ctx context.Context, arg UpdateAcqui
arg.SearchIntervalHours,
arg.AllowedQualities,
arg.ImportRoot,
arg.SeedRatio,
arg.SeedDays,
arg.StallHours,
)
var i AcquisitionSetting
err := row.Scan(
@@ -611,6 +641,9 @@ func (q *Queries) UpdateAcquisitionSettings(ctx context.Context, arg UpdateAcqui
&i.LastPass,
&i.UpdatedAt,
&i.ImportRoot,
&i.SeedRatio,
&i.SeedDays,
&i.StallHours,
)
return i, err
}
+5 -2
View File
@@ -30,7 +30,8 @@ SELECT COUNT(*) FILTER (WHERE fulfilled_at IS NULL AND reason <> 'missing')::big
(SELECT COUNT(*) FROM grabs WHERE ended_at IS NULL)::bigint AS activity,
(SELECT COUNT(*) FROM grabs
WHERE ended_at IS NULL AND import_state IN ('review', 'failed'))::bigint AS attention,
(SELECT COUNT(*) FROM release_blocks)::bigint AS blocked
(SELECT COUNT(*) FROM release_blocks b
WHERE b.expires_at IS NULL OR b.expires_at > now())::bigint AS blocked
FROM wants
`
@@ -537,7 +538,7 @@ SELECT w.id, w.release_group_mbid, w.artist_mbid, w.artist_name, w.title, w.reas
gr.id AS grab_id, gr.name AS grab_name, gr.grabbed_at, gr.origin AS grab_origin,
gr.quality AS grab_quality, gr.ended_at AS grab_ended_at, gr.end_reason AS grab_end_reason,
gr.progress AS grab_progress, gr.import_state AS grab_import_state,
gr.import_reason AS grab_import_reason, gr.imported_at AS grab_imported_at,
gr.import_reason AS grab_import_reason, gr.end_detail AS grab_end_detail, gr.imported_at AS grab_imported_at,
gr.imported_files AS grab_imported_files
FROM wants w
LEFT JOIN users u ON u.id = w.created_by
@@ -590,6 +591,7 @@ type ListWantsRow struct {
GrabProgress *float32
GrabImportState *string
GrabImportReason *string
GrabEndDetail *string
GrabImportedAt pgtype.Timestamptz
GrabImportedFiles *int32
}
@@ -636,6 +638,7 @@ func (q *Queries) ListWants(ctx context.Context, arg ListWantsParams) ([]ListWan
&i.GrabProgress,
&i.GrabImportState,
&i.GrabImportReason,
&i.GrabEndDetail,
&i.GrabImportedAt,
&i.GrabImportedFiles,
); err != nil {
@@ -0,0 +1,23 @@
ALTER TABLE acquisition_settings
DROP COLUMN seed_ratio,
DROP COLUMN seed_days,
DROP COLUMN stall_hours;
ALTER TABLE release_blocks DROP COLUMN expires_at;
DELETE FROM release_blocks WHERE reason = 'stalled';
ALTER TABLE release_blocks DROP CONSTRAINT release_blocks_reason_check;
ALTER TABLE release_blocks ADD CONSTRAINT release_blocks_reason_check
CHECK (reason IN ('operator', 'files', 'import'));
DROP INDEX grabs_seeding_idx;
ALTER TABLE grabs
DROP COLUMN seeding,
DROP COLUMN ratio,
DROP COLUMN seed_released_at;
UPDATE grabs SET end_reason = 'superseded' WHERE end_reason IN ('stalled', 'failed', 'abandoned', 'gone');
ALTER TABLE grabs DROP CONSTRAINT grabs_end_reason_check;
ALTER TABLE grabs ADD CONSTRAINT grabs_end_reason_check
CHECK (end_reason IN ('', 'superseded', 'imported', 'refused', 'held'));
DROP TABLE download_interventions;
@@ -0,0 +1,90 @@
-- Download lifecycle (M502 #5523), ported from Fabled Librarian's System 104:
-- a download that stalls or fails is given up and its album searched again;
-- an imported download seeds, then is removed.
--
-- download_interventions what was seen wrong with a download or a client,
-- what Minstrel did about it, and whether it
-- cleared. One open row per (client, download,
-- condition), updated in place.
-- grabs (columns) a grab also ends by being given up (stalled,
-- failed), set aside by a person, or leaving the
-- client; an imported one records its seeding.
-- release_blocks a release given up on is held back for a while,
-- not for ever: expires_at.
-- acquisition_settings how long to seed, and how long a download may sit
-- with nobody sharing it.
CREATE TABLE download_interventions (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
-- The acquisition_services code of the download client.
client text NOT NULL,
-- The torrent, or '' for a condition of the client itself.
info_hash text NOT NULL DEFAULT '',
grab_id uuid REFERENCES grabs (id) ON DELETE SET NULL,
name text NOT NULL DEFAULT '',
-- no_peers nobody is sharing an unfinished download.
-- no_metadata still fetching the torrent's file list.
-- client_error the client gave up on it, in its own words.
-- client_unreachable the client does not answer, or its engine is down.
-- gone the client no longer lists a running download.
-- not_removed given up on, and the client would not remove it.
trigger text NOT NULL CONSTRAINT download_interventions_trigger_check CHECK (trigger IN (
'no_peers', 'no_metadata', 'client_error', 'client_unreachable', 'gone', 'not_removed')),
-- escalated seen and shown; nothing done yet.
-- retried asked the trackers again and re-read the files.
-- given_up the download was removed and its album searched again.
-- removed a download left behind was removed.
action text NOT NULL DEFAULT 'escalated'
CONSTRAINT download_interventions_action_check CHECK (action IN ('escalated', 'retried', 'given_up', 'removed')),
outcome text NOT NULL DEFAULT 'open'
CONSTRAINT download_interventions_outcome_check CHECK (outcome IN ('open', 'resolved')),
detail text NOT NULL DEFAULT '',
-- How many passes have seen it, and how many times Minstrel acted on it.
-- The second is the retry ladder's rung, kept here so a restart does not
-- hand a stuck download fresh retries.
observations integer NOT NULL DEFAULT 1,
remediations integer NOT NULL DEFAULT 0,
first_seen timestamptz NOT NULL DEFAULT now(),
last_seen timestamptz NOT NULL DEFAULT now(),
settled_at timestamptz,
CONSTRAINT download_interventions_settled_together CHECK ((outcome = 'open') = (settled_at IS NULL))
);
-- One open row per condition: a pass that sees it again lands on the same row.
CREATE UNIQUE INDEX download_interventions_one_open
ON download_interventions (client, info_hash, trigger) WHERE outcome = 'open';
CREATE INDEX download_interventions_grab_idx ON download_interventions (grab_id) WHERE grab_id IS NOT NULL;
ALTER TABLE grabs DROP CONSTRAINT grabs_end_reason_check;
ALTER TABLE grabs ADD CONSTRAINT grabs_end_reason_check
CHECK (end_reason IN ('', 'superseded', 'imported', 'refused', 'held', 'stalled', 'failed', 'abandoned', 'gone'));
-- An imported download goes on sharing (the files were copied) until its
-- ratio reaches the setting or its time is up; seed_released_at is when it
-- was removed, or found gone.
ALTER TABLE grabs
ADD COLUMN seeding boolean NOT NULL DEFAULT false,
ADD COLUMN ratio real NOT NULL DEFAULT 0,
ADD COLUMN seed_released_at timestamptz;
CREATE INDEX grabs_seeding_idx ON grabs (imported_at) WHERE seeding AND seed_released_at IS NULL;
-- stalled: given up on because it would not finish; held back until
-- expires_at. NULL is for good.
ALTER TABLE release_blocks DROP CONSTRAINT release_blocks_reason_check;
ALTER TABLE release_blocks ADD CONSTRAINT release_blocks_reason_check
CHECK (reason IN ('operator', 'files', 'import', 'stalled'));
ALTER TABLE release_blocks ADD COLUMN expires_at timestamptz;
-- FL's rulings: seed to ratio 2.0 or for 14 days at most; a download nobody
-- has shared for 6 hours is retried, then given up.
-- seed_ratio 0 the download is removed once imported.
-- seed_days 0 no time limit: only the ratio ends it.
-- stall_hours 0 never give up on a download for want of peers (a failed
-- one is still given up).
ALTER TABLE acquisition_settings
ADD COLUMN seed_ratio real NOT NULL DEFAULT 2.0
CONSTRAINT acquisition_settings_seed_ratio_check CHECK (seed_ratio BETWEEN 0 AND 100),
ADD COLUMN seed_days integer NOT NULL DEFAULT 14
CONSTRAINT acquisition_settings_seed_days_check CHECK (seed_days BETWEEN 0 AND 365),
ADD COLUMN stall_hours integer NOT NULL DEFAULT 6
CONSTRAINT acquisition_settings_stall_hours_check CHECK (stall_hours BETWEEN 0 AND 720);
+15 -3
View File
@@ -51,6 +51,9 @@ UPDATE grabs
import_updated_at = now(),
import_forced = false,
imported_at = CASE WHEN sqlc.arg(import_state)::text = 'imported' THEN now() ELSE imported_at END,
-- The files were copied, so an imported download goes on sharing
-- until the lifecycle removes it (#5523).
seeding = sqlc.arg(import_state)::text = 'imported',
imported_files = sqlc.arg(imported_files),
destination = sqlc.arg(destination),
ended_at = CASE WHEN sqlc.arg(end_reason)::text <> '' THEN now() ELSE ended_at END,
@@ -110,21 +113,30 @@ SELECT DISTINCT COALESCE(t.disc_number, 1)::int AS disc, t.track_number::int AS
-- name: ListGrabs :many
-- The Wanted view's Activity tab: downloads waiting on a person first, then
-- the rest still running, then the ended ones newest first.
-- the rest still running, then the ended ones newest first. Each carries
-- what is wrong with it now, if anything (#5523).
SELECT g.id, g.want_id, g.name, g.indexer, g.client, g.quality, g.origin, g.forced,
g.grabbed_at, g.ended_at, g.end_reason, g.end_detail, g.progress, g.import_state,
g.import_reason, g.import_detail, g.import_updated_at, g.imported_at, g.imported_files,
g.destination, w.title AS want_title, w.artist_name AS want_artist
g.destination, g.seeding, g.ratio, g.seed_released_at,
w.title AS want_title, w.artist_name AS want_artist,
di.trigger AS condition, di.detail AS condition_detail, di.remediations AS condition_retries,
di.first_seen AS condition_since
FROM grabs g
LEFT JOIN wants w ON w.id = g.want_id
LEFT JOIN download_interventions di
ON di.id = (SELECT x.id FROM download_interventions x
WHERE x.grab_id = g.id AND x.outcome = 'open'
ORDER BY x.last_seen DESC LIMIT 1)
ORDER BY (g.ended_at IS NULL AND g.import_state IN ('review', 'failed')) DESC,
g.ended_at IS NULL DESC, g.grabbed_at DESC, g.id
LIMIT sqlc.arg(max_rows) OFFSET sqlc.arg(skip);
-- name: ListReleaseBlocksPage :many
SELECT b.info_hash, b.name, b.reason, b.detail, b.blocked_at, w.title AS want_title,
SELECT b.info_hash, b.name, b.reason, b.detail, b.blocked_at, b.expires_at, w.title AS want_title,
w.artist_name AS want_artist
FROM release_blocks b
LEFT JOIN wants w ON w.id = b.want_id
WHERE b.expires_at IS NULL OR b.expires_at > now()
ORDER BY b.blocked_at DESC, b.info_hash
LIMIT sqlc.arg(max_rows) OFFSET sqlc.arg(skip);
+96
View File
@@ -0,0 +1,96 @@
-- The download lifecycle (M502 #5523): the intervention ledger, giving a
-- download up, and seeding after import.
-- name: RaiseIntervention :one
-- A condition seen, or seen again: a second pass lands on the open row, so
-- a download stuck for a week is one row that says how long. The name only
-- improves, and the detail is the latest word.
INSERT INTO download_interventions (client, info_hash, grab_id, name, trigger, detail)
VALUES (sqlc.arg(client), sqlc.arg(info_hash), sqlc.narg(grab_id), sqlc.arg(name),
sqlc.arg(trigger), sqlc.arg(detail))
ON CONFLICT (client, info_hash, trigger) WHERE outcome = 'open' DO UPDATE SET
observations = download_interventions.observations + 1,
last_seen = now(),
detail = EXCLUDED.detail,
grab_id = COALESCE(EXCLUDED.grab_id, download_interventions.grab_id),
name = CASE WHEN EXCLUDED.name <> '' THEN EXCLUDED.name ELSE download_interventions.name END
RETURNING *;
-- name: SettleIntervention :execrows
-- The condition cleared.
UPDATE download_interventions
SET outcome = 'resolved', settled_at = now(), last_seen = now()
WHERE client = sqlc.arg(client) AND info_hash = sqlc.arg(info_hash)
AND trigger = sqlc.arg(trigger) AND outcome = 'open';
-- name: SettleInterventionsFor :execrows
-- Everything open about one download is over: it is gone from the client.
UPDATE download_interventions
SET outcome = 'resolved', settled_at = now(), last_seen = now()
WHERE client = sqlc.arg(client) AND info_hash = sqlc.arg(info_hash) AND outcome = 'open';
-- name: SettleEndedGrabs :execrows
-- What was wrong with a download whose grab has ended (imported, refused,
-- given up) is over, except a download left in the client to remove.
UPDATE download_interventions di
SET outcome = 'resolved', settled_at = now(), last_seen = now()
FROM grabs g
WHERE g.id = di.grab_id AND g.ended_at IS NOT NULL
AND di.outcome = 'open' AND di.trigger <> 'not_removed';
-- name: CountRemediation :one
-- Minstrel acted on an open condition. A retry climbs the ladder; giving up
-- or removing ends the condition, since what it was about is gone. Written
-- before the act, so a retry that happened is never uncounted.
UPDATE download_interventions
SET remediations = remediations + 1,
action = sqlc.arg(action),
last_seen = now(),
outcome = CASE WHEN sqlc.arg(action)::text IN ('given_up', 'removed') THEN 'resolved' ELSE outcome END,
settled_at = CASE WHEN sqlc.arg(action)::text IN ('given_up', 'removed') THEN now() ELSE settled_at END
WHERE client = sqlc.arg(client) AND info_hash = sqlc.arg(info_hash)
AND trigger = sqlc.arg(trigger) AND outcome = 'open'
RETURNING remediations;
-- name: ListLeftovers :many
-- Downloads given up on that the client would not remove; each pass asks
-- again.
SELECT * FROM download_interventions
WHERE trigger = 'not_removed' AND outcome = 'open' AND info_hash <> ''
ORDER BY first_seen;
-- name: ListClientsDown :many
-- Download clients not answering now, for the Activity tab.
SELECT client, detail, first_seen FROM download_interventions
WHERE trigger = 'client_unreachable' AND outcome = 'open'
ORDER BY client;
-- name: LiveGrabOnHash :one
SELECT EXISTS (SELECT 1 FROM grabs WHERE info_hash = sqlc.arg(info_hash) AND ended_at IS NULL)::bool;
-- name: GiveUpGrab :one
-- Ends a running grab, given up on. Only from the states named: the update
-- is the lock against an import that claimed the download meanwhile.
UPDATE grabs
SET ended_at = now(), end_reason = sqlc.arg(end_reason), end_detail = sqlc.arg(end_detail),
import_updated_at = now()
WHERE id = sqlc.arg(id) AND ended_at IS NULL AND import_state = ANY(sqlc.arg(states)::text[])
RETURNING *;
-- name: ListSeedingGrabs :many
-- Imported downloads still sharing, oldest import first.
SELECT id, client, info_hash, name, imported_at FROM grabs
WHERE seeding AND seed_released_at IS NULL
ORDER BY imported_at, id;
-- name: SetGrabRatio :exec
UPDATE grabs SET ratio = sqlc.arg(ratio) WHERE id = sqlc.arg(id);
-- name: ReleaseSeed :exec
-- The seeding download was removed, or found gone.
UPDATE grabs SET seed_released_at = now() WHERE id = $1 AND seed_released_at IS NULL;
-- name: DeleteExpiredBlocks :execrows
-- A release given up on is held back for a while; after that the search may
-- take it again.
DELETE FROM release_blocks WHERE expires_at IS NOT NULL AND expires_at <= now();
+16 -4
View File
@@ -9,6 +9,9 @@ UPDATE acquisition_settings
search_interval_hours = sqlc.arg(search_interval_hours),
allowed_qualities = sqlc.arg(allowed_qualities),
import_root = sqlc.arg(import_root),
seed_ratio = sqlc.arg(seed_ratio),
seed_days = sqlc.arg(seed_days),
stall_hours = sqlc.arg(stall_hours),
updated_at = now()
WHERE id = true
RETURNING *;
@@ -109,14 +112,23 @@ UPDATE grabs
WHERE id = sqlc.arg(id) AND ended_at IS NULL;
-- name: ListReleaseBlocks :many
SELECT * FROM release_blocks ORDER BY blocked_at DESC;
-- Every release set aside and still held back: a stalled one lifts by itself
-- at expires_at (#5523).
SELECT * FROM release_blocks
WHERE expires_at IS NULL OR expires_at > now()
ORDER BY blocked_at DESC;
-- name: BlockRelease :exec
INSERT INTO release_blocks (info_hash, name, reason, detail, want_id, blocked_by)
-- A release set aside. expires_at NULL is for good. A block for good is never
-- shortened by a timed one: only a timed block, or another for good, is
-- replaced.
INSERT INTO release_blocks (info_hash, name, reason, detail, want_id, blocked_by, expires_at)
VALUES (sqlc.arg(info_hash), sqlc.arg(name), sqlc.arg(reason), sqlc.arg(detail),
sqlc.narg(want_id), sqlc.narg(blocked_by))
sqlc.narg(want_id), sqlc.narg(blocked_by), sqlc.narg(expires_at))
ON CONFLICT (info_hash) DO UPDATE
SET reason = EXCLUDED.reason, detail = EXCLUDED.detail, blocked_at = now();
SET reason = EXCLUDED.reason, detail = EXCLUDED.detail, blocked_at = now(),
expires_at = EXCLUDED.expires_at, blocked_by = EXCLUDED.blocked_by
WHERE release_blocks.expires_at IS NOT NULL OR EXCLUDED.expires_at IS NULL;
-- name: UnblockRelease :execrows
DELETE FROM release_blocks WHERE info_hash = $1;
+3 -2
View File
@@ -194,7 +194,7 @@ SELECT w.id, w.release_group_mbid, w.artist_mbid, w.artist_name, w.title, w.reas
gr.id AS grab_id, gr.name AS grab_name, gr.grabbed_at, gr.origin AS grab_origin,
gr.quality AS grab_quality, gr.ended_at AS grab_ended_at, gr.end_reason AS grab_end_reason,
gr.progress AS grab_progress, gr.import_state AS grab_import_state,
gr.import_reason AS grab_import_reason, gr.imported_at AS grab_imported_at,
gr.import_reason AS grab_import_reason, gr.end_detail AS grab_end_detail, gr.imported_at AS grab_imported_at,
gr.imported_files AS grab_imported_files
FROM wants w
LEFT JOIN users u ON u.id = w.created_by
@@ -219,5 +219,6 @@ SELECT COUNT(*) FILTER (WHERE fulfilled_at IS NULL AND reason <> 'missing')::big
(SELECT COUNT(*) FROM grabs WHERE ended_at IS NULL)::bigint AS activity,
(SELECT COUNT(*) FROM grabs
WHERE ended_at IS NULL AND import_state IN ('review', 'failed'))::bigint AS attention,
(SELECT COUNT(*) FROM release_blocks)::bigint AS blocked
(SELECT COUNT(*) FROM release_blocks b
WHERE b.expires_at IS NULL OR b.expires_at > now())::bigint AS blocked
FROM wants;