@teamkeel/functions-runtime 0.456.0 → 0.457.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -4273,15 +4273,18 @@ function createFlowContext(runId, data, action, callback, element, spanId, ctx)
4273
4273
  const failedSteps = past.filter(
4274
4274
  (step) => step.status === "FAILED" /* FAILED */
4275
4275
  );
4276
+ const runningSteps = past.filter(
4277
+ (step) => step.status === "RUNNING" /* RUNNING */
4278
+ );
4276
4279
  if (newSteps.length > 1) {
4277
4280
  throw new Error("Multiple NEW steps found for the same step");
4278
4281
  }
4279
4282
  if (completedSteps.length > 1) {
4280
4283
  throw new Error("Multiple completed steps found for the same step");
4281
4284
  }
4282
- if (completedSteps.length > 1 && newSteps.length > 1) {
4285
+ if (completedSteps.length === 1 && newSteps.length === 1) {
4283
4286
  throw new Error(
4284
- "Multiple completed and new steps found for the same step"
4287
+ "Found both a completed and a pending NEW step for the same step"
4285
4288
  );
4286
4289
  }
4287
4290
  if (completedSteps.length === 1) {
@@ -4291,9 +4294,15 @@ function createFlowContext(runId, data, action, callback, element, spanId, ctx)
4291
4294
  }
4292
4295
  if (newSteps.length === 1) {
4293
4296
  let result = null;
4294
- await db.updateTable("keel.flow_step").set({
4297
+ const claimed = await db.updateTable("keel.flow_step").set({
4298
+ status: "RUNNING" /* RUNNING */,
4295
4299
  startTime: /* @__PURE__ */ new Date()
4296
- }).where("id", "=", newSteps[0].id).returningAll().executeTakeFirst();
4300
+ }).where("id", "=", newSteps[0].id).where("status", "=", "NEW" /* NEW */).returning("id").executeTakeFirst();
4301
+ if (!claimed) {
4302
+ span.setAttribute(KEEL_INTERNAL_ATTR, KEEL_INTERNAL_CHILDREN);
4303
+ span.setAttribute("step.status", "RUNNING" /* RUNNING */);
4304
+ throw new StepCreatedDisrupt();
4305
+ }
4297
4306
  try {
4298
4307
  const stepArgs = {
4299
4308
  attempt: failedSteps.length + 1,
@@ -4331,6 +4340,11 @@ function createFlowContext(runId, data, action, callback, element, spanId, ctx)
4331
4340
  span.setAttribute("step.status", "COMPLETED" /* COMPLETED */);
4332
4341
  return result;
4333
4342
  }
4343
+ if (runningSteps.length >= 1) {
4344
+ span.setAttribute(KEEL_INTERNAL_ATTR, KEEL_INTERNAL_CHILDREN);
4345
+ span.setAttribute("step.status", "RUNNING" /* RUNNING */);
4346
+ throw new StepCreatedDisrupt();
4347
+ }
4334
4348
  await insertNewStep(db, runId, name, options.stage);
4335
4349
  span.setAttribute(KEEL_INTERNAL_ATTR, KEEL_INTERNAL_CHILDREN);
4336
4350
  span.setAttribute("step.status", "NEW" /* NEW */);