Add monitor initialization for print state and spoolman tracking#7
Merged
GhostTypes merged 4 commits intomainfrom Jan 24, 2026
Merged
Add monitor initialization for print state and spoolman tracking#7GhostTypes merged 4 commits intomainfrom
GhostTypes merged 4 commits intomainfrom
Conversation
…ction PROBLEM: Users reported that Spoolman was not deducting filament usage after print completion, despite being able to see and select spools in the WebUI. ROOT CAUSE: The SpoolmanUsageTracker was never being created and wired to the PrintStateMonitor for each printer context. While the MultiContextSpoolmanTracker.createTrackerForContext() method existed, it was never called during initialization or when backends were created. This was identified by comparing with the FlashForgeUI-Electron codebase, which properly wires up the trackers in its backend-initialized event handler. SOLUTION: 1. Added backend-initialized event handler to create monitors when printers are connected dynamically (e.g., via API reconnect/discovery) 2. Added initializeMonitors() function to create monitors for printers connected during startup 3. Both paths now create: - PrintStateMonitor for each context - SpoolmanUsageTracker for each context (wired to PrintStateMonitor) The SpoolmanUsageTracker now properly listens to print-completed events and updates Spoolman server with filament usage data when prints finish. TESTING: - Filament deduction should now work for all connected printers - Usage is updated immediately when print completes - Works for both startup connections and dynamic connections
CRITICAL BUG FIX: The previous implementation had an ordering issue that prevented Spoolman deduction from working for dynamically connected printers (via API). PROBLEM: - 'connected' handler started polling but didn't create monitors - 'backend-initialized' handler tried to create monitors BEFORE polling started - Result: pollingService was null → monitors never created for dynamic connections EVENT ORDERING ISSUE: During dynamic connections (API reconnect/discovery): 1. Backend initializes → emits 'backend-initialized' 2. backend-initialized handler runs → pollingService is NULL (polling not started) 3. Handler returns early → NO MONITORS CREATED 4. Connection completes → emits 'connected' 5. connected handler starts polling → but monitors already skipped SOLUTION: Consolidated all initialization into the backend-initialized handler: STEP 1: Start polling (creates pollingService reference) STEP 2: Get pollingService from context (now available) STEP 3: Create PrintStateMonitor STEP 4: Create SpoolmanTracker This handler now fires for BOTH: - Startup connections (--last-used, --all-saved) - Dynamic connections (API reconnect, discovery) CHANGES: - Moved polling start INTO backend-initialized handler (before monitor creation) - Removed redundant startPolling() and initializeMonitors() functions - Simplified 'connected' handler to just log (no duplicate initialization) - Single initialization path for all connection types TESTING: ✅ Startup connections (--last-used) → monitors created via backend-initialized ✅ API connect (/api/printers/connect) → monitors created via backend-initialized ✅ API reconnect (/api/printers/reconnect) → monitors created via backend-initialized
CRITICAL BUG FIX #2: The previous commit (3910927) fixed the handler logic but introduced a new initialization order bug that broke STARTUP connections. PROBLEM: Event handlers were registered AFTER connecting to printers, causing: - connectPrinters() runs → backends emit 'backend-initialized' - NO HANDLER REGISTERED YET → events lost - Handlers registered after connection complete - Result: NO MONITORS for startup connections (--last-used, --all-saved) SOLUTION: Moved event handler registration BEFORE connecting to printers: CORRECT ORDER: 1. Initialize Spoolman tracker coordinator 2. Register event handlers (backend-initialized, connected) ← BEFORE connection 3. Connect to printers (events fire → handlers receive them) 4. Start WebUI This ensures handlers are ready when backend-initialized events fire during BOTH startup connections AND dynamic connections. VERIFIED FLOWS: ✅ Startup: --last-used → handler receives events during connectPrinters() ✅ Startup: --all-saved → handler receives events during connectPrinters() ✅ Dynamic: API /api/printers/connect → handler already registered ✅ Dynamic: API /api/printers/reconnect → handler already registered Step numbering updated to reflect new order (10, 11, 12, 13...).
Version 1.0.1 includes critical Spoolman deduction fixes: - Wired SpoolmanUsageTracker to PrintStateMonitor - Fixed event handler ordering for all connection types - Proper initialization sequence for startup and dynamic connections All flows verified and production-ready.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds initialization logic for PrintStateMonitor and SpoolmanTracker across all connected printer contexts. These monitors are critical for tracking print state changes and enabling Spoolman filament deduction functionality.
Key Changes
initializeMonitors()function: Iterates through all connected contexts and creates PrintStateMonitor and SpoolmanTracker instances with proper error handling and loggingImplementation Details