Fix incorrect error during import, better logging during import
This commit is contained in:
@@ -46,13 +46,14 @@ function logError(error, context = '') {
|
||||
}
|
||||
|
||||
// Helper function to log import progress
|
||||
function logImport(message) {
|
||||
function logImport(message, isSignificant = false) {
|
||||
// Only write to disk if it's a significant event
|
||||
if (isSignificant) {
|
||||
const timestamp = new Date().toISOString();
|
||||
const logMessage = `[${timestamp}] ${message}\n`;
|
||||
|
||||
// Log to import file
|
||||
fs.appendFileSync(IMPORT_LOG, logMessage);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to format duration
|
||||
function formatDuration(seconds) {
|
||||
@@ -70,7 +71,26 @@ function formatDuration(seconds) {
|
||||
|
||||
// Helper function to output progress
|
||||
function outputProgress(data) {
|
||||
// Always send to stdout for frontend
|
||||
process.stdout.write(JSON.stringify(data) + '\n');
|
||||
|
||||
// Log significant events to disk
|
||||
const isSignificant =
|
||||
// Operation starts
|
||||
(data.operation && !data.current) ||
|
||||
// Operation completions and errors
|
||||
data.status === 'complete' ||
|
||||
data.status === 'error' ||
|
||||
// Test limits reached
|
||||
data.message?.includes('test limit') ||
|
||||
// Schema changes
|
||||
data.operation?.includes('Creating database schema') ||
|
||||
// Parallel import starts
|
||||
data.message?.includes('Processing orders and purchase orders simultaneously');
|
||||
|
||||
if (isSignificant) {
|
||||
logImport(`${data.operation || 'Operation'}${data.message ? ': ' + data.message : ''}${data.error ? ' Error: ' + data.error : ''}${data.status ? ' Status: ' + data.status : ''}`, true);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to count total rows in a CSV file
|
||||
@@ -930,15 +950,16 @@ async function importPurchaseOrders(pool, filePath) {
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const startTime = Date.now();
|
||||
let pool;
|
||||
let importInProgress = false;
|
||||
|
||||
try {
|
||||
outputProgress({
|
||||
operation: 'Starting import process',
|
||||
message: 'Creating connection pool...'
|
||||
});
|
||||
|
||||
const startTime = Date.now();
|
||||
let pool;
|
||||
|
||||
try {
|
||||
pool = mysql.createPool(dbConfig);
|
||||
|
||||
// Check if tables exist, if not create them
|
||||
@@ -970,6 +991,8 @@ async function main() {
|
||||
|
||||
// Import all data
|
||||
try {
|
||||
importInProgress = true;
|
||||
|
||||
// Import products first since they're referenced by other tables
|
||||
await importProducts(pool, path.join(__dirname, '../csv/39f2x83-products.csv'));
|
||||
|
||||
@@ -984,20 +1007,31 @@ async function main() {
|
||||
importPurchaseOrders(pool, path.join(__dirname, '../csv/39f2x83-purchase_orders.csv'))
|
||||
]);
|
||||
|
||||
// Only output completion if we haven't encountered an error
|
||||
if (importInProgress) {
|
||||
outputProgress({
|
||||
status: 'complete',
|
||||
operation: 'Import process completed',
|
||||
duration: formatDuration((Date.now() - startTime) / 1000)
|
||||
});
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
importInProgress = false;
|
||||
logError(error, 'Error during import');
|
||||
outputProgress({
|
||||
status: 'error',
|
||||
operation: 'Import process',
|
||||
error: error.message
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
} catch (error) {
|
||||
importInProgress = false;
|
||||
logError(error, 'Fatal error during import process');
|
||||
outputProgress({
|
||||
status: 'error',
|
||||
operation: 'Import process',
|
||||
error: error.message
|
||||
});
|
||||
process.exit(1);
|
||||
|
||||
@@ -528,31 +528,18 @@ export function Settings() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const data = await response.json().catch(() => ({}));
|
||||
// Only handle error if we don't have any progress yet
|
||||
// Only check response status if we don't have any progress
|
||||
if (!importProgress?.current && !purchaseOrdersProgress?.current) {
|
||||
if (!response.ok && response.status !== 200) {
|
||||
const data = await response.json().catch(() => ({}));
|
||||
throw new Error(data.error || 'Failed to start CSV import');
|
||||
}
|
||||
} else {
|
||||
console.log('Response not ok but import is in progress, continuing...');
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('Import error:', error);
|
||||
|
||||
// If we have any progress, assume the import is still running
|
||||
if (importProgress?.current || purchaseOrdersProgress?.current) {
|
||||
console.log('Error occurred but import appears to be running');
|
||||
return;
|
||||
}
|
||||
|
||||
// Only clean up if we don't have any progress
|
||||
if (eventSource) {
|
||||
eventSource.close();
|
||||
setEventSource(null);
|
||||
}
|
||||
handleError('CSV import', error instanceof Error ? error.message : 'Unknown error');
|
||||
setIsImporting(false);
|
||||
setImportProgress(null);
|
||||
setPurchaseOrdersProgress(null);
|
||||
handleError('Data import', error instanceof Error ? error.message : 'Unknown error');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user