import "dotenv/config"; import { sdkLogger as logger } from "../../../services/reporting-service/logger.js"; import { createConnectorFromPath } from "../../../utils/connector-factory.js"; import { getEnvString } from "../../../utils/env.js"; import { summarizeError } from "../../../utils/error-utils.js"; import { signalBreeJobComplete } from "../utils/bree-worker-complete.js"; // Configure the logger with the correct log level logger.level = getEnvString("LOG_LEVEL", "info"); const main = async () => { try { logger.info("==========Starting from-erp job cycle=========="); // Get the connector path from the environment variable const connectorPath = process.env.CONNECTOR_PATH; if (!connectorPath) { throw new Error("Connector path not provided in environment variables"); } // Create a new connector instance for this job const connector = await createConnectorFromPath(connectorPath); // No lock needed: from-erp syncs ERP→MM which doesn't conflict with // to-erp/NATS that sync MM→ERP. Removing the lock prevents blocking // real-time NATS labor ticket requests during long sync operations. await connector.syncFromERP(); await connector.syncFromERPCompleted(); logger.info("==========Completed from-erp job cycle=========="); } catch (error) { // Logger now handles circular references and error objects safely logger.error('Worker for job "from-erp" had an error', { error: summarizeError(error), }); throw error; // Rethrow so Bree can handle it properly } }; // Cross-platform module detection fix for Bree compatibility // Windows: process.argv[1] uses backslashes, import.meta.url uses forward slashes // Linux/Mac: both use forward slashes, so this normalization is safe const normalizedArgv1 = process.argv[1].replace(/\\/g, '/'); const fileUrl = normalizedArgv1.startsWith('/') ? `file://${normalizedArgv1}` : // Unix: file:// + /path = file:///path `file:///${normalizedArgv1}`; // Windows: file:/// + C:/path = file:///C:/path const isMainModule = import.meta.url === fileUrl; if (isMainModule) { // This is called when Bree runs this file as a worker try { await main(); signalBreeJobComplete(); } catch { process.exit(1); } } export default main;