Spaces:
Sleeping
Sleeping
File size: 1,467 Bytes
097eaf0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import { redisEvictConnection } from "../services/redis";
import { supabase_service } from "../services/supabase";
import { logger as _logger } from "./logger";
const logger = _logger.child({ module: "browser-sessions" });
const QUEUE_KEY = "browser-session-activity-queue";
const BATCH_SIZE = 500;
interface BrowserSessionActivityEvent {
team_id: string;
session_id: string;
source: "interact" | "browser";
language: string;
timeout: number;
exit_code: number | null;
killed: boolean;
created_at: string;
}
export function enqueueBrowserSessionActivity(
event: Omit<BrowserSessionActivityEvent, "created_at">,
) {
const row: BrowserSessionActivityEvent = {
...event,
created_at: new Date().toISOString(),
};
redisEvictConnection.rpush(QUEUE_KEY, JSON.stringify(row)).catch(() => {});
}
export async function processBrowserSessionActivityJobs() {
const raw = (await redisEvictConnection.lpop(QUEUE_KEY, BATCH_SIZE)) ?? [];
if (raw.length === 0) return;
const rows: BrowserSessionActivityEvent[] = raw.map(x => JSON.parse(x));
try {
const { error } = await supabase_service
.from("browser_session_activities")
.insert(rows);
if (error) {
logger.error("Failed to insert browser session activities", {
error,
count: rows.length,
});
}
} catch (err) {
logger.error("Error inserting browser session activities", {
err,
count: rows.length,
});
}
}
|