Skip to content

Commit

Permalink
shutdown server when electron window closed
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasgabriel18 committed Nov 6, 2023
1 parent b688dba commit a488458
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
20 changes: 19 additions & 1 deletion api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
from DataProcesser import DataProcesser

import os
import atexit
import threading
import pandas as pd
import nltk
import json
nltk.download('wordnet')

app = Flask(__name__)
server_thread = None
CORS(app) # Permite todas as origens por padrão (não recomendado para produção)

app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True

data_processer = DataProcesser()

def shutdown_server():
print("Server shutting down...")
os.kill(os.getpid(), 9)

@app.route('/', methods=['GET'])
def hello_world():
return jsonify("Hello, world!")
Expand All @@ -31,5 +38,16 @@ def upload_file():

return jsonify({'result': result.to_json()})

@app.get('/shutdown')
def shutdown():
shutdown_server()
return 'Server shutting down...'

def run_flask_app():
global server_thread
app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False)

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
server_thread = threading.Thread(target=run_flask_app)
server_thread.start()
atexit.register(shutdown_server)
19 changes: 12 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const { app, BrowserWindow } = require("electron");
const childProcess = require("child_process");
const path = require("path");
const axios = require("axios");
const url = require("url");

var processes = [];

/*
const child = childProcess.spawn("python", ["./api/app.py"], {
detached: false,
stdio: "ignore",
});
processes.push(child);

*/
function createWindow() {
// cria a janela
const win = new BrowserWindow({
Expand Down Expand Up @@ -56,12 +58,15 @@ app.on("before-quit", () => {});

app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
/*
// TENTA fechar o processo do python
processes.forEach(function (proc) {
proc.kill();
}); */
//console.log("Fechando")
axios.get("http://127.0.0.1:5000/shutdown").then(response => {

}).catch(error => {

})
setTimeout(() => {
app.quit();
}, 2000);

app.quit();
}
});

0 comments on commit a488458

Please sign in to comment.