Using forkDaemon to Run Cron Job Concurrently Without Blocking Other APIs

Here the cronJob function blocks the execution of rest of the apis when it runs. I was thinking if i did forkDaemon it would run separately and not block the execution. How do i fix this?
const HttpLive = HttpApiBuilder.serve(HttpMiddleware.logger).pipe(
    HttpServer.withLogAddress,
    Layer.provide(ScalarLive),
    Layer.provide(HttpApiSwagger.layer()),
    Layer.provide(ApiLive),
    Layer.provide(NodeHttpServer.layer(createServer, { port: 3007 })),
);

const cronJob = Effect.gen(function* () {
    yield* Effect.log("Running cron job...");
    
    let r = 0;
    while (r < 500000) {
        r++;
        console.log(r);
    }
}).pipe(Effect.repeat(Schedule.fixed("10 seconds")));

const program = Effect.log("🚀  Starting server on port 3007").pipe(
    Effect.zipLeft(cronJob.pipe(Effect.forkDaemon)),
    Effect.zipLeft(Layer.launch(HttpLive)),
    Effect.tapError((e) => Effect.sync(() => console.error(e))),
);

program.pipe(
    Effect.provide(DevToolsLive),
    Effect.provide(NodeSdkLive),
    Effect.provide(EnvVars.Default),
    NodeRuntime.runMain,
);
Was this page helpful?