reactiveData/plt/handleTriggerExecutionResult.inc.php
2025-06-19 14:17:51 +01:00

39 lines
1.6 KiB
PHP

<?php
/**
* Handles the result of trigger execution, including error handling and updating database rows if necessary.
*
* @param int $result Exit code from the sandbox execution (0 for success, non-zero for failure).
* @param string $stdout Captured standard output from the execution (JSON encoded new row data).
* @param string $stderr Captured standard error from the execution (error message if any).
* @param array $originalRow Original database row before trigger execution.
* @param array $activeTable The active table details (including Name).
*/
function handleTriggerExecutionResult($result, $stdout, $stderr, $originalRow, $activeTable) {
$tableName = $activeTable['Name'];
if ($result !== 0) {
// Update table error status and notify via Telegram if execution failed
updateTriggerError($tableName, $stderr);
} else {
// Decode the output from JSON to array
$newRowValue = json_decode($stdout, true);
// Ignore the hydrate columns
$newRowValue = array_filter($newRowValue ?? [], fn($k) => !str_ends_with($k, '_ref'), ARRAY_FILTER_USE_KEY);
if ($newRowValue === null) {
updateTriggerError($tableName, 'Invalid JSON output from sandboxed execution.');
return;
}
// Check if the new data differs from the original data
if (json_encode($newRowValue) !== json_encode($originalRow)) {
// Update the database row accordingly
updateDatabaseRow($tableName, $originalRow, $newRowValue);
}
// Clear any previous error status
clearTriggerError($tableName);
}
}