2016-09-06 14 views
2

私はNode.JS、Electronを使用してアプリケーションを開発中です。このアプリケーションはMongoDBの独自のインスタンスを実行します。モンゴの開始までは、次のコードを使用して作業している:ユーザーがプログラムを終了するとプログラムでMongoDBを停止する

child = childProcess.exec(`mongod --dbpath ${appConfig.dbConfigPath}`); 

は、しかし、私はモンゴを停止したいです。私はすべてがMongoDB Documentation

child = childProcess.exec('mongod --shutdown'); 

child = childProcess.exec(`kill -2 ${child.pid}`); 

まだこれらのいずれもがプロセスをシャットダウンしているから取られ、次のことを試してみました。

このアプリケーションはWindowsプラットフォーム上で動作するように開発されています。

わかりやすくするために、ここに私のアプリの設定ファイルがあります。 init()関数はmain.jsから実行されます。 shutdown()はwindowMain.on( 'close')で実行されます。

calibration.js

'use strict'; 

const childProcess = require('child_process'); 

const fileUtils = require('./lib/utils/fileUtils'); 
const appConfig = require('./config/appConfig'); 

let child; 

class Calibration { 
    constructor() {} 

    init() { 
     createAppConfigDir(); 
     createAppDataDir(); 
     startMongo(); 
    } 

    shutdown() { 
     shutdownMongo(); 
    } 
} 

function createAppConfigDir() { 
    fileUtils.createDirSync(appConfig.appConfigDir); 
} 

function createAppDataDir() { 
    fileUtils.createDirSync(appConfig.dbConfigPath); 
} 

function startMongo() { 
    child = childProcess.exec(`mongod --dbpath ${appConfig.dbConfigPath}`); 
    console.log(child.pid); 
} 

function shutdownMongo() { 
    console.log('inside shutdownMongo'); 
    //This is where I want to shutdown Mongo 
} 

module.exports = new Calibration(); 

main.js

'use strict' 

const { app, BrowserWindow, crashReporter, ipcMain: ipc } = require('electron'); 
const path = require('path'); 

const appCalibration = require('../calibration'); 

appCalibration.init(); 

const appConfig = require('../config/appConfig'); 

let mainWindow = null; 

ipc.on('set-title', (event, title) => { 
    mainWindow.setTitle(title || appconfig.name); 
}) 

ipc.on('quit',() => { 
    app.quit(); 
}) 

// Quit when all windows are closed. 
app.on('window-all-closed', function() { 
    if (process.platform != 'darwin') { 
     app.quit(); 
    } 
}); 

// This method will be called when Electron has finished 
// initialization and is ready to create browser windows. 
app.on('ready', function() { 

    // Create the browser window. 
    mainWindow = new BrowserWindow({ center: true }); 

    mainWindow.maximize(); 

    mainWindow.setMinimumSize(770, 400); 

    mainWindow.loadURL(path.join(`file://${__dirname}`, '../ui/index.html')); 

    mainWindow.on('close',() => { 
     console.log('Inside quit') 
     appCalibration.shutdown(); 
     app.quit(); 
    }); 

    mainWindow.on('closed', function() { 
     mainWindow = null; 
    }); 
}); 

どのような援助が大幅に高く評価されています。

+0

注文を送信し、あなたのMongoDB –

+0

@PauloGaldoSandovalをシャットダウンするIPCを使用することができ、コメントありがとうございました。私はちょうど電子を理解し始めているので、私の無知を許してください。あなたは例を投稿するほど親切ですか?ありがとうございました –

答えて

2

Ipcを使用すると、jsファイルで注文を送信できます。あなたは電子を定義し、あなたのmain.js

、あなたがこれを置くことができます。

ipcMain.on("shutDownDatabase", function (event, content) { 
    // shutdown operations. 
}); 

を次に、あなたのアプリケーションコードの一部では、あなたはこのように機能を置くことができます。また

function sendShutdownOrder (content){ 
    var ipcRenderer = require("electron").ipcRenderer; 
    // the content can be a parameter or whatever you want that should be required for the operation. 
    ipcRenderer.send("shutDownDatabase", content); 
} 

私は電子のイベントを使用してデータベースをシャットダウンすることができると思います。これは、電子メールを開始するときに作成されたメインウィンドウのイベントをリッスンします。

mainWindow.on('closed', function() { 
     // here you command to shutdowm your data base. 
     mainWindow = null; 
    }); 

IPCの詳細については、hereと、ウィンドウのイベントに関する情報hereを参照してください。

1

Paulo Galdo Sandovalの提案で、私はこれを動作させることができました。しかし、WindowsタスクマネージャからmongodのPIDを取得する必要がありました。私は、アプリケーション構成のjsファイルに

function getTaskList() { 
    let pgm = 'mongod'; 

    exec('tasklist', function(err, stdout, stderr) { 
     var lines = stdout.toString().split('\n'); 
     var results = new Array(); 
     lines.forEach(function(line) { 
      var parts = line.split('='); 
      parts.forEach(function(items) { 
       if (items.toString().indexOf(pgm) > -1) { 
        taskList.push(items.toString().replace(/\s+/g, '|').split('|')[1]) 
       } 
      }); 
     }); 
    }); 
} 

を、以下の機能を追加したことを行うために、私はまたにあるPIDを配置する配列変数を宣言した。その後、私はこの私と私のシャットダウン機能

function shutdownMongo() { 
    var pgm = 'mongod'; 

    console.log('inside shutdownMongo'); 

    taskList.forEach(function(item) { 
     console.log('Killing process ' + item); 
     process.kill(item); 
    }); 
} 

を更新します私のアプリケーションが起動して終了すると、Mongoを起動して停止することができます。

おかげで、すべての

関連する問題