Skip to content

Commit 055dc32

Browse files
committed
harden example client timer workflows against random failures
1 parent 1b861b9 commit 055dc32

1 file changed

Lines changed: 35 additions & 5 deletions

File tree

example/client.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { DevTools } from "@effect/experimental";
2323
import * as NodeSocket from "@effect/platform-node/NodeSocket";
2424
import * as readline from "node:readline";
2525

26-
// Helper to create randomized delays with occasional "hangs"
26+
// Helper to create randomized delays with occasional "hangs" and failures
2727
const randomDelay = (baseMs: number, varianceMs: number) =>
2828
Effect.gen(function* () {
2929
const offset = yield* Random.nextIntBetween(-varianceMs, varianceMs + 1);
@@ -37,6 +37,12 @@ const randomDelay = (baseMs: number, varianceMs: number) =>
3737
}
3838

3939
yield* Effect.sleep(Duration.millis(delayMs));
40+
41+
// 5% chance to fail
42+
const failChance = yield* Random.nextIntBetween(0, 100);
43+
if (failChance < 5) {
44+
yield* Effect.fail(new Error("Random failure occurred"));
45+
}
4046
});
4147

4248
// Connect to the DevTools TUI server
@@ -222,13 +228,32 @@ const program = Effect.gen(function* () {
222228
break;
223229
} else if (input === "1") {
224230
console.log("Running userWorkflow...");
225-
yield* Effect.fork(userWorkflow(1));
231+
yield* Effect.fork(
232+
userWorkflow(1).pipe(
233+
Effect.tapError((error) =>
234+
Effect.log(`userWorkflow failed: ${error}`),
235+
),
236+
Effect.ignore,
237+
),
238+
);
226239
} else if (input === "2") {
227240
console.log("Running databaseQuery...");
228-
yield* Effect.fork(databaseQuery("SELECT * FROM users"));
241+
yield* Effect.fork(
242+
databaseQuery("SELECT * FROM users").pipe(
243+
Effect.tapError((error) =>
244+
Effect.log(`databaseQuery failed: ${error}`),
245+
),
246+
Effect.ignore,
247+
),
248+
);
229249
} else if (input === "3") {
230250
console.log("Running apiRequest...");
231-
yield* Effect.fork(apiRequest("/api/v1/data"));
251+
yield* Effect.fork(
252+
apiRequest("/api/v1/data").pipe(
253+
Effect.tapError((error) => Effect.log(`apiRequest failed: ${error}`)),
254+
Effect.ignore,
255+
),
256+
);
232257
} else if (input === "t") {
233258
const running = yield* Ref.get(timerRunning);
234259
if (running) {
@@ -245,7 +270,12 @@ const program = Effect.gen(function* () {
245270
Effect.repeat(
246271
Effect.gen(function* () {
247272
console.log("[Timer] Running userWorkflow...");
248-
yield* userWorkflow(1);
273+
yield* userWorkflow(1).pipe(
274+
Effect.tapError((error) =>
275+
Effect.log(`[Timer] userWorkflow failed: ${error}`),
276+
),
277+
Effect.ignore,
278+
);
249279
}),
250280
Schedule.spaced("3 seconds"),
251281
),

0 commit comments

Comments
 (0)