(name, steps, ctx)
| 10 | const logger_1 = require("./logger"); |
| 11 | const metrics_1 = require("./metrics"); |
| 12 | async function executeFlow(name, steps, ctx) { |
| 13 | const completed = []; |
| 14 | for (const step of steps) { |
| 15 | try { |
| 16 | logger_1.logger.info("flow_step_started", { event: name + "." + step.name }); |
| 17 | await step.action(ctx); |
| 18 | completed.push({ step: step.name, compensation: step.compensation }); |
| 19 | (0, metrics_1.counter)("flow.step_completed", { flow: name, step: step.name }); |
| 20 | } |
| 21 | catch (e) { |
| 22 | logger_1.logger.error("flow_step_failed", { event: name + "." + step.name, metadata: { error: e.message } }); |
| 23 | (0, metrics_1.counter)("flow.step_failed", { flow: name, step: step.name }); |
| 24 | // Backward compensation in reverse order |
| 25 | const compensated = []; |
| 26 | for (const c of [...completed].reverse()) { |
| 27 | if (!c.compensation) |
| 28 | continue; |
| 29 | try { |
| 30 | await c.compensation(ctx); |
| 31 | compensated.push(c.step); |
| 32 | logger_1.logger.info("flow_compensated", { event: name + "." + c.step }); |
| 33 | } |
| 34 | catch (compErr) { |
| 35 | logger_1.logger.error("flow_compensation_failed", { event: name + "." + c.step, metadata: { error: compErr.message } }); |
| 36 | // Continue with other compensations even if one fails |
| 37 | } |
| 38 | } |
| 39 | return { ok: false, failed_step: step.name, compensated, error: e.message }; |
| 40 | } |
| 41 | } |
| 42 | (0, metrics_1.counter)("flow.completed", { flow: name }); |
| 43 | return { ok: true, compensated: [] }; |
| 44 | } |
| 45 | // Flow: improvement_loop |
| 46 | // ctx must contain: { req, res, auth, client? } for transactional flows |
| 47 | async function execute_improvement_loop(ctx) { |
no test coverage detected