hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! The podcast nest: browser ⇄ backend ⇄ the podcast service (over the QUIC |
| 2 | //! bridge), plus the doors that hand back the produced audio. |
| 3 | //! |
| 4 | //! The **backend owns the job**. `POST /podcast/jobs` mints the id, writes the |
| 5 | //! row, and only then dispatches `podcast.submit`; the service reports every |
| 6 | //! transition back (`podcast.report`) and uploads the finished mp3 |
| 7 | //! (`BlobUploadRequest`), which lands under **this school's** blob directory. |
| 8 | //! `GET /podcast/jobs/{id}` and `.../result` therefore answer from |
| 9 | //! [`crate::db::podcast_job`] alone — no worker, no service, no second store |
| 10 | //! that a restart or a wiped volume could take down with it. The service's own |
| 11 | //! record is a mirror the pipeline works against, not the source of truth. |
| 12 | //! |
| 13 | //! Six doors: |
| 14 | //! |
| 15 | //! * `POST /podcast/jobs` — write the row, dispatch `podcast.submit` |
| 16 | //! * `GET /podcast/jobs` — the caller's own history, newest first, |
| 17 | //! optionally narrowed to one course note (`?source_id=`) |
| 18 | //! * `GET /podcast/jobs/{id}` — the row's snapshot (projected, see below) |
| 19 | //! * `GET /podcast/jobs/{id}/result` — a finished job's artifact references |
| 20 | //! * `GET /podcast/jobs/{id}/audio` — streams the ingested episode |
| 21 | //! * `POST /podcast/jobs/{id}/cancel` — forward the cancel, stamp the verdict |
| 22 | //! |
| 23 | //! The two dispatching doors (`submit`, `cancel`) refuse `503` when no worker |
| 24 | //! declares their capability; the four reading doors need no service at all. |
| 25 | //! A relayed failure keeps the service's own code and decides the HTTP status |
| 26 | //! (`failure`), so a client branches on the same vocabulary the bridge speaks. |
| 27 | //! |
| 28 | //! Every door is school-scoped twice: the session resolves the school's |
| 29 | //! database (`st.db`) and the school's blob directory (`st.files_path`), and |
| 30 | //! every read is filtered by the submitting user in the statement itself — a |
| 31 | //! foreign id is a plain `404`, never a hint that it exists. Terminal rows |
| 32 | //! older than the retention window answer `410`. |
| 33 | //! |
| 34 | //! A job nobody is updating — the service died mid-pipeline — is *presented* |
| 35 | //! as `failed`/`interrupted` by the read path after a bounded, ETA-scaled |
| 36 | //! window (see [`PodcastJob::projected`]): the projection writes nothing, and |
| 37 | //! the service's next report overwrites it. |
| 38 | //! |
| 39 | //! The **sources** are resolved here, not by the service: `source_id` names a |
| 40 | //! course note in the caller's own school, and the submit door hands the |
| 41 | //! service that note's attachments — every one whose blob is on this host, in |
| 42 | //! upload order — as `sources` (`source_key` rides on as the first source's |
| 43 | //! key, one-release compat). A note with no attachments, or one whose blobs |
| 44 | //! are all missing from this host, is refused `409 source_missing` before the |
| 45 | //! row is written, so a job that could only fail is never queued. |
| 46 | |
| 47 | use Json; |
| 48 | use Body; |
| 49 | use ; |
| 50 | use HeaderValue; |
| 51 | use StatusCode; |
| 52 | use ; |
| 53 | use ; |
| 54 | use ; |
| 55 | use json; |
| 56 | use ; |
| 57 | use OpenApiRouter; |
| 58 | use routes; |
| 59 | |
| 60 | use crateAiBridge; |
| 61 | use crate; |
| 62 | use crate |
| 63 | |
| 64 | ; |
| 65 | use crateCourseNoteId; |
| 66 | use crate; |
| 67 | use crateUser; |
| 68 | use crate; |
| 69 | use cratecourse_note_file as note_files; |
| 70 | use cratepodcast_job as jobs; |
| 71 | use crateAppState; |
| 72 | use crate; |
| 73 | |
| 74 | use ; |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | /// The receipt for an accepted job: the backend's own row plus the service's |
| 100 | /// ETA. `state` is always `queued` here — the row was just written. |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | /// One job's state, from the backend's own row. |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | /// One attachment's outcome in the finished episode, as the service reported |
| 128 | /// it. |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | /// A finished job's artifacts. |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | /// The verdict on a cancel. |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | /// Start one podcast job. Answers `202` with the backend's own receipt the |
| 173 | /// moment the service accepts it. |
| 174 | /// |
| 175 | /// The row is written **before** the dispatch, so a poll that races the submit |
| 176 | /// finds the job rather than a `404`; and every dispatch outcome that is not an |
| 177 | /// acceptance leaves the row `failed` with a reason code rather than claiming |
| 178 | /// `queued` forever. With no worker connected the row is not written at all — |
| 179 | /// the `503` is the whole answer — and a note with no usable PDF never gets as |
| 180 | /// far as a row either (see below). |
| 181 | /// |
| 182 | /// `source_id` names a course note in this school, and the door resolves it |
| 183 | /// first: what the service narrates is that note's **attachments** — every |
| 184 | /// one whose blob is on this host, dispatched as `sources` in upload order |
| 185 | /// (a missing blob is skipped with a log line, not a refusal). A note with |
| 186 | /// no attachments — or none whose blob is on this host — is a `409 |
| 187 | /// source_missing` before anything is written, so a job that is guaranteed |
| 188 | /// to fail is never queued for the service to discover later. |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | async |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | /// One episode in the caller's history: the backend's own row, plus the title |
| 317 | /// of the course note it narrates. |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | /// The optional `?source_id=` filter on the history: keep only the episodes |
| 368 | /// that narrate one course note. Omitted, the list is the caller's whole |
| 369 | /// history — the same response it has always been. |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | /// The caller's own episodes, newest first — every job the backend ever |
| 406 | /// minted for this user, whatever state it stopped in, each with the title of |
| 407 | /// the note it narrated. Paged via `?limit=&offset=`. |
| 408 | /// |
| 409 | /// This is a reading door like the per-id ones: it answers from the rows |
| 410 | /// alone, so with the service down, restarted, or never connected at all the |
| 411 | /// history is still exactly what it was. A job nobody has updated inside its |
| 412 | /// ETA-scaled window is presented as `failed` (the same read-side projection |
| 413 | /// the per-id door applies); rows past the retention window stay listed — |
| 414 | /// opening one is what earns the `410`. |
| 415 | /// |
| 416 | /// `source_id` narrows the list to the jobs that narrate one course note — the |
| 417 | /// studio panel that lists a note's own episodes — while leaving the door's |
| 418 | /// school scoping, ordering and projection untouched. Omitted, the response is |
| 419 | /// the caller's whole history, unchanged. |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | async |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | /// One job's current state — the backend's own row, so this door answers with |
| 450 | /// the service down, restarted, or never connected at all. A job nobody has |
| 451 | /// updated inside its ETA-scaled window reads as `failed`/`interrupted`; that |
| 452 | /// projection is read-side only and never written. |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | async |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | /// A finished job's artifact references. A job that has not finished is a |
| 483 | /// `409 not_ready`; one the caller does not own, a `404`. |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | async |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | /// Stream one produced episode. The bytes are this school's own — ingested |
| 531 | /// under this school's blob directory — and the content type is the one the |
| 532 | /// upload declared, replayed from the row. |
| 533 | /// |
| 534 | /// A job that is not `done` is a `409 not_ready`; a row whose blob is missing |
| 535 | /// from the host is a `409 audio_missing` (the row is intact, the bytes are |
| 536 | /// not — a state a client can report, unlike a generic `500`). |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | async |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | /// Cancel one job. `cancelled` says whether *this call* stopped work — a job |
| 595 | /// that had already finished, or was already cancelled, answers `false` and is |
| 596 | /// not an error. Only a live job needs the service; a terminal one is answered |
| 597 | /// from the row without a worker. A service that has never heard of a live job |
| 598 | /// (its store was wiped) leaves the row `failed`/`interrupted`: the job cannot |
| 599 | /// finish, and the row says so. |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | async |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | /// Read one job for this caller, projected and expiry-checked — the three |
| 668 | /// reading doors' shared first act. A foreign id and an unknown id are the |
| 669 | /// same `404`; a job past its retention window is a `410`. |
| 670 | async |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | /// Why a dispatching door has no worker to talk to. Deliberately small: the |
| 682 | /// refusal is not a response — the caller builds the `503` at its own call |
| 683 | /// site, so a large, rarely-taken `Response` never rides the `Result` type of |
| 684 | /// a hot path. |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | /// The 503 gate the two dispatching doors share: no bridge configured, or no |
| 704 | /// worker declaring this door's capability. `has_capability` is documented |
| 705 | /// racy — fine here: it never guards a write, and the dispatch that follows |
| 706 | /// re-checks for real; this only spares a caller a round trip into a service |
| 707 | /// that cannot answer. |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | /// The submit door's own refusal: the note behind `source_id` cannot be |
| 719 | /// narrated — it has no attachments, or the bytes every one of them names are |
| 720 | /// gone from this host. One code (`source_missing`) for both, a message that |
| 721 | /// says which side is missing, and a `409` either way, so a client branches on |
| 722 | /// the code instead of parsing the sentence. |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | /// A relayed failure as the response a client branches on: the service's own |
| 731 | /// code decides the status where it is one this backend knows, and anything |
| 732 | /// else is a `502` — the backend cannot vouch for an answer it did not |
| 733 | /// understand, and a gateway error is exactly that claim. |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 |