2.5 KiB
2.5 KiB
Phase 2 Completion Summary
Goal
Add cycle persistence to work_orders table with 5-second throttling
Changes Made
Modified Files
- 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
lastWorkOrderDbWriteglobal 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
- Every machine cycle increments
cycleCountand calculatesgood_parts - On every cycle, check if 5 seconds have passed since last DB write
- If yes:
- Send UPDATE message on output 4
- Set
lastWorkOrderDbWriteto current timestamp - Log the DB write operation
- If no:
- Send
nullon output 4 (no DB write)
- Send
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