URLLogger - Define folder to run the CODE

More
2 years 3 months ago #23900 by prashant
Hellos,

 it possible to define the folder in which to execute the script below in URL logger like we do in a package in ETL

 

I am trying to run this script in the folder
C:\Users\Superadmin\Documents\KBIAutomation\brain




const { execFile } = require('child_process');
try {
// Dynamic file name due to use of logid, we can also timestamp if needed
const jsonFilePath = `C:\\Users\\Superadmin\\Documents\\KBIAutomation\\brain\\whip.json`;
const pythonScriptPath = `C:\\Users\\Superadmin\\Documents\\KBIAutomation\\brain\\whip.py`;
const pythomYaml = `C:\\Users\\Superadmin\\Documents\\KBIAutomation\\brain\\whip.yaml`;
const pythonExecutablePath = `C:\\Users\\Superadmin\\AppData\\Local\\Programs\\Python\\Python312\\python.exe`;
const agparameters = [pythonScriptPath, jsonFilePath, pythomYaml];
execFile(pythonExecutablePath, agparameters);
return 200;
} catch (err) {
    logger.error(`${err.message ? err.message : err}`)
    return 500;
}

 

Please Log in or Create an account to join the conversation.

More
2 years 3 months ago #23904 by admin
you can try

execFile(pythonExecutablePath, agparameters,{cwd: 'C:\\Users\\Superadmin\\Documents\\KBIAutomation\\brain\\'});

nodejs.org/api/child_process.html

Mike
ETL Architect

Please Log in or Create an account to join the conversation.

More
2 years 3 months ago #23905 by prashant
Thank you so much for pointing this to me , this script is majorly simplified now , written by chatgpt of course


const fs = require('fs');
const { execFile } = require('child_process');
try {
    // Path to the JSON file
    const jsonFilePath = `C:\\Users\\Superadmin\\Downloads\\director\\${logId}.json`;
    const jsonString = JSON.stringify(body, null, 4);
    // Write JSON data to a file
    fs.writeFileSync(jsonFilePath, jsonString, 'utf8');
    console.log('File written successfully to', jsonFilePath);
    // Python executable and parameters
    const pythonExecutablePath = `python`;  // Use 'python' from PATH
    const parameters = ;
    // Execute the Python script asynchronously with specified working directory
    execFile(pythonExecutablePath, parameters, { cwd: 'C:\\Users\\Superadmin\\Documents\\KBIAutomation\\brain\\' }, (error, stdout, stderr) => {
        if (error) {
            console.error(`Execution error: ${error.message ? error.message : error}`);
            return;
        }
        if (stderr) {
            console.error(`Python script stderr: ${stderr}`);
        }
        console.log('Python script executed successfully');
        console.log('Output:', stdout);
    });
    // Immediate return without waiting for the Python script to finish
    return 200;
} catch (err) {
    console.error('Caught an error:', err.message ? err.message : err);
    return 500; // Any error caught here results in a 500 status
}
 
The following user(s) said Thank You: admin

Please Log in or Create an account to join the conversation.

More
2 years 3 months ago #23913 by admin
I have noticed that you are using console.log. that unfortunately prevents you from seeing the errors and they are not written into the log.

Please use logger instead

    execFile(pythonExecutablePath, parameters, { cwd: 'C:\\Temp\\' }, (error, stdout, stderr) => {
        if (error) {
            logger.error(`Execution error: ${error.message ? error.message : error}`);
            return;
        }
        if (stderr) {
            logger.error(`Python script stderr: ${stderr}`);
        }
        logger.log('Python script executed successfully');
        logger.log('Output:', stdout);
    });

 

Mike
ETL Architect

Please Log in or Create an account to join the conversation.

More
2 years 3 months ago #23917 by prashant

I have noticed that you are using console.log. that unfortunately prevents you from seeing the errors and they are not written into the log.

Please use logger instead
 
THIS IS gold information , thank you , I will also update my chatGPT prompt accordingly to avoid using it. 

Please Log in or Create an account to join the conversation.

Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Read more
Marketing
Set of techniques which have for object the commercial strategy and in particular the market study.
Google
Accept
Decline
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline
Google Analytics
Accept
Decline
Functional
Tools used to give you more features when navigating on the website, this can include social sharing.
Advertisement
If you accept, the ads on the page will be adapted to your preferences.
Google Ad
Accept
Decline
Save