Always check if incoming data is newer than your current stored state:
Report incorrect code
Copy
Ask AI
def update_vehicle_state(payload): vehicle_id = payload.get("vehicleId") delivered_at = payload["meta"]["deliveredAt"] # Get current stored state current = db.get_vehicle_state(vehicle_id) # Only update if this event is newer if current and current.updated_at > delivered_at: print(f"Ignoring older event for {vehicle_id}") return # Safe to update db.update_vehicle_state(vehicle_id, payload["data"], delivered_at)
def update_signal_value(vehicle_id, signal_path, value, timestamp): """Update signal only if timestamp is newer""" with db.transaction(): current = db.query( "SELECT value, updated_at FROM signals WHERE vehicle_id = %s AND path = %s", (vehicle_id, signal_path) ) if current and current['updated_at'] >= timestamp: # Existing value is newer or same age return False # Update with newer value db.execute( "INSERT INTO signals (vehicle_id, path, value, updated_at) VALUES (%s, %s, %s, %s) ON CONFLICT (vehicle_id, path) DO UPDATE SET value = EXCLUDED.value, updated_at = EXCLUDED.updated_at WHERE signals.updated_at < EXCLUDED.updated_at", (vehicle_id, signal_path, value, timestamp) ) return True
Use idempotency check to skip already-processed events
2
Process the payload
Perform your business logic
3
Mark as processed
Store the eventId to prevent reprocessing
4
Return 200
Acknowledge successful processing
Don’t trigger retries manually. If you return a 2xx status code and then discover an issue, you cannot ask Smartcar to retry. The delivery is considered successful.