Files
Virtual-Box/projects/Plastico/PHASE2_COMPLETION_SUMMARY.md
2025-12-02 16:27:21 +00:00

2.5 KiB

Phase 2 Completion Summary

Goal

Add cycle persistence to work_orders table with 5-second throttling

Changes Made

Modified Files

  1. flows.json - Updated "Machine cycles" function node (ID: 0d023d87a13bf56f)

Code Modifications

Machine Cycles Function (/home/mdares/projects/Plastico/flows.json:1180)

Added throttling logic:

  • Added lastWorkOrderDbWrite global variable to track last DB write timestamp
  • Implemented 5-second throttle (5000ms) for work_orders persistence
  • Only sends DB update when timeSinceLastWrite >= 5000
  • Added debug logging for DB writes

Updated SQL query on 4th output:

UPDATE work_orders
SET cycle_count = ?,
    good_parts = ?,
    progress_percent = ?,
    updated_at = NOW()
WHERE work_order_id = ?

4th Output now sends (when throttle allows):

{
    topic: "UPDATE work_orders SET cycle_count = ?, good_parts = ?, progress_percent = ?, updated_at = NOW() WHERE work_order_id = ?",
    payload: [cycles, produced, progress, activeOrder.id]
}

Wiring Verification

Output 1 (port 0): Scrap prompt → link out + debug Output 2 (port 1): Production state → multiple consumers Output 3 (port 2): KPI trigger → multiple consumers Output 4 (port 3): DB persistence → DB Guard (Cycles) → mariaDB

How It Works

  1. Every machine cycle increments cycleCount and calculates good_parts
  2. On every cycle, check if 5 seconds have passed since last DB write
  3. If yes:
    • Send UPDATE message on output 4
    • Set lastWorkOrderDbWrite to current timestamp
    • Log the DB write operation
  4. If no:
    • Send null on output 4 (no DB write)

Data Loss Prevention

  • Maximum data loss on crash: 5 seconds of cycles (typically 1-2 cycles depending on cycle time)
  • Balances between:
    • Data persistence (resume work orders)
    • Database load (not writing every 3-15 second cycle)
    • Recovery granularity (acceptable for manufacturing use case)

Backup Created

flows.json.backup_phase2_20251129_055629

Risk Assessment

Risk Level: LOW

  • Only adds throttled writes to existing logic
  • Doesn't modify existing UI or state management
  • Default values (0) won't break existing work orders
  • DB writes use parameterized queries (SQL injection safe)

Dependencies Met

Phase 1 complete (schema has cycle_count, good_parts columns) Existing 4th output wiring to DB Guard intact MariaDB node configured and connected

Next Steps

Ready for Phase 3: Implement Resume/Restart prompt on Load