import { must } from "../../../shared/src/must.js";
import { parentWorker, singleProcessMode } from "../types/processes.js";
import { exitAfter, runUntilKilled } from "../services/life-cycle.js";
import { getShardID } from "../types/shards.js";
import { getNormalizedZeroConfig } from "../config/zero-config.js";
import { startOtelAuto } from "./otel-start.js";
import { createLogContext } from "./logging.js";
import { pgClient } from "../types/pg.js";
import { initEventSink } from "../observability/events.js";
import { CVRPurger } from "../services/view-syncer/cvr-purger.js";
import { startAnonymousTelemetry } from "./anonymous-otel-start.js";
import { ActiveUsersGauge } from "../services/view-syncer/active-users-gauge.js";
import { initViewSyncerSchema } from "../services/view-syncer/schema/init.js";
//#region ../zero-cache/src/server/reaper.ts
var MS_PER_HOUR = 1e3 * 60 * 60;
async function runWorker(parent, env, ...argv) {
	const config = getNormalizedZeroConfig({
		env,
		argv
	});
	startOtelAuto(createLogContext(config, { worker: "reaper" }, false));
	const lc = createLogContext(config, { worker: "reaper" }, true);
	initEventSink(lc, config);
	startAnonymousTelemetry(lc, config);
	const { cvr } = config;
	const shard = getShardID(config);
	const cvrDB = pgClient(lc, cvr.db, {
		max: 1,
		connection: { ["application_name"]: `zero-sync-cvr-purger` }
	});
	await initViewSyncerSchema(lc, cvrDB, shard);
	parent.send(["ready", { ready: true }]);
	return runUntilKilled(lc, parent, new CVRPurger(lc, cvrDB, shard, {
		inactivityThresholdMs: cvr.garbageCollectionInactivityThresholdHours * MS_PER_HOUR,
		initialBatchSize: cvr.garbageCollectionInitialBatchSize,
		initialIntervalMs: cvr.garbageCollectionInitialIntervalSeconds * 1e3
	}), new ActiveUsersGauge(lc, cvrDB, shard, { updateIntervalMs: 600 * 1e3 }));
}
if (!singleProcessMode()) exitAfter(() => runWorker(must(parentWorker), process.env, ...process.argv.slice(2)));
//#endregion
export { runWorker as default };

//# sourceMappingURL=reaper.js.map