Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

3.2 KB
Raw Blame
import "dotenv/config";
import { config } from "../config";
import { shutdownTracing } from "../otel";
import Express from "express";
import { logger as _logger } from "../lib/logger";
import {
  floorToMinute,
  getMsUntilNextMinute,
  runCclogTick,
} from "../lib/cclog";
import { getRedisConnection } from "./queue-service";

const CCLOG_WORKER_LOCK_KEY = "cclog:worker:tick-lock";
const CCLOG_WORKER_LOCK_TTL_SECONDS = 55;

const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

async function sleepUntilNextMinute() {
  const now = new Date();
  const nextMinute = now.getTime() + getMsUntilNextMinute(now);
  while (Date.now() < nextMinute) {
    await sleep(nextMinute - Date.now());
  }
}

async function sendHeartbeat() {
  if (config.CCLOG_WORKER_HEARTBEAT_URL) {
    try {
      const response = await fetch(config.CCLOG_WORKER_HEARTBEAT_URL, {
        signal: AbortSignal.timeout(5000),
      });
      if (!response.ok) {
        _logger.warn("cclog heartbeat got non-OK response", {
          status: response.status,
        });
      }
    } catch (error) {
      _logger.warn("Failed to send cclog heartbeat", { error });
    }
  }
}

(async () => {
  let isShuttingDown = false;
  let tickInFlight = false;

  const app = Express();
  app.get("/health", (_, res) => {
    res.status(200).send("OK");
  });

  const server = app.listen(config.CCLOG_WORKER_PORT, (error?: Error) => {
    if (error) {
      _logger.error("Failed to start cclog worker", {
        error,
        port: config.CCLOG_WORKER_PORT,
      });
      throw error;
    }

    _logger.info("cclog worker started", {
      port: config.CCLOG_WORKER_PORT,
    });
  });

  async function shutdown() {
    if (isShuttingDown) return;
    isShuttingDown = true;
    _logger.info("cclog worker shutting down");

    while (tickInFlight) {
      _logger.info("Waiting for in-flight cclog tick to complete...");
      await sleep(1000);
    }

    server.close(async () => {
      await shutdownTracing();
      _logger.info("cclog worker shut down");
      process.exit(0);
    });
  }

  if (require.main === module) {
    process.on("SIGINT", shutdown);
    process.on("SIGTERM", shutdown);
  }

  _logger.info("Waiting for next exact minute to start cclog worker", {
    waitMs: getMsUntilNextMinute(),
  });
  await sleepUntilNextMinute();

  const redis = getRedisConnection();

  while (!isShuttingDown) {
    const at = floorToMinute(new Date());
    tickInFlight = true;

    try {
      const lock = await redis.set(
        CCLOG_WORKER_LOCK_KEY,
        `${process.pid}:${at.toISOString()}`,
        "EX",
        CCLOG_WORKER_LOCK_TTL_SECONDS,
        "NX",
      );

      if (lock === "OK") {
        const summary = await runCclogTick(redis, at);
        _logger.info("cclog tick complete", {
          at: at.toISOString(),
          ...summary,
        });
        await sendHeartbeat();
      } else {
        _logger.info("Skipping cclog tick because another worker holds lock", {
          at: at.toISOString(),
        });
      }
    } catch (error) {
      _logger.error("cclog tick failed", { error });
    } finally {
      tickInFlight = false;
    }

    if (!isShuttingDown) {
      await sleepUntilNextMinute();
    }
  }
})();