Add route and frontend button to run import from prod script

This commit is contained in:
2025-01-25 10:01:39 -05:00
parent 353ae4540e
commit e1174b8e63
3 changed files with 412 additions and 354 deletions

View File

@@ -376,19 +376,20 @@ router.post('/cancel', (req, res) => {
}
try {
// Kill the process with SIGTERM signal
activeImport.kill('SIGTERM');
// Clean up
activeImport = null;
importProgress = null;
// If it's the prod import module, call its cancel function
if (typeof activeImport.cancelImport === 'function') {
activeImport.cancelImport();
} else {
// Otherwise it's a child process
activeImport.kill('SIGTERM');
}
// Get the operation type from the request
const { operation } = req.query;
// Send cancel message only to the appropriate client set
const cancelMessage = {
status: 'complete',
status: 'cancelled',
operation: 'Operation cancelled'
};
@@ -669,4 +670,45 @@ router.post('/calculate-metrics', async (req, res) => {
}
});
// Route to import from production database
router.post('/import-from-prod', async (req, res) => {
if (activeImport) {
return res.status(409).json({ error: 'Import already in progress' });
}
try {
const importFromProd = require('../../scripts/import-from-prod');
// Set up progress handler
const progressHandler = (data) => {
importProgress = data;
sendProgressToClients(importClients, data);
};
// Start the import process
importFromProd.outputProgress = progressHandler;
activeImport = importFromProd; // Store the module for cancellation
// Run the import in the background
importFromProd.main().catch(error => {
console.error('Error in import process:', error);
activeImport = null;
importProgress = {
status: error.message === 'Import cancelled' ? 'cancelled' : 'error',
operation: 'Import process',
error: error.message
};
sendProgressToClients(importClients, importProgress);
}).finally(() => {
activeImport = null;
});
res.json({ message: 'Import from production started' });
} catch (error) {
console.error('Error starting production import:', error);
activeImport = null;
res.status(500).json({ error: error.message || 'Failed to start production import' });
}
});
module.exports = router;