print_queue.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. from datetime import datetime
  2. from sqlalchemy import Boolean, DateTime, Float, ForeignKey, Integer, String, Text, false, func
  3. from sqlalchemy.orm import Mapped, mapped_column, relationship
  4. from backend.app.core.database import Base
  5. class PrintQueueItem(Base):
  6. """Print queue item for scheduled/queued prints."""
  7. __tablename__ = "print_queue"
  8. id: Mapped[int] = mapped_column(primary_key=True)
  9. # Links
  10. printer_id: Mapped[int | None] = mapped_column(ForeignKey("printers.id", ondelete="CASCADE"), nullable=True)
  11. # Target printer model for model-based assignment (mutually exclusive with printer_id)
  12. # When set, scheduler assigns to any idle printer of matching model
  13. target_model: Mapped[str | None] = mapped_column(String(50), nullable=True)
  14. # Target location filter for model-based assignment (only used with target_model)
  15. # When set, only printers in this location are considered
  16. target_location: Mapped[str | None] = mapped_column(String(100), nullable=True)
  17. # Required filament types for model-based assignment (JSON array, e.g., '["PLA", "PETG"]')
  18. # Used by scheduler to validate printer has compatible filaments loaded
  19. required_filament_types: Mapped[str | None] = mapped_column(Text, nullable=True)
  20. # Waiting reason - explains why a model-based job hasn't started yet
  21. # Set by scheduler when no matching printer is available
  22. waiting_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
  23. # Either archive_id OR library_file_id must be set (archive created at print start from library file)
  24. archive_id: Mapped[int | None] = mapped_column(ForeignKey("print_archives.id", ondelete="CASCADE"), nullable=True)
  25. library_file_id: Mapped[int | None] = mapped_column(
  26. ForeignKey("library_files.id", ondelete="CASCADE"), nullable=True
  27. )
  28. cost_center_id: Mapped[int | None] = mapped_column(
  29. ForeignKey("cost_centers.id", ondelete="SET NULL"), nullable=True
  30. )
  31. estimated_cost: Mapped[float | None] = mapped_column(Float, nullable=True)
  32. # Bambuddy-owned globally unique identity for one physical dispatch. This
  33. # must not reuse the printer protocol's 31-bit subtask_id.
  34. billing_run_id: Mapped[str | None] = mapped_column(String(36), nullable=True)
  35. project_id: Mapped[int | None] = mapped_column(ForeignKey("projects.id", ondelete="SET NULL"), nullable=True)
  36. batch_id: Mapped[int | None] = mapped_column(ForeignKey("print_batches.id", ondelete="SET NULL"), nullable=True)
  37. # Scheduling
  38. position: Mapped[int] = mapped_column(Integer, default=0) # Queue order
  39. scheduled_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) # None = ASAP
  40. manual_start: Mapped[bool] = mapped_column(Boolean, default=False) # Requires manual trigger to start
  41. # Conditions
  42. require_previous_success: Mapped[bool] = mapped_column(Boolean, default=False)
  43. # Power management
  44. auto_off_after: Mapped[bool] = mapped_column(Boolean, default=False) # Power off printer after print
  45. # AMS mapping: JSON array of global tray IDs for each filament slot
  46. # Format: "[5, -1, 2, -1]" where position = slot_id-1, value = global tray ID (-1 = unused)
  47. ams_mapping: Mapped[str | None] = mapped_column(Text, nullable=True)
  48. # The slicer's own AMS pick for a virtual-printer upload with "Save AMS
  49. # mapping" on (#2700), same format as ams_mapping. Not used for this
  50. # print (ams_mapping is): it goes onto the archive the dispatch creates
  51. # (extra_data.slicer_ams_mapping, for printer_id), so a reprint of that
  52. # archive can reuse it. Kept apart because "Force color match" leaves
  53. # ams_mapping empty and still wants the pick saved.
  54. slicer_ams_mapping: Mapped[str | None] = mapped_column(Text, nullable=True)
  55. # Filament overrides for model-based assignment: JSON array of override objects
  56. # Format: '[{"slot_id": 1, "type": "PLA", "color": "#FFFFFF"}]'
  57. # Only slots with overrides are included (sparse). null = use original 3MF values.
  58. filament_overrides: Mapped[str | None] = mapped_column(Text, nullable=True)
  59. # Plate ID for multi-plate 3MF files (1-indexed, None = auto-detect/plate 1)
  60. plate_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
  61. # Shortest-job-first scheduling
  62. print_time_seconds: Mapped[int | None] = mapped_column(Integer, nullable=True) # Cached from archive/library
  63. been_jumped: Mapped[bool] = mapped_column(Boolean, default=False) # Starvation guard for SJF
  64. # Auto-print G-code injection (#422)
  65. gcode_injection: Mapped[bool] = mapped_column(Boolean, default=False)
  66. # How many times the start-watchdog has reverted this item from 'printing'
  67. # back to 'pending' (#2555). A printer that accepts project_file but never
  68. # starts (#1678) used to be retried forever: upload, wait out the watchdog,
  69. # revert, upload again — burning a full 3MF transfer per cycle and, with
  70. # the queue dispatching serially, dragging every other printer's start time
  71. # out with it. The counter bounds that loop; see DISPATCH_MAX_ATTEMPTS.
  72. dispatch_attempts: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
  73. # H2C dual-nozzle-rack slicer pick preservation (#1780). BambuStudio's
  74. # project_file MQTT command for rack-swap-capable models (O1C2 today)
  75. # carries per-filament physical nozzle position IDs in `nozzle_mapping`,
  76. # forwarded verbatim through the queue and replayed by the dispatcher so
  77. # the firmware honours the user's pick instead of falling back to
  78. # "last matching nozzle type" auto-pick. Stored as opaque JSON string
  79. # (list[int]); NULL on every other model. `nozzles_info` is a deprecated
  80. # column from the original #1780 attempt — kept nullable so old rows still
  81. # load; never written to or read from.
  82. nozzle_mapping: Mapped[str | None] = mapped_column(Text, nullable=True)
  83. nozzles_info: Mapped[str | None] = mapped_column(Text, nullable=True)
  84. # Which rack position each filament group prints from, on a nozzle-rack
  85. # machine (#1784). JSON object keyed by the 3MF's group id, valued with a
  86. # 1-based rack position as the operator counts them.
  87. #
  88. # Deliberately not the expanded `nozzle_mapping` above, though that is what
  89. # goes on the wire: the rack can be re-loaded between queueing and
  90. # dispatch, and only the position-and-group form can be re-checked against
  91. # what is actually mounted at the moment the job runs. NULL means nothing
  92. # was picked, and the dispatcher assigns positions itself.
  93. nozzle_rack_choice: Mapped[str | None] = mapped_column(Text, nullable=True)
  94. # Slots sent with a spool of another colour than the file asked for (#3279),
  95. # as a JSON list of {slot_id, type, wanted, got, tray}. Worked out from the
  96. # mapping each time the job is sent, so a retry replaces it. NULL when every
  97. # slot got its colour or nothing could be compared.
  98. color_substitutions: Mapped[str | None] = mapped_column(Text, nullable=True)
  99. # Printer-card direct uploads create transient library rows, and a queue
  100. # virtual printer can ask for its uploads to go once they print. When this
  101. # is true, the scheduler deletes the source row/files after archiving a copy.
  102. cleanup_library_after_dispatch: Mapped[bool] = mapped_column(Boolean, default=False, server_default=false())
  103. # Print options. bed_levelling / flow_cali / nozzle_offset_cali are tri-state
  104. # strings (off/on/auto) matching BambuStudio; "auto" = skip if recently done.
  105. # The remaining three stay boolean (BambuStudio exposes no auto for them).
  106. bed_levelling: Mapped[str] = mapped_column(String(8), default="auto", server_default="auto")
  107. flow_cali: Mapped[str] = mapped_column(String(8), default="auto", server_default="auto")
  108. vibration_cali: Mapped[bool] = mapped_column(Boolean, default=True)
  109. layer_inspect: Mapped[bool] = mapped_column(Boolean, default=False)
  110. timelapse: Mapped[bool] = mapped_column(Boolean, default=False)
  111. use_ams: Mapped[bool] = mapped_column(Boolean, default=True)
  112. # Nozzle offset calibration — dual-nozzle printers only, MQTT-gated (#1682)
  113. nozzle_offset_cali: Mapped[str] = mapped_column(String(8), default="auto", server_default="auto")
  114. # Ask for a post-print outcome verdict when this job completes (#1898).
  115. # Copied onto the archive as confirm_requested at dispatch, like plate_id.
  116. confirm_outcome: Mapped[bool] = mapped_column(Boolean, default=False, server_default="0")
  117. # Preheat / heat-soak override (#1468). 'inherit' uses the global
  118. # preheat_enabled setting; 'on' / 'off' force the per-item decision. The
  119. # chamber target falls through: per-item override → max(filament-map[loaded
  120. # tray type]) → 0 (skips chamber phase). 'inherit' + global off + override
  121. # null = no preheat. Default 'inherit' so existing queue items behave
  122. # exactly as before the migration.
  123. preheat_override: Mapped[str] = mapped_column(String(10), default="inherit", server_default="inherit")
  124. preheat_chamber_target_override: Mapped[int | None] = mapped_column(Integer, nullable=True)
  125. # Status: pending, printing, completed, failed, skipped, cancelled
  126. status: Mapped[str] = mapped_column(String(20), default="pending")
  127. # Dispatch claim (#2615). Set atomically by the scheduler the moment it
  128. # begins dispatching this row and cleared when dispatch ends. The row stays
  129. # `status='pending'` throughout the (slow) FTP upload, which left a window
  130. # where a concurrent PATCH could reassign printer_id mid-upload and split the
  131. # queue row from the archive/expected-print/physical command. While this is
  132. # set the edit routes reject changes (409) and the scheduler won't re-select
  133. # the row. Startup reconciliation clears any left over by a crash mid-dispatch
  134. # (no coroutine survives a restart), so a stale claim never wedges an item.
  135. dispatching_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  136. # Cleared by the per-printer "Resume after failure" action (#1818) so the
  137. # scheduler's `_check_previous_success` lookback skips this row. Without
  138. # this, a single `failed` or `aborted` print poisoned every later
  139. # `require_previous_success` item on the same printer forever — the
  140. # lookback excluded `skipped` but had no way to dismiss the originating
  141. # failure. The flag is per-item, not per-printer, so a fresh failure
  142. # after a resume re-gates downstream items independently.
  143. gate_acknowledged: Mapped[bool] = mapped_column(Boolean, default=False, server_default=false())
  144. # Set by the dispatch scheduler when the assigned spool can't satisfy
  145. # this print's per-slot filament weight (#1496). Display-only flag — the
  146. # actual deficit is recomputed live every time the user clicks ▶, so
  147. # swapping a spool to a fuller one between flag and dispatch clears the
  148. # block automatically.
  149. filament_short: Mapped[bool] = mapped_column(Boolean, default=False, server_default=false())
  150. # User has acknowledged the filament-shortage warning for this item
  151. # ("Print Anyway"). Set by the start route when the user passes
  152. # skip_filament_check=true, or at queue-creation time if PrintModal's
  153. # frontend deficit warning was acknowledged. Survives scheduler ticks so
  154. # the dispatch no longer bounces between "user said anyway" and
  155. # "scheduler re-flagged" (#1698-followup).
  156. skip_filament_check: Mapped[bool] = mapped_column(Boolean, default=False, server_default=false())
  157. # Tracking
  158. started_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  159. completed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  160. error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
  161. # Timestamps
  162. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  163. # User tracking (who added this to the queue)
  164. created_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
  165. # Relationships
  166. printer: Mapped["Printer"] = relationship()
  167. archive: Mapped["PrintArchive | None"] = relationship()
  168. library_file: Mapped["LibraryFile | None"] = relationship()
  169. cost_center: Mapped["CostCenter | None"] = relationship()
  170. project: Mapped["Project | None"] = relationship(back_populates="queue_items")
  171. batch: Mapped["PrintBatch | None"] = relationship(back_populates="queue_items")
  172. created_by: Mapped["User | None"] = relationship()
  173. variants: Mapped[list["PrintQueueVariant"]] = relationship(
  174. back_populates="queue_item",
  175. cascade="all, delete-orphan",
  176. order_by="PrintQueueVariant.position",
  177. )
  178. class PrintQueueVariant(Base):
  179. """One candidate file for a queue item that may print on several models (#671).
  180. A user with an H2S and an H2C slices the same job twice and does not care
  181. which machine runs it. Each slice becomes a variant; the scheduler walks them
  182. in ``position`` order and takes the first whose model has an idle printer.
  183. **This is a snapshot, not a pointer.** The candidate list is copied from the
  184. library's variant group when the item is queued, and every per-file setting
  185. the dispatcher needs is copied with it. Two reasons:
  186. - Editing the library group afterwards must not silently change a job that is
  187. already waiting in the queue.
  188. - The per-file settings genuinely differ between candidates and are choices
  189. the user made for *this* job, not properties of the file. An H2C slice is
  190. dual-nozzle and will not have the same slot count, AMS mapping or nozzle
  191. mapping as the H2S slice of the same model.
  192. On a match the winning variant's fields are written onto the queue row before
  193. the dispatch commit, so everything downstream — upload, archive creation,
  194. print history, reprint — sees an ordinary single-file item and needs no
  195. knowledge that variants exist.
  196. Variants reference library files only. An archive records a print that already
  197. happened, of one specific file, so it is never a candidate for "which of these
  198. should we run".
  199. """
  200. __tablename__ = "print_queue_variants"
  201. id: Mapped[int] = mapped_column(primary_key=True)
  202. queue_item_id: Mapped[int] = mapped_column(
  203. ForeignKey("print_queue.id", ondelete="CASCADE"), nullable=False, index=True
  204. )
  205. # User's priority order. When two printers are idle in the same scheduler
  206. # pass, the lowest position wins — so the choice is reproducible instead of
  207. # depending on which match the matcher happened to find first.
  208. position: Mapped[int] = mapped_column(Integer, default=0)
  209. # CASCADE: deleting the file drops this candidate but leaves the item and its
  210. # other candidates alone. Losing the *last* candidate is handled by the
  211. # resolver, which holds the item pending with an explicit waiting_reason
  212. # rather than letting it sit there looking dispatchable forever.
  213. library_file_id: Mapped[int] = mapped_column(ForeignKey("library_files.id", ondelete="CASCADE"), nullable=False)
  214. # Normalized short name ("H2S"), taken from the file's own sliced_for_model
  215. # at creation, or picked by the user for a legacy file that declares none.
  216. target_model: Mapped[str] = mapped_column(String(50), nullable=False)
  217. # Per-file dispatch settings, same semantics as the identically named columns
  218. # on PrintQueueItem — see there for the formats.
  219. plate_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
  220. ams_mapping: Mapped[str | None] = mapped_column(Text, nullable=True)
  221. nozzle_mapping: Mapped[str | None] = mapped_column(Text, nullable=True)
  222. nozzle_rack_choice: Mapped[str | None] = mapped_column(Text, nullable=True)
  223. filament_overrides: Mapped[str | None] = mapped_column(Text, nullable=True)
  224. required_filament_types: Mapped[str | None] = mapped_column(Text, nullable=True)
  225. print_time_seconds: Mapped[int | None] = mapped_column(Integer, nullable=True)
  226. # How many times this candidate has been dispatched and bounced back to
  227. # pending by the start-watchdog. The resolver tries least-attempted first, so
  228. # a printer that accepts the file and never starts (#1678) hands the job to
  229. # the other machine on the next lap instead of burning the item's whole
  230. # DISPATCH_MAX_ATTEMPTS budget against the same wedged printer — which is the
  231. # entire reason the user queued an alternative.
  232. attempt_count: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
  233. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  234. queue_item: Mapped["PrintQueueItem"] = relationship(back_populates="variants")
  235. library_file: Mapped["LibraryFile"] = relationship()
  236. from backend.app.models.archive import PrintArchive # noqa: E402
  237. from backend.app.models.finance import CostCenter # noqa: E402
  238. from backend.app.models.library import LibraryFile # noqa: E402
  239. from backend.app.models.print_batch import PrintBatch # noqa: E402
  240. from backend.app.models.printer import Printer # noqa: E402
  241. from backend.app.models.project import Project # noqa: E402
  242. from backend.app.models.user import User # noqa: E402