How to run external programs at startup?


#1

I want to start my python flask application and couchbase before electron loads the url.

app.on('ready', function() {
    mainWindow = new BrowserWindow({width:960, height:800});
    mainWindow.loadUrl('http://127.0.0.1:5000/');
    mainWindow.on('closed', function() {
        mainWindow = null;
    });
});

I get it I’ll have to do it somewhere here, but I can’t find how in documentation.


#2

Are you getting an error? What have you tried so far?


#3
app.on('ready', function() {
    var child_process = require('child_process');
    child = child_process.spawn('./run.py');
    mainWindow = new BrowserWindow({width:960, height:800});
    mainWindow.loadUrl('http://127.0.0.1:5000/');
    mainWindow.on('closed', function() {
        mainWindow = null;
    });
});

This is the code I’m trying, from io.js documentation.
It starts the python process, but I get blank browser window until I reload it manually.


#4

spawn is returning immediately. Try using spawnSync instead.


#5

The page load is attempted before the python server had time to start. I believe if you use spawnSync the application will probably lockup, as the flask background loop will never exit.

This is what I’ve done to reload the page if not loaded correctly:

    mainWindow.webContents.on('did-fail-load',
        function (event, errorCode, errorDescription) {
            console.log('Page failed to load (' + errorCode + '). The server is probably not yet running. Trying again in 100ms.');
            setTimeout(function() {
                mainWindow.webContents.reload();
            }, 100);
        }
    );