Skip to content

Instantly share code, notes, and snippets.

@afonsomatos
Last active September 9, 2025 08:41
Show Gist options
  • Save afonsomatos/91ef7b17ea6c9116b8a877191b197dec to your computer and use it in GitHub Desktop.
Save afonsomatos/91ef7b17ea6c9116b8a877191b197dec to your computer and use it in GitHub Desktop.
freezes
import { ClusterSchema, Entity } from "@effect/cluster";
import { NodeClusterRunnerSocket, NodeFileSystem, NodeRuntime } from "@effect/platform-node";
import { PlatformConfigProvider } from "@effect/platform/index";
import { Rpc } from "@effect/rpc";
import { PgClient } from "@effect/sql-pg/index";
import { Array, Config, DateTime, Duration, Effect, Function, Layer, Schema } from "effect";
export const EnvLoader = Layer.provide(PlatformConfigProvider.layerDotEnvAdd(".env"), NodeFileSystem.layer);
export const ClusterDatabaseLive = Layer.unwrapEffect(
Effect.gen(function* () {
const nodeEnv = yield* Config.string("NODE_ENV").pipe(Config.withDefault("development"));
const url = yield* Config.redacted("CLUSTER_DATABASE_URL");
return PgClient.layer({
url,
onnotice: Function.constVoid,
maxConnections: 5,
ssl: nodeEnv === "production" ? { rejectUnauthorized: false } : false
});
})
);
export const floor = (date: DateTime.DateTime, duration: Duration.Duration) => {
const nowMillis = DateTime.toEpochMillis(date);
const bucketMillis = Duration.toMillis(duration);
const bucketCeil = Math.floor(nowMillis / bucketMillis) * bucketMillis;
return DateTime.unsafeMake(bucketCeil);
};
const RunnerLive = NodeClusterRunnerSocket.layer({
storage: "sql",
clientOnly: true
}).pipe(Layer.provide(ClusterDatabaseLive), Layer.provide(EnvLoader));
export const SitemapRetriever = Entity.make("SitemapRetriever", [
Rpc.make("index", {
payload: {
url: Schema.String,
date: Schema.DateTimeUtc
},
primaryKey(payload) {
const roundedTime = floor(payload.date, Duration.minutes(10));
return `${payload.url}4/${DateTime.formatIso(roundedTime)}`;
},
success: Schema.Array(Schema.String),
error: Schema.Any
}).annotate(ClusterSchema.Persisted, true)
]);
export const SitemapRetrieverLive = SitemapRetriever.toLayer(
Effect.gen(function* () {
return {
index: Effect.fn("SitemapIndexer.index")(function* ({ payload: { url } }) {
// return yield* sitemap.getAllURLsFromSitemap(url);
return [];
})
};
}),
{ concurrency: 2 }
);
const dep = SitemapRetrieverLive.pipe(Layer.provideMerge(RunnerLive));
NodeRuntime.runMain(
Effect.gen(function* () {
const client = yield* SitemapRetriever.client;
const result = yield* Effect.all(
Array.makeBy(5, () => "https://www.mindfulhealthhaven.com/sitemap.xml").map(
Effect.fn(function* (x) {
// to simulate the freeze just change the client ID here to a random value and run it
yield* client("..55..").index({ url: x, date: yield* DateTime.now });
})
),
{ concurrency: 5 }
);
console.log(result.length);
}).pipe(Effect.provide(dep))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment