Use CoAkka Runtime inside a Temporal workflow

An order may wait overnight for approval. Keep Temporal for progress and the approval deadline. Once approved, an Activity can use CoAkka Runtime to ask the service that handles pricing.

Who owns each part of order approval?

Task Temporal CoAkka Runtime
Wait overnight for approval The Workflow waits for an approval signal or a deadline. Temporal records those events in its history. Delivers work when the application sends a request to the target. The response deadline applies to one call.
Resume after a Worker stops A new Worker uses the history to rebuild Workflow state and continue the pending step. Routes submitted calls. The Workflow or application manages the order history and the next step.
Get a quote after approval The Workflow schedules the Activity and records its result when it completes. Delivers the request to the billing.quote handler and returns the outcome of the call.
Handle a missing quote Temporal applies the Activity retry policy. The Workflow decides what to do based on the outcome. The caller sees a delivery outcome such as a reply, timeout, or Deadletter.

How does the Activity call billing.quote?

Approval path managed by Temporal
Create order → wait for approval or expiry → on approval, schedule quoteOrder
Pricing path inside the Activity
Node.js Worker → CoAkka Runtime calls billing.quote with "3" → Python calculates the price → Activity returns $36.00 to the Workflow

Make the network call inside an Activity so Temporal can use the recorded result during Workflow replay. The trial reuses one CoAkka Runtime instance across Activity calls and closes it when the trial ends.

Check what happens before retrying an Activity

The quote in this example is read-only. Each CoAkka Runtime call waits up to 2 seconds, and Temporal can run the Activity up to 2 times. For writes or charges, reuse a stable operation ID across attempts and check for a recorded outcome before repeating the operation. A timeout means the caller did not receive an outcome within its wait budget.

Distinguish timeouts and Deadletters.

When should you keep Temporal?

Keep Temporal when orders wait for a person, have a deadline, or must resume after a Worker stops. If an application only needs an immediate quote and has no progress to persist, it can call the target directly. Keep confirmation and compensation rules in the Workflow or the application that owns them.

See when a Saga needs compensation.

Run the order approval locally

Run on your own machine with Node.js and Python. The trial starts a temporary Temporal dev server. The first run needs internet access to download packages and the dev server executable.

In one terminal, follow the Python installation and start the pricing handler. Leave Python listening on 127.0.0.1:19326. Open the Node.js to Python walkthrough.

Create a separate Node.js directory. Copy the 2 files below into it, then install the dependencies and run the trial.

Workflow · wait for approval or expiry
temporal-approval-workflow.cjs
const { condition, defineQuery, defineSignal, proxyActivities, setHandler } =
  require("@temporalio/workflow");

const approveOrder = defineSignal("approveOrder");
const approvalStatus = defineQuery("approvalStatus");
const { quoteOrder } = proxyActivities({
  startToCloseTimeout: "5 seconds",
  scheduleToCloseTimeout: "20 seconds",
  retry: { maximumAttempts: 2 },
});

async function orderApproval(quantity, approvalWindow = "24 hours") {
  let approved = false;
  setHandler(approveOrder, () => { approved = true; });
  setHandler(approvalStatus, () => (approved ? "approved" : "waiting"));

  if (!(await condition(() => approved, approvalWindow))) {
    return { status: "expired" };
  }

  // The Activity performs the network call. Replay uses its recorded result.
  const quote = await quoteOrder(quantity);
  return { status: "quoted", quote };
}

module.exports = { orderApproval };
Worker and trial · call billing.quote
temporal-approval.mjs
import assert from "node:assert/strict";
import { Buffer } from "node:buffer";
import { fileURLToPath } from "node:url";
import { TestWorkflowEnvironment } from "@temporalio/testing";
import { bundleWorkflowCode, Worker } from "@temporalio/worker";
import {
  RouteStrategy,
  RuntimeHost,
  outboundOnlyNetwork,
} from "coakka-runtime-node";

const taskQueue = "checkout-approval";
const target = "billing.quote";
const runtime = RuntimeHost.start({
  systemName: "temporal-checkout",
  nodeId: "approval-worker-1",
  network: outboundOnlyNetwork(),
  routes: [{
    target,
    strategy: RouteStrategy.SINGLE_OWNER,
    endpoints: [{ host: "127.0.0.1", port: 19326, weight: 1, flags: 0 }],
  }],
});

let quoteCalls = 0;
async function quoteOrder(quantity) {
  quoteCalls += 1;
  const reply = await runtime.askPayload(
    "temporal-checkout",
    target,
    Buffer.from(String(quantity), "utf8"),
    2000,
  );
  return Buffer.from(reply).toString("utf8");
}

try {
  const env = await TestWorkflowEnvironment.createLocal();
  try {
    const workflowBundle = await bundleWorkflowCode({
      workflowsPath: fileURLToPath(new URL("./temporal-approval-workflow.cjs", import.meta.url)),
    });
    const workerOptions = {
      connection: env.nativeConnection,
      taskQueue,
      workflowBundle,
      activities: { quoteOrder },
    };

    const order = await env.client.workflow.start("orderApproval", {
      taskQueue,
      workflowId: "order-42",
      args: [3],
    });
    const firstWorker = await Worker.create(workerOptions);
    const status = await firstWorker.runUntil(() => order.query("approvalStatus"));
    assert.equal(status, "waiting");
    assert.equal(quoteCalls, 0);
    console.log("order-42: waiting for approval, worker stopped");

    // The service accepts the signal while the worker is stopped.
    await order.signal("approveOrder");
    const nextWorker = await Worker.create(workerOptions);
    await nextWorker.runUntil(async () => {
      const quoted = await order.result();
      assert.deepEqual(quoted, { status: "quoted", quote: "$36.00" });
      assert.equal(quoteCalls, 1);
      console.log("order-42: approved, quote", quoted.quote);

      const expiringOrder = await env.client.workflow.start("orderApproval", {
        taskQueue,
        workflowId: "order-43",
        args: [3, "1 second"],
      });
      assert.deepEqual(await expiringOrder.result(), { status: "expired" });
      assert.equal(quoteCalls, 1);
      console.log("order-43: approval expired, no quote call");
    });
  } finally {
    await env.teardown();
  }
} finally {
  await runtime.close();
}
Install and run
npm install 1.27.0 1.27.0 1.27.0 [email protected]
node temporal-approval.mjs

Expected result

order-42: waiting for approval, worker stopped
order-42: approved, quote $36.00
order-43: approval expired, no quote call

For the first order, the trial sends the approval signal while the Worker is stopped. A new Worker replays the history and gets the quote from Python. The second order uses a 1-second deadline and expires without a quote call. The program closes the Runtime and dev server. Stop Python with Ctrl+C when finished.

When trying this in an existing system, keep the original pricing path for comparison. Exercise both approval and expiry. To roll back the trial, use the original path for new test orders.

References