Effect CommunityEC
Effect Community7mo ago
12 replies
Victor Korzunin

DurableClock Sleep Duration Doubled in Workflow Example

why in this simple Workflow example dutable clock sleeps twice of the time:
import { ClusterWorkflowEngine } from "@effect/cluster";
import { NodeClusterRunnerSocket, NodeRuntime } from "@effect/platform-node";
import { DurableClock } from "@effect/workflow";
import { Effect, Layer } from "effect";
import { SqlLayer } from "./Sql";
import { EmailWorkflow } from "./workflow";

// Once you have defined the workflow, you can create a layer for by providing
// the implementation.
const EmailWorkflowLayer = EmailWorkflow.toLayer(
  Effect.fn(function* (payload, executionId) {
    // Use the `DurableClock` to sleep for a specified duration.
    // The workflow will pause execution for the specified duration.
    //
    // You can sleep for as long as you want - when the workflow pauses it
    // consumes no resources.
    yield* Effect.log("Sleeping for 10 seconds");
    yield* DurableClock.sleep({
      name: "Some sleep",
      duration: "10 seconds",
    });
    yield* Effect.log("Woke up");

    return "done";
  })
);

// To integrate with @effect/cluster, you can use the
// `ClusterWorkflowEngine.layer` Layer, and provide it with your cluster Runner
// layer.
const WorkflowEngineLayer = ClusterWorkflowEngine.layer.pipe(
  Layer.provideMerge(
    NodeClusterRunnerSocket.layer({
      storage: "sql",
      shardingConfig: { shardsPerGroup: 4 },
    })
  ),
  Layer.provideMerge(SqlLayer)
);

const EnvLayer = Layer.mergeAll(
  EmailWorkflowLayer
  // You can add any other cluster entities or workflow layers here
).pipe(Layer.provide(WorkflowEngineLayer));

Layer.launch(EnvLayer).pipe(NodeRuntime.runMain);

output:
[22:48:17.118] INFO (#1): Listening on: localhost:34431
service: Runner
package: @effect/cluster
[22:48:27.139] INFO (#55): Sleeping for 10 seconds
[22:48:47.158] INFO (#63): Sleeping for 10 seconds
[22:48:47.163] INFO (#63): Woke up
id: 8
executionId: 9ad1fc4a7f21a85388b4d27ecccdbc91
Was this page helpful?