Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.
TypeScript ai-agentai-resume-analyzerapplication-trackerapplication-tracking-systematscareerjob-applicationjob-automationjob-huntingjob-searchjob-seeker-toolsjob-trackerjob-trackingjobsearchjobseekersmcpollamaresumeresume-reviewself-hosted
feat: add partial unique indexes to prevent duplicate automation runs and discovered jobs
Khuram Niaz3d13ac8bf000
diff --git a/prisma/migrations/20260710000001_automation_run_single_active/migration.sql b/prisma/migrations/20260710000001_automation_run_single_active/migration.sqlnew file mode 100644index 0000000..338c039--- /dev/null+++ b/prisma/migrations/20260710000001_automation_run_single_active/migration.sql@@ -0,0 +1,12 @@+-- Prevents two AutomationRun rows from being "running"/"cancelling" at once+-- for the same automation. The prior guard (SELECT-then-INSERT inside a+-- $transaction) assumed SQLite serializes concurrent write transactions,+-- but deferred transactions don't take a write lock until the first write,+-- so two concurrent runAutomation() calls could both pass the SELECT check+-- before either INSERTed, producing duplicate concurrent runs (and, in turn,+-- duplicate discovered jobs since each run computed its own dedup snapshot).+-- Partial index (not expressible via Prisma's schema DSL) so historical+-- completed/failed/cancelled runs are unaffected.+CREATE UNIQUE INDEX "AutomationRun_automationId_active_key"+ON "AutomationRun"("automationId")+WHERE "status" IN ('running', 'cancelling');diff --git a/prisma/migrations/20260710000002_job_automation_url_unique/migration.sql b/prisma/migrations/20260710000002_job_automation_url_unique/migration.sqlnew file mode 100644index 0000000..6e76ce4--- /dev/null+++ b/prisma/migrations/20260710000002_job_automation_url_unique/migration.sql@@ -0,0 +1,9 @@+-- Guards against duplicate automation-discovered jobs at the DB level, since+-- app-level dedup (dedupeJobs in scraper/utils.ts) reads existing URLs at the+-- start of a run and can't see another concurrent run's in-flight saves.+-- Scoped to automationId IS NOT NULL so it never affects manually-created+-- jobs, which legitimately reuse placeholder/generic URLs. Partial index,+-- not expressible via Prisma's schema DSL.+CREATE UNIQUE INDEX "Job_userId_jobUrl_automation_key"+ON "Job"("userId", "jobUrl")+WHERE "automationId" IS NOT NULL;diff --git a/prisma/schema.prisma b/prisma/schema.prismaindex 3758aa8..1744d9f 100644--- a/prisma/schema.prisma+++ b/prisma/schema.prisma@@ -325,6 +325,9 @@ model Job {@@index([userId, automationId])@@index([userId, discoveryStatus])+ // Also has a hand-written partial unique index (not expressible here):+ // UNIQUE(userId, jobUrl) WHERE automationId IS NOT NULL+ // See migrations/20260710000002_job_automation_url_unique}model ActivityType {@@ -439,6 +442,9 @@ model AutomationRun {@@index([automationId])@@index([startedAt])+ // Also has a hand-written partial unique index (not expressible here):+ // UNIQUE(automationId) WHERE status IN ('running', 'cancelling')+ // See migrations/20260710000001_automation_run_single_active}model Note {diff --git a/src/app/api/automations/[id]/run/route.ts b/src/app/api/automations/[id]/run/route.tsindex 844d087..b63b4ec 100644--- a/src/app/api/automations/[id]/run/route.ts+++ b/src/app/api/automations/[id]/run/route.ts@@ -3,7 +3,11 @@ import "server-only";import { NextRequest, NextResponse } from "next/server";import { auth } from "@/auth";import db from "@/lib/db";-import { runAutomation, getUserAiSettings } from "@/lib/scraper";+import {+ runAutomation,+ getUserAiSettings,+ AutomationAlreadyRunningError,+} from "@/lib/scraper";import { PROVIDER_VERIFIERS } from "@/lib/ai/provider-registry.server";import { getOllamaBaseUrl } from "@/actions/apiKey.actions";import { AiProvider } from "@/models/ai.model";@@ -127,6 +131,10 @@ export async function POST(createdAt: automation.createdAt,updatedAt: automation.updatedAt,}).catch((err) => {+ if (err instanceof AutomationAlreadyRunningError) {+ console.log(`Skipping manual run for ${automation.id} - run already in progress`);+ return;+ }console.error("Background automation run failed:", err);});diff --git a/src/components/automations/DiscoveredJobDetail.tsx b/src/components/automations/DiscoveredJobDetail.tsxindex 8fa6b6f..39b6c49 100644--- a/src/components/automations/DiscoveredJobDetail.tsx+++ b/src/components/automations/DiscoveredJobDetail.tsx@@ -16,7 +16,6 @@ import { toast } from "@/components/ui/use-toast";import {Building2,MapPin,- ExternalLink,Check,X,Loader2,@@ -25,6 +24,7 @@ import {import type { DiscoveredJob } from "@/models/automation.model";import type { JobMatchData } from "@/models/ai.schemas";import { getWorkplaceTypeLabel } from "@/models/job.model";+import { formatUrl } from "@/lib/utils";import {acceptDiscoveredJob,dismissDiscoveredJob,@@ -130,19 +130,7 @@ export function DiscoveredJobDetail({<DialogHeader><div className="flex items-start justify-between gap-4 pr-8"><div className="min-w-0 space-y-1.5">- <DialogTitle className="flex items-center gap-2">- {job.JobTitle.label}- {job.jobUrl && (- <a- href={job.jobUrl}- target="_blank"- rel="noopener noreferrer"- className="shrink-0 text-muted-foreground hover:text-foreground"- >- <ExternalLink className="h-4 w-4" />- </a>- )}- </DialogTitle>+ <DialogTitle>{job.JobTitle.label}</DialogTitle><DialogDescription className="flex flex-wrap items-start gap-x-4 gap-y-1"><span className="flex items-start gap-1"><Building2 className="h-4 w-4 shrink-0 mt-0.5" />@@ -186,6 +174,19 @@ export function DiscoveredJobDetail({<ScrollArea className="max-h-[60vh]"><div className="space-y-4 pr-4">+ {job.jobUrl && (+ <div>+ <span className="font-semibold mr-2">Job URL:</span>+ <a+ href={formatUrl(job.jobUrl)}+ target="_blank"+ rel="noopener noreferrer"+ >+ {job.jobUrl}+ </a>+ </div>+ )}+<div className="flex items-center gap-4"><Badge variant="outline">{job.discoveryStatus}</Badge>{job.automation && (diff --git a/src/lib/scheduler/index.ts b/src/lib/scheduler/index.tsindex c930a73..5edc5fc 100644--- a/src/lib/scheduler/index.ts+++ b/src/lib/scheduler/index.ts@@ -1,7 +1,7 @@import cron, { ScheduledTask } from "node-cron";import { SCHEDULER_CONSTANTS } from "@/lib/constants";import db from "@/lib/db";-import { runAutomation } from "@/lib/scraper";+import { runAutomation, AutomationAlreadyRunningError } from "@/lib/scraper";import type { JobBoard } from "@/models/automation.model";let scheduledTask: ScheduledTask | null = null;@@ -78,6 +78,10 @@ async function runDueAutomations() {});console.log(`[Scheduler] Automation ${automation.name} completed: ${result.status}, saved ${result.jobsSaved} jobs`);} catch (error) {+ if (error instanceof AutomationAlreadyRunningError) {+ console.log(`[Scheduler] Skipping automation ${automation.id} - run already in progress`);+ continue;+ }const message = error instanceof Error ? error.message : "Unknown error";console.error(`[Scheduler] Automation ${automation.name} failed:`, message);}diff --git a/src/lib/scraper/index.ts b/src/lib/scraper/index.tsindex aaeae3c..c111cf9 100644--- a/src/lib/scraper/index.ts+++ b/src/lib/scraper/index.ts@@ -23,5 +23,6 @@ export {export {runAutomation,getUserAiSettings,+ AutomationAlreadyRunningError,type RunnerResult,} from "./runner";diff --git a/src/lib/scraper/runner.ts b/src/lib/scraper/runner.tsindex 25fa7fb..b73b0a5 100644--- a/src/lib/scraper/runner.ts+++ b/src/lib/scraper/runner.ts@@ -153,20 +153,47 @@ interface ResumeWithSections extends PrismaResume {}>;}+// Thrown by runAutomation's atomic claim when another run is already active+// for this automation. Callers (scheduler, manual-run route) already do their+// own pre-check for a fast/clean skip, but that check-then-act isn't atomic on+// its own — two near-simultaneous callers can both pass it before either has+// written a "running" row. This error signals the loser of that race so it+// can skip instead of starting a duplicate run.+export class AutomationAlreadyRunningError extends Error {+ constructor(automationId: string) {+ super(`Automation ${automationId} already has an active run`);+ this.name = "AutomationAlreadyRunningError";+ }+}+export async function runAutomation(automation: Automation,signal?: AbortSignal,): Promise<RunnerResult> {+ // Atomic claim: a partial unique index (AutomationRun_automationId_active_key,+ // see migrations/20260710000001_automation_run_single_active) allows at most+ // one running/cancelling row per automation. A prior SELECT-then-INSERT+ // $transaction assumed SQLite serializes concurrent write transactions,+ // but deferred transactions don't take a write lock until the first write,+ // so two racing callers could both pass the SELECT before either INSERTed —+ // relying on the DB constraint instead closes that race for good.+ let run;+ try {+ run = await db.automationRun.create({+ data: {+ automationId: automation.id,+ status: "running",+ },+ });+ } catch (error: any) {+ if (error?.code === "P2002") {+ throw new AutomationAlreadyRunningError(automation.id);+ }+ throw error;+ }+console.log(`[Automation ${automation.id}] Starting automation run`);automationLogger.startRun(automation.id);-- const run = await db.automationRun.create({- data: {- automationId: automation.id,- status: "running",- },- });-console.log(`[Automation ${automation.id}] Created run with ID: ${run.id}`);automationLogger.log(automation.id,@@ -517,7 +544,16 @@ export async function runAutomation(`Job saved successfully (${jobsSaved} total)`,{ jobsSaved },);- } catch (err) {+ } catch (err: any) {+ if (err?.code === "P2002") {+ // Another concurrent run already saved this URL first.+ automationLogger.log(+ automation.id,+ "info",+ "Job skipped - already saved by a concurrent run",+ );+ return;+ }const errorMsg = err instanceof Error ? err.message : "Unknown error";automationLogger.log(automation.id,@@ -696,12 +732,17 @@ function scalePrerank(raw: number): number {return Math.min(99, Math.max(0, Math.round((raw / PRERANK_MAX) * 99)));}+// Returns false (instead of throwing) when a concurrent run already saved+// this exact URL first — the Job_userId_jobUrl_automation_key partial unique+// index (migrations/20260710000002_job_automation_url_unique) is the+// backstop for that race, since app-level dedup only sees a point-in-time+// snapshot of existing URLs.async function persistDiscoveredJob(automation: Automation,job: JobDetails,matchScore: number,matchData: object,-): Promise<void> {+): Promise<boolean> {const scrapedJob: ScrapedJobData = {title: job.title,company: job.company,@@ -722,7 +763,13 @@ async function persistDiscoveredJob(matchData: JSON.stringify(matchData),});- await db.job.create({ data: jobRecord });+ try {+ await db.job.create({ data: jobRecord });+ return true;+ } catch (err: any) {+ if (err?.code === "P2002") return false;+ throw err;+ }}async function runAtsRun(@@ -903,7 +950,7 @@ async function runAtsRun(for (const scored of pipeline.toSaveUnanalyzed) {if (signal?.aborted) break;try {- await persistDiscoveredJob(+ const saved = await persistDiscoveredJob(automation,scored.job,scalePrerank(scored.score),@@ -913,7 +960,7 @@ async function runAtsRun(analyzed: false,},);- jobsSaved++;+ if (saved) jobsSaved++;} catch (err) {console.error(`${label} Failed to save listing:`, err);}@@ -935,7 +982,7 @@ async function runAtsRun(const saveUnanalyzed = async () => {try {- await persistDiscoveredJob(+ const saved = await persistDiscoveredJob(automation,scored.job,scalePrerank(scored.score),@@ -945,7 +992,7 @@ async function runAtsRun(analyzed: false,},);- jobsSaved++;+ if (saved) jobsSaved++;} catch (err) {console.error(`${label} Failed to save listing:`, err);}@@ -1007,18 +1054,23 @@ async function runAtsRun();try {- await persistDiscoveredJob(automation, scored.job, matchResult.score, {- ...matchResult.data,- resumeId: resume.id,- resumeTitle: resume.title,- matchedAt: new Date().toISOString(),- provider: aiSettings.provider,- model: modelName,- prerankScore: scored.score,- prerankComponents: scored.components,- analyzed: true,- });- jobsSaved++;+ const saved = await persistDiscoveredJob(+ automation,+ scored.job,+ matchResult.score,+ {+ ...matchResult.data,+ resumeId: resume.id,+ resumeTitle: resume.title,+ matchedAt: new Date().toISOString(),+ provider: aiSettings.provider,+ model: modelName,+ prerankScore: scored.score,+ prerankComponents: scored.components,+ analyzed: true,+ },+ );+ if (saved) jobsSaved++;} catch (err) {console.error(`${label} Failed to save analyzed job:`, err);}