Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 86d7426e8b | |||
| afe56393e7 | |||
| b2cd26af30 |
@@ -231,9 +231,10 @@ jobs:
|
|||||||
run: bun install --frozen-lockfile
|
run: bun install --frozen-lockfile
|
||||||
|
|
||||||
- name: Rebuild native modules
|
- name: Rebuild native modules
|
||||||
run: npm rebuild
|
run: npm rebuild --ignore-scripts
|
||||||
env:
|
env:
|
||||||
HUSKY: "0"
|
HUSKY: "0"
|
||||||
|
HUSKY_SKIP_INSTALL: "1"
|
||||||
|
|
||||||
- name: Build macOS distributables
|
- name: Build macOS distributables
|
||||||
run: bun run make:macos
|
run: bun run make:macos
|
||||||
@@ -272,9 +273,10 @@ jobs:
|
|||||||
run: bun install --frozen-lockfile
|
run: bun install --frozen-lockfile
|
||||||
|
|
||||||
- name: Rebuild native modules
|
- name: Rebuild native modules
|
||||||
run: npm rebuild
|
run: npm rebuild --ignore-scripts
|
||||||
env:
|
env:
|
||||||
HUSKY: "0"
|
HUSKY: "0"
|
||||||
|
HUSKY_SKIP_INSTALL: "1"
|
||||||
|
|
||||||
- name: Build Windows distributables
|
- name: Build Windows distributables
|
||||||
run: bun run make -- --platform win32
|
run: bun run make -- --platform win32
|
||||||
|
|||||||
+1
-1
@@ -91,7 +91,7 @@ COPY --from=build /app/dalpuri/generated/ ./dalpuri/generated/
|
|||||||
COPY --from=deps /app/node_modules/ ./node_modules/
|
COPY --from=deps /app/node_modules/ ./node_modules/
|
||||||
|
|
||||||
# Ensure pdfmake Roboto fonts are present at runtime for PDF generation.
|
# Ensure pdfmake Roboto fonts are present at runtime for PDF generation.
|
||||||
COPY --from=build /app/node_modules/pdfmake/build/fonts/ ./node_modules/pdfmake/build/fonts/
|
COPY --from=build /app/api/node_modules/pdfmake/build/fonts/ ./node_modules/pdfmake/build/fonts/
|
||||||
|
|
||||||
ENV NODE_ENV=production
|
ENV NODE_ENV=production
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,11 @@ spec:
|
|||||||
env:
|
env:
|
||||||
- name: MANAGER_SOCKET_URL
|
- name: MANAGER_SOCKET_URL
|
||||||
value: "http://optima-api.optima.svc.cluster.local:8671"
|
value: "http://optima-api.optima.svc.cluster.local:8671"
|
||||||
|
- name: API_DATABASE_URL
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: api-env-secret
|
||||||
|
key: DATABASE_URL
|
||||||
envFrom:
|
envFrom:
|
||||||
- secretRef:
|
- secretRef:
|
||||||
name: api-env-secret
|
name: api-env-secret
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Socket } from "socket.io-client";
|
import { Socket } from "socket.io-client";
|
||||||
import { executeFullDalpuriSync, executeForcedIncrementalDalpuriSync } from "dalpuri";
|
import { executeFullDalpuriSync, executeForcedIncrementalDalpuriSync } from "dalpuri";
|
||||||
|
import { prisma } from "../../constants";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute a full sync from Dalpuri (ConnectWise) to the API database.
|
* Execute a full sync from Dalpuri (ConnectWise) to the API database.
|
||||||
@@ -14,5 +15,53 @@ export async function executeFullSync(_workerSocket: Socket): Promise<void> {
|
|||||||
* Called every 5 seconds via PgBoss from the API process interval.
|
* Called every 5 seconds via PgBoss from the API process interval.
|
||||||
*/
|
*/
|
||||||
export async function executeIncrementalSync(): Promise<void> {
|
export async function executeIncrementalSync(): Promise<void> {
|
||||||
return executeForcedIncrementalDalpuriSync();
|
let jobRunId: string | undefined;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const run = await prisma.syncJobRun.create({
|
||||||
|
data: {
|
||||||
|
jobType: "INCREMENTAL_SYNC",
|
||||||
|
status: "RUNNING",
|
||||||
|
triggeredBy: "worker",
|
||||||
|
startedAt: new Date(),
|
||||||
|
},
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
jobRunId = run.id;
|
||||||
|
} catch (err) {
|
||||||
|
// Sync should still run even if tracking insert fails.
|
||||||
|
console.error("[sync] Failed to create incremental SyncJobRun", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await executeForcedIncrementalDalpuriSync({ jobRunId });
|
||||||
|
|
||||||
|
if (jobRunId) {
|
||||||
|
await prisma.syncJobRun.update({
|
||||||
|
where: { id: jobRunId },
|
||||||
|
data: {
|
||||||
|
status: "COMPLETED",
|
||||||
|
completedAt: new Date(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
if (jobRunId) {
|
||||||
|
const errorSummary = err instanceof Error ? err.message : String(err);
|
||||||
|
await prisma.syncJobRun
|
||||||
|
.update({
|
||||||
|
where: { id: jobRunId },
|
||||||
|
data: {
|
||||||
|
status: "FAILED",
|
||||||
|
completedAt: new Date(),
|
||||||
|
errorSummary: errorSummary.slice(0, 2000),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
// Best-effort update only.
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+73
-6
@@ -75,6 +75,43 @@ type DeleteResult = {
|
|||||||
|
|
||||||
let incrementalDeleteStepIndex = 0;
|
let incrementalDeleteStepIndex = 0;
|
||||||
|
|
||||||
|
const CRITICAL_INCREMENTAL_RECONCILE_TABLES = new Set([
|
||||||
|
"Companies",
|
||||||
|
"Company Addresses",
|
||||||
|
"Contacts",
|
||||||
|
]);
|
||||||
|
|
||||||
|
const criticalFullSyncIntervalMinutes = Math.max(
|
||||||
|
1,
|
||||||
|
Number.parseInt(
|
||||||
|
process.env.DALPURI_CRITICAL_FULL_SYNC_INTERVAL_MINUTES ?? "60",
|
||||||
|
10
|
||||||
|
) || 60
|
||||||
|
);
|
||||||
|
|
||||||
|
const CRITICAL_FULL_SYNC_INTERVAL_MS =
|
||||||
|
criticalFullSyncIntervalMinutes * 60 * 1000;
|
||||||
|
|
||||||
|
const lastCriticalFullSyncByStep = new Map<string, number>();
|
||||||
|
|
||||||
|
const shouldForceCriticalFullSync = (
|
||||||
|
step: Step,
|
||||||
|
forceIncremental: boolean
|
||||||
|
): boolean => {
|
||||||
|
if (!forceIncremental) return false;
|
||||||
|
if (!CRITICAL_INCREMENTAL_RECONCILE_TABLES.has(step.name)) return false;
|
||||||
|
|
||||||
|
const now = Date.now();
|
||||||
|
const last = lastCriticalFullSyncByStep.get(step.name) ?? 0;
|
||||||
|
|
||||||
|
if (now - last < CRITICAL_FULL_SYNC_INTERVAL_MS) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastCriticalFullSyncByStep.set(step.name, now);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
const parseEnvFile = (path: string): Record<string, string> => {
|
const parseEnvFile = (path: string): Record<string, string> => {
|
||||||
const envData = readFileSync(path, "utf8");
|
const envData = readFileSync(path, "utf8");
|
||||||
const out: Record<string, string> = {};
|
const out: Record<string, string> = {};
|
||||||
@@ -107,6 +144,20 @@ const resolveApiDatabaseUrl = (): string => {
|
|||||||
if (process.env.OPTIMA_API_DATABASE_URL)
|
if (process.env.OPTIMA_API_DATABASE_URL)
|
||||||
return process.env.OPTIMA_API_DATABASE_URL;
|
return process.env.OPTIMA_API_DATABASE_URL;
|
||||||
|
|
||||||
|
// Worker/runtime fallback:
|
||||||
|
// In Kubernetes we often provide CW via CW_DATABASE_URL and API Postgres via
|
||||||
|
// DATABASE_URL. Only use DATABASE_URL as API when we can safely infer that.
|
||||||
|
if (process.env.CW_DATABASE_URL && process.env.DATABASE_URL) {
|
||||||
|
return process.env.DATABASE_URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
process.env.DATABASE_URL &&
|
||||||
|
/^(postgres|postgresql):\/\//i.test(process.env.DATABASE_URL)
|
||||||
|
) {
|
||||||
|
return process.env.DATABASE_URL;
|
||||||
|
}
|
||||||
|
|
||||||
const candidates = [
|
const candidates = [
|
||||||
resolve(import.meta.dir, "../../api/.env"),
|
resolve(import.meta.dir, "../../api/.env"),
|
||||||
resolve(process.cwd(), "../api/.env"),
|
resolve(process.cwd(), "../api/.env"),
|
||||||
@@ -1757,19 +1808,35 @@ export const executeFullDalpuriSync = async (options?: {
|
|||||||
step,
|
step,
|
||||||
forceIncremental
|
forceIncremental
|
||||||
);
|
);
|
||||||
|
const forceCriticalFullSync = shouldForceCriticalFullSync(
|
||||||
|
step,
|
||||||
|
forceIncremental
|
||||||
|
);
|
||||||
|
const effectiveDecision = forceCriticalFullSync
|
||||||
|
? ({ mode: "full", differences: decision.differences } as SmartSyncDecision)
|
||||||
|
: decision;
|
||||||
|
|
||||||
|
if (forceCriticalFullSync) {
|
||||||
|
console.log(
|
||||||
|
` [smart-sync][forced-full] ${step.name}: forcing periodic full reconciliation every ${criticalFullSyncIntervalMinutes}m`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const sourceIdsFilter =
|
const sourceIdsFilter =
|
||||||
decision.mode === "incremental" ? decision.sourceIds : undefined;
|
effectiveDecision.mode === "incremental"
|
||||||
|
? effectiveDecision.sourceIds
|
||||||
|
: undefined;
|
||||||
console.log(
|
console.log(
|
||||||
` [smart-sync]${forceIncremental ? "[forced]" : ""} mode=${
|
` [smart-sync]${forceIncremental ? "[forced]" : ""} mode=${
|
||||||
decision.mode
|
effectiveDecision.mode
|
||||||
}${
|
}${
|
||||||
decision.mode === "incremental"
|
effectiveDecision.mode === "incremental"
|
||||||
? ` (${decision.sourceIds.length} ids)`
|
? ` (${effectiveDecision.sourceIds.length} ids)`
|
||||||
: ""
|
: ""
|
||||||
}`
|
}`
|
||||||
);
|
);
|
||||||
if (logAllDifferences) {
|
if (logAllDifferences) {
|
||||||
logAllSmartSyncDifferences(step, decision.differences);
|
logAllSmartSyncDifferences(step, effectiveDecision.differences);
|
||||||
}
|
}
|
||||||
const result = await syncStep(
|
const result = await syncStep(
|
||||||
cwPrisma,
|
cwPrisma,
|
||||||
@@ -1791,7 +1858,7 @@ export const executeFullDalpuriSync = async (options?: {
|
|||||||
|
|
||||||
await writeStepLog(
|
await writeStepLog(
|
||||||
step.name,
|
step.name,
|
||||||
decision.mode,
|
effectiveDecision.mode,
|
||||||
result,
|
result,
|
||||||
{ deleted: 0, failed: 0 },
|
{ deleted: 0, failed: 0 },
|
||||||
Date.now() - stepStart
|
Date.now() - stepStart
|
||||||
|
|||||||
Reference in New Issue
Block a user