Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
chore(server): structured logging Phase 2 — lib job manager/runner (#336)
Gökhan Geyik4991e9fc5b83
diff --git a/server/src/lib/job-manager.js b/server/src/lib/job-manager.jsindex 8c51c6f86..96abfa0d6 100644--- a/server/src/lib/job-manager.js+++ b/server/src/lib/job-manager.js@@ -5,6 +5,7 @@*/import supabaseAdmin from '../config/supabase.js';+import logger from './logger.js';/** @type {Map<string, AbortController>} */const activeJobs = new Map();@@ -38,7 +39,7 @@ export async function updateJobProgress(jobId, progress) {.update({ progress, updated_at: new Date().toISOString() }).eq('id', jobId);- if (error) console.error(`[job-manager] Failed to update progress for ${jobId}:`, error.message);+ if (error) logger.error({ err: error, jobId }, 'failed to update job progress');}/**@@ -56,7 +57,7 @@ export async function completeJob(jobId, result) {}).eq('id', jobId);- if (error) console.error(`[job-manager] Failed to complete job ${jobId}:`, error.message);+ if (error) logger.error({ err: error, jobId }, 'failed to complete job');}/**@@ -72,7 +73,7 @@ export async function failJob(jobId, reason) {}).eq('id', jobId);- if (error) console.error(`[job-manager] Failed to fail job ${jobId}:`, error.message);+ if (error) logger.error({ err: error, jobId }, 'failed to fail job');}/**@@ -98,7 +99,7 @@ export async function markJobActive(jobId) {}).eq('id', jobId);- if (error) console.error(`[job-manager] Failed to mark job active ${jobId}:`, error.message);+ if (error) logger.error({ err: error, jobId }, 'failed to mark job active');}/**@@ -127,7 +128,7 @@ export async function cancelJob(jobId) {}).eq('id', jobId);- if (error) console.error(`[job-manager] Failed to cancel job ${jobId}:`, error.message);+ if (error) logger.error({ err: error, jobId }, 'failed to cancel job');const controller = activeJobs.get(jobId);if (controller) controller.abort();@@ -164,9 +165,9 @@ export async function cleanupStaleJobs() {.select('id');if (error) {- console.error('[job-manager] Failed to cleanup stale jobs:', error.message);+ logger.error({ err: error }, 'failed to cleanup stale jobs');} else if (data && data.length > 0) {- console.log(`[job-manager] Cleaned up ${data.length} stale active jobs`);+ logger.info({ count: data.length }, 'cleaned up stale active jobs');}}@@ -178,7 +179,7 @@ export async function cleanupOldJobs() {const { error } = await supabaseAdmin.from('jobs').delete().lt('created_at', cutoff);if (error) {- console.error('[job-manager] Failed to cleanup old jobs:', error.message);+ logger.error({ err: error }, 'failed to cleanup old jobs');}}@@ -197,8 +198,8 @@ export async function cleanupStalePendingTasks() {.lt('submitted_at', cutoff);if (error) {- console.error('[job-manager] Failed to cleanup stale pending tasks:', error.message);+ logger.error({ err: error }, 'failed to cleanup stale pending tasks');} else if (count && count > 0) {- console.log(`[job-manager] Cleaned up ${count} orphaned Cloro pending tasks`);+ logger.info({ count }, 'cleaned up orphaned cloro pending tasks');}}diff --git a/server/src/lib/job-runner.js b/server/src/lib/job-runner.jsindex a8746f871..e80974bf9 100644--- a/server/src/lib/job-runner.js+++ b/server/src/lib/job-runner.js@@ -15,6 +15,7 @@ import {} from './job-manager.js';import { processTrackingJob } from '../workers/tracking-worker.js';import { processContentJob } from '../workers/content-worker.js';+import logger from './logger.js';// Concurrency counters (matches previous Bull concurrency of 2 per queue)let activeTrackingCount = 0;@@ -69,18 +70,19 @@ export async function runTrackingJob(jobId, io) {}} catch (err) {if (abortController.signal.aborted) {- console.log(`[job-runner] Tracking job ${jobId} was cancelled`);+ logger.info({ jobId }, 'tracking job was cancelled');return;}- console.error(`[job-runner] Tracking job ${jobId} failed:`, err.message);+ logger.error({ err, jobId }, 'tracking job failed');// Re-fetch to get latest attempts countconst latest = await getJob(jobId);if (latest && latest.attempts < latest.max_attempts) {const delay = latest.attempts * 30_000; // exponential-ish backoff- console.log(- `[job-runner] Retrying tracking job ${jobId} in ${delay / 1000}s (attempt ${latest.attempts}/${latest.max_attempts})`,+ logger.info(+ { jobId, delayMs: delay, attempt: latest.attempts, attempts: latest.max_attempts },+ 'retrying tracking job',);await failJob(jobId, err.message);@@ -135,17 +137,18 @@ export async function runContentJob(jobId, io) {}} catch (err) {if (abortController.signal.aborted) {- console.log(`[job-runner] Content job ${jobId} was cancelled`);+ logger.info({ jobId }, 'content job was cancelled');return;}- console.error(`[job-runner] Content job ${jobId} failed:`, err.message);+ logger.error({ err, jobId }, 'content job failed');const latest = await getJob(jobId);if (latest && latest.attempts < latest.max_attempts) {const delay = latest.attempts * 15_000;- console.log(- `[job-runner] Retrying content job ${jobId} in ${delay / 1000}s (attempt ${latest.attempts}/${latest.max_attempts})`,+ logger.info(+ { jobId, delayMs: delay, attempt: latest.attempts, attempts: latest.max_attempts },+ 'retrying content job',);await failJob(jobId, err.message);