Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions docs-developers/bridge/audible.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,22 @@ progress or anything sent to the server carries an account id.
- **promoting**: a request that finds the account's poll open raises its
priority, never lowers it.

Enqueue keeps only the newest ended poll of each account (`pruneAudiblePolls`).
Queueing a poll keeps the account's newest ended poll (`pruneAudibleEnded`,
[Job history](pairing-and-jobs.md#job-history)).
Removing an account cancels its open poll (`CancelAudibleRemoved`), and so does
deleting the library it is mapped to (`CancelAudiblePaused`). Both are
`IsCancelReason`'s, and the console keeps a copy (`SERVER_CANCEL_REASONS`).

**The scheduler** (`internal/audible.RunScheduler`, the module's `Start`) passes
at once, then every minute while the module is on. With an online Bridge that
offers the kind, it queues up to 20 due accounts at priority `-10` and records
`last_queued_at`, so a failed poll waits its interval. `catalog.DueAudiblePolls`
picks accounts that are not paused, are mapped to a person an account may still be
mapped to, and have a `last_queued_at` older than `poll_hours` (6 by default).
**The scheduler** (`internal/audible.RunScheduler`) runs when the module starts
and every minute while it is on. Each pass first cancels the open polls, syncs
and backups whose account is gone (`catalog.CancelOrphanAudibleJobs`), Bridge
online or not, which recovers the jobs an interrupted
[person delete](#on-the-server-0063-catalogaudiblego) left open. With an online
Bridge that offers the poll kind, it then queues up to 20 due accounts at
priority `-10` and records `last_queued_at`, so a failed poll waits its
interval. `catalog.DueAudiblePolls` picks accounts that are not paused, are
mapped to a person an account may still be mapped to, and have a
`last_queued_at` older than `poll_hours` (6 by default).

The console's Check now (`POST /admin/audible/accounts/{account}/poll`) queues at a
person's priority (`0`), or raises the open poll to it. A request that finds the
Expand Down Expand Up @@ -922,11 +927,10 @@ server's scan would make a file part of an existing book when it is placed in a
listed book's folder, or in a folder that already holds audio. The server refuses
such a done import (`ErrImportNotOwn`, in
[The done fold](#on-the-server-the-done-fold)). So `vacant` refuses that
destination before the license (step 3). The server refuses the one join left
before it queues anything: a `dest_path` whose folder is a disc folder ("CD 3")
directly inside an indexed book, or inside a folder with a `book` override, is
`400 invalid_dest_path` (`checkDestOwnFolder`), since the scan would join the disc
into that book and the fold would refuse it only after the whole download.
destination before the license (step 3). The server also checks disc folders when
saving destinations, templates, library mappings and folder overrides. The API
distinguishes an indexed parent book from a setting that joins its discs.
See [Audible disc destinations](../server/api/reference.md#audible-disc-destinations).

### Enrichment after placing

Expand Down Expand Up @@ -1081,11 +1085,10 @@ request. Where the Bridge finds preorders is in [Preorders](#preorders).
2. `importOwnBook` checks the book. It must be indexed in a books library, else
`ErrImportNotIndexed` (`not_indexed`). It must be the **title's own**, else
`ErrImportNotOwn` (logged; `not_own_book`, or `rejected_match` for the book
marked Not this book). The title's own book carries the title's ASIN, or holds
**only** the files the backup placed (a single-file book is its file). A file the scan joined
into a folder book already there never makes that book (nor another title's)
this title's match. Nor does it ever match the book the admin marked Not this
book for the title.
marked Not this book). Ordinarily a matching ASIN is enough. When the scan
joins a backed-up disc into its parent book, even a matching ASIN requires
the file check. In that case, or when the ASIN differs, the book must have
indexed files and every one must appear in the backup's list of placed files.
3. If the checks pass, it sets the match (`match_how = 'import'`) and clears
`rejected`, `import_pending` and any previous refusal. If it refuses the
result, it records the reason and time on the title and keeps the existing
Expand Down Expand Up @@ -1612,9 +1615,13 @@ the preorder clock skip an ignored title. Migration `0066` adds `sync_queued_at`
the book's places (`moveAudibleTitles`). So the re-match trigger offers the moved
book's place to Audible, never a stale row an earlier book left at the new path,
and a carry that fails leaves the titles where they were.
- Unpairing keeps the rows; deleting a user removes their accounts (the FK). The
delete, of a person or of an owner's household member, then cancels the gone
accounts' open poll, backups and sync (`catalog.CancelOrphanAudibleJobs`).
- Unpairing keeps the rows. Deleting a person removes their accounts and those of
the household members deleted with them; deleting one household member removes
theirs. The delete transaction records those account ids, and after commit
`catalog.CancelRemovedAudibleJobs` cancels the open polls, syncs and backups of
the ids still absent, a backup queued in an earlier library included. A delete
interrupted before that is caught by the
[scheduler's sweep](#the-poll-audiblepoll).
- The Bridge's writes are user-state writes (`store/userstate.go`): titles and the
poll times don't move the content generation; a mapping change does.
- Free text is cleaned and bounded, never refused (one odd title never fails a
Expand Down
13 changes: 13 additions & 0 deletions docs-developers/bridge/pairing-and-jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,19 @@ The Bridge may report a job it holds (`claimed`, `running` or `needs_attention`)
`running`, `done`, `failed`, `needs_attention` or `cancelled`; it can never put a
job back in the queue.

### Job history

When the server enqueues a new job, it applies the kind's history rule
(`kindSpec.prune`) in the same transaction. Each rule applies only to ended
jobs of the same kind.

| Job kind | History kept on enqueue |
|---|---|
| `audible.poll`, `audible.sync` | The newest ended poll and the newest ended sync for each Audible account. |
| `podcast.poll` | The newest ended poll for the show in that library. |
| `podcast.fetch`, `podcast.retain` | The newest ended fetch for the episode or retain for the show in that library, plus any older results the server has yet to apply (`podcast_folded`). |
| Other kinds, including `audible.import` | All of it. |

### Claims and leases

The Bridge long-polls `GET /bridge/jobs/next?wait=25&kind=…`. The server picks the
Expand Down
3 changes: 2 additions & 1 deletion docs-developers/server/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ Failures a person can fix also carry a machine-readable **`code`** next to
| `account_paused` | `409` | `POST /admin/audible/accounts/{account}/poll` (Check now), a backup or Back up everything for an account whose library was deleted, until the Bridge maps it again |
| `unknown_title` | `404` | an Audible title route (a backup, `PATCH /admin/audible/titles/{account}/{asin}`) naming no title of the account |
| `already_matched` / `import_open` / `not_available` | `409` | [`POST /admin/audible/titles/{account}/{asin}/backup`](reference.md#post-apiv1adminaudibletitlesaccountasinbackup): the title is on this server; its backup is waiting, running or pending; it is `unavailable` or `gone` |
| `invalid_dest_path` / `invalid_template` | `400` | a title's `dest_path` or an account's `template` the server refuses (the reason in `error`) |
| `invalid_dest_path` | `400` | an Audible title's `dest_path` the server refuses (what is wrong in `error`); a disc folder the scan would join into another book also carries a `reason`, on a title's or account's edit, the Bridge's library change or a `book` folder override: [Audible disc destinations](reference.md#audible-disc-destinations); a [backup](reference.md#post-apiv1adminaudibletitlesaccountasinbackup) of a title whose saved destination no longer validates carries `reason: "title"` |
| `invalid_template` | `400` | an account's `template` the server refuses (what is wrong in `error`); a backup or Back up everything with a saved template that no longer validates carries `reason: "account"` |
| `unknown_user` | `404` | the Bridge's `PUT /bridge/audible/accounts/{account}` naming no user, or one an account may not be mapped to (a kid household member, a demo or a disabled account) |
| `unknown_library` | `404` | the Bridge's `PUT /bridge/audible/accounts/{account}` or `GET /bridge/libraries/{id}/books` naming no library |
| `not_books` | `400` | the same two routes naming a podcast library |
Expand Down
76 changes: 63 additions & 13 deletions docs-developers/server/api/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -6150,9 +6150,9 @@ and left ticked. An id that isn't one of this person's members, or one
`keep_members` also names, is `400 invalid_delete_members` and nothing is
deleted.

Deleting a person, or an owner's member with them, also cancels the open
Audible jobs (polls, backups and syncs) of the accounts mapped to them (from
2.7.0).
The server cancels open Audible polls, syncs and backups only for accounts removed
with the person or their deleted household members. Members kept as independent
accounts keep their jobs.

The checks answer in this order: an id that isn't a number, then a malformed
`keep_members` (`400 invalid_keep_members`) or `delete_members` (`400
Expand Down Expand Up @@ -6589,6 +6589,11 @@ state onto it (see [Joined books](../scanner.md#joined-books-disc-sets)); on
any other folder without audio it changes nothing. Clearing it splits the discs
out again.

`"book"` is refused, and nothing saved, while an Audible title's destination, an
account's template or an open backup in this library places audio in a disc
folder directly under `path`: `400 invalid_dest_path` with `reason:
"book_override"` ([Audible disc destinations](#audible-disc-destinations)).

Response `200`: `{ "status": "override set", "path": "…", "mode": "collection" }`.
`400` for any other mode or a path over 4096 bytes (`path is too long`, before
the library is looked up); `404` library not found.
Expand Down Expand Up @@ -12258,6 +12263,40 @@ Two sets of routes, as for [the Bridge](#the-bridge):
An account id is the Bridge's own, `^[a-z0-9]{8,32}$`. On every route that names
one, an id that doesn't match is `404 unknown_account`.

### Audible disc destinations

When a destination names a disc folder such as `Shelf/CD 2`, or a file inside it,
the server checks its parent (`Shelf`) in the target library, since the scan
would join the disc into that book and the done fold would refuse it only after
the whole download. It checks on a title's `dest_path`
([PATCH](#patch-apiv1adminaudibletitlesaccountasin)), an account's `template`
([PATCH](#patch-apiv1adminaudibleaccountsaccount)), the Bridge's library change
([PUT](#put-apiv1bridgeaudibleaccountsaccount)) and a `book`
[folder override](#put-apiv1adminlibrariesidfolder-override). A conflict is
`400` with `code: "invalid_dest_path"`, a message in `error` and a `reason`.

| `reason` | Condition |
|---|---|
| `book_override` | The parent has a `book` override, or setting one would join a disc used by an Audible destination. This reason takes precedence when the parent is also an indexed book. |
| `indexed_book` | The parent is already indexed as a book and has no `book` override. |

The `reason` field is additive. Clients that read only `code` still receive
`invalid_dest_path`. An invalid path such as `../Book` gets that code without
a `reason` on a title edit.

For templates, the server checks a disc folder it can identify before the Bridge
substitutes title data. For example, `Shelf/CD 2/` and `Shelf/CD 2/{Title}.m4b`
both use a disc directly under `Shelf`. When the parent depends on title data,
the server checks the actual book and files in the
[finished backup](../../bridge/audible.md#on-the-server-the-done-fold).

An open backup keeps the library and placement it was queued with, whatever
later edits to the title or account say. So the folder override also counts the
open backups' placements: a backup that is queued, running or parked
(`needs_attention`) holds it until the job ends (`done`, `failed` or `cancelled`).
Cancelling a running backup only asks the Bridge to stop, so the hold lasts until
the Bridge reports.

### The account object

```json
Expand Down Expand Up @@ -12336,10 +12375,15 @@ the server can't tell a fresh sign-in from an edit of the label, and the sync's
places are the account's either way. Every PUT clears
[`refused_at`](#the-account-object).

When `library_id` changes, the server checks the saved template and title
destinations in the proposed library before saving the mapping. It checks saved
destinations only for unmatched titles and titles marked **Not this book**.
This also applies when a paused account gets a new library.

| Status | Meaning |
|---|---|
| `200` | `{ "account": Account }` (without `titles`) |
| `400` | `invalid_request` (the JSON); `invalid_policy`; `invalid_marketplace`; `not_books` - the library is a podcast library |
| `400` | `invalid_request` (the JSON); `invalid_policy`; `invalid_marketplace`; `not_books` - the library is a podcast library; `invalid_dest_path` with a [disc `reason`](#audible-disc-destinations) |
| `404` | `unknown_account` - an id the Bridge couldn't have made; `unknown_user` - no such user, or one an account may not be mapped to (above); `unknown_library` |

### `DELETE /api/v1/bridge/audible/accounts/{account}`
Expand Down Expand Up @@ -12604,7 +12648,7 @@ answer. The backup job itself can still be `done`.
|---|---|
| `invalid_result` | The server could not read the backup result, or it named the wrong library or lacked a usable book path. |
| `not_indexed` | The server found no indexed book at that path in the backup's books library. |
| `not_own_book` | The indexed book's ASIN did not match the title's, and the server could not verify that the book contained only files placed by this backup. |
| `not_own_book` | The server could not verify that this backup placed every indexed file in the book. It requires that check when the ASIN differs or when the scan joined the backed-up disc into its parent book, even if the ASIN matches. |
| `rejected_match` | The book at that path was marked **Not this book** for this title. |

The server clears both fields when it queues the title's next backup or
Expand Down Expand Up @@ -12645,7 +12689,10 @@ now on a poll still queued gives it the account's current template. It is quiet
(no notifications) and held while the module is off. The
module's scheduler queues each due account's poll every `poll_hours` at priority
`-10` while an online Bridge offers the kind. Only the newest ended poll of each
account is kept.
account is kept ([Job history](../../bridge/pairing-and-jobs.md#job-history)).
At start and every minute while the module is on, Bridge online or not, the
scheduler also cancels the open polls, syncs and backups of accounts that are
gone.

### `POST /api/v1/admin/audible/titles/{account}/{asin}/backup`

Expand All @@ -12660,6 +12707,7 @@ by the first poll that reports the title available.
|---|---|
| `202` | `{ "pending": false, "job_id": N }`: queued (or the title's open backup, when a request at the same moment queued it first) |
| `200` | `{ "pending": true, "job_id": null }`: a preorder, backed up on release |
| `400` | `invalid_dest_path` with reason `title` - the title's saved destination no longer validates; `invalid_template` with reason `account` - the account's saved template no longer does |
| `404` | `unknown_account`; `unknown_title` (also an `{asin}` that isn't an ASIN); `module_off` |
| `409` | `already_matched` - the title is on this server (matched, not marked "Not this book"); `import_open` - its backup is waiting, running or parked, or a preorder request is pending; `not_available` - the title is `unavailable` or `gone`; `account_paused`; `kind_unavailable` - no Bridge has reported, or the paired one doesn't run `audible.import`; `library_not_writable` - the Bridge can't write to the account's library |

Expand Down Expand Up @@ -12707,10 +12755,10 @@ out is left as it is:
account's placement: a file path, or a folder (a last segment without an audio
extension) the title's file goes in. At most 1024 bytes, clean (no `..`, no
leading or trailing `/`, no `.` segment), no hidden, dot-led segment, no
backslash or control character. From 2.7.0 it is refused (`invalid_dest_path`)
when it is a disc folder (`CD 3`) directly inside an indexed book or a folder
with a `book` override, which the scan would join into that book. It is copied
into a backup when it is queued; the Bridge checks it again against its
backslash or control character, and not a
[disc folder another book would join](#audible-disc-destinations) in the
account's library (clearing it checks the account's template instead). It is
copied into a backup when it is queued; the Bridge checks it again against its
mapping.
- `no_sync` `true` keeps the title's book out of the account's
[position sync](#audible-position-sync), both ways; `false` lets it sync again.
Expand All @@ -12723,7 +12771,7 @@ logged and the next scheduled sync carries the change.
| Status | Meaning |
|---|---|
| `200` | the [title](#get-apiv1adminaudibletitles) (not wrapped) |
| `400` | `invalid_request` - the JSON (an unknown field too); `invalid_dest_path` - with what is wrong in `error` |
| `400` | `invalid_request` - the JSON (an unknown field too); `invalid_dest_path` - with what is wrong in `error`, and a [disc `reason`](#audible-disc-destinations) for a disc folder |
| `404` | `unknown_account`; `unknown_title`; `module_off` |

Audited as **`bridge.audible.title`** (with `account`, `asin`, and `rejected`,
Expand All @@ -12745,7 +12793,9 @@ it is:
- `template`: the placement template, `""` for the Bridge's placement beside the
series' other books. At most 500 bytes, in the Bridge's
[template grammar](../../bridge/audible.md#the-template-grammar) (its variables,
mods and the segment rules the server checks).
mods and the segment rules the server checks), and not a
[disc folder another book would join](#audible-disc-destinations) in the
account's library.
- `sync`: `true` turns the account's [position sync](#audible-position-sync) on
and queues its first `audible.sync` at once (when an online Bridge offers the
kind); `false` turns it off and cancels its open sync ("sync with Audible was
Expand All @@ -12757,7 +12807,7 @@ The account's person and library stay the Bridge's.
| Status | Meaning |
|---|---|
| `200` | `{ "account": Account }` (without `titles` and `backup_estimate`) |
| `400` | `invalid_request` - the JSON (an unknown field too); `invalid_policy`; `invalid_template` - with what is wrong in `error` |
| `400` | `invalid_request` - the JSON (an unknown field too); `invalid_policy`; `invalid_template` - with what is wrong in `error`; `invalid_dest_path` with a [disc `reason`](#audible-disc-destinations) |
| `404` | `unknown_account`; `module_off` |

Audited as `bridge.audible.account` (with `account`, and `policy`, `template` and
Expand Down
Loading