From 6e2128e39cf053207cefad5ea6645e44f147a356 Mon Sep 17 00:00:00 2001 From: thecookingsenpai Date: Fri, 2 Feb 2024 22:10:31 +0100 Subject: [PATCH] Eliminated python dependencies and managed server / instances configuration --- .env | 4 ++++ assets/favicon.ico | Bin 0 -> 1281 bytes main.js | 45 +++++++++++++++++++++++++++++++++------ package.json | 3 +++ run | 51 --------------------------------------------- run_source | 50 -------------------------------------------- 6 files changed, 46 insertions(+), 107 deletions(-) create mode 100644 .env create mode 100644 assets/favicon.ico delete mode 100755 run delete mode 100755 run_source diff --git a/.env b/.env new file mode 100644 index 0000000..39e5bcc --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +SERVER_URL="https://play.discus.sh" +START_SERVER="false" +GEOM_X="1280" +GEOM_Y="720" \ No newline at end of file diff --git a/assets/favicon.ico b/assets/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..206eea05b59aafd8c86ddc66b0805cdce71cb8a4 GIT binary patch literal 1281 zcmV+c1^)T~0096206;(h009690P6$*02TlM0EtjeM-2)Z3IG5A4M|8uQUCw|KmY&$ zKnMl^0063Kaozv`010qNS#tmY1Lyz%1Ly&@ds1rv00f;$L_t(|oaLK6PAf4G$L9i^ zAvFyb!8dpn>-l-U zyla#D{fB|()#>hXO86U?QMB|ef=cw@9#(LMh;C(AWgLI33xfBA08f->+9=f#RbXDC%uJQjd@d3ni#Y&p<>sZJnkzt1WE{QTUcJj?Cv zZ5BW)1SuYnWcnR9`lL7u6zKr~DOo{F-_W+?3M7nvQ*B@BBm!Hn@{s%cdn+q=R0Ejj z`KDF*90UX*1ildfhy}O~WE4Wq3TUm-=T5f1Z5(Mo5{fYp*_Zy4R=aN=a z^=Spc)B-^21~gLhOQYXg{FDT>1g>iVA)X^pZ}gXxhahlS0)Tk$O#sB%fO`Eug%ATk zM=i}ib%PRwo;W};fR)dI9GPyiDsO52uJ^q_2|x`(3L*uNQ@mCA%HY-C3pmBRK(c}s2qgeH<@uVi@~r^W zZ-!jO3UW6sLC6J0mJ~p3)JK$WdN=5lZ--kUNHa#HzxX#?t5x|@f4kleyGlkVbsz^p zyD~)c46s%C?)3pqxk5w(!RozWkMct+M4V!cPz`{^`hc3xmjDc}5pn}_gct&X8m7Ok zg19g;`X;NyTxUFx(qftoQfbNgLg2GoC+I2=daJ{@Ca^4gR#_?P3J^v{pB2{>Kw1E+ zt`&AsOyIz1HTqfrrP}{wtHoWE9S(bWh5@ve!0G)?zF)v4J4Vt@X#u1VTI0as^LciR zxWpPzYxe+l!h8 literal 0 HcmV?d00001 diff --git a/main.js b/main.js index 559f499..8306bf2 100644 --- a/main.js +++ b/main.js @@ -1,15 +1,47 @@ // Modules to control application life and create native browser window const { app, BrowserWindow, screen } = require('electron') const path = require('node:path') +require('dotenv').config() +const { spawn } = require('child_process'); + +console.log(process.env) +const server_url = process.env.SERVER_URL let factor = null +var server = null +function launchServer() { + server = spawn('swingmusic_bin/swingmusic'); + server.stdout.on('data', (data) => { + console.log(`[SERVER]: ${data}`); + }); + server.stderr.on('data', (data) => { + console.error(`[SERVER ERROR]: ${data}`); + }) + server.on('error', (error) => { + console.error(`[SERVER error]: ${error.message}`); + }); + server.on('close', (code) => { + console.log(`[SERVER EXITED] child process exited with code ${code}`); + }); + +} + +if (process.env.START_SERVER == "true") { + console.log("Launching server") + launchServer() +} else { + console.log("Server not launched") +} function createWindow () { // Create the browser window. const mainWindow = new BrowserWindow({ - width: 1280 / factor, - height: 1024 / factor, + icon: "assets/favicon.ico", + title: "SwingMusic", + center: true, + width: process.env.GEOM_X / factor, + height: process.env.GEOM_Y / factor, webPreferences: { zoomFactor: 1.0 / factor, nodeIntegration: true, @@ -18,7 +50,7 @@ function createWindow () { // Testing some env variables mainWindow.setMenuBarVisibility(false) - mainWindow.loadURL("http://localhost:1970", {userAgent: 'swingmusic-desktop'}) + mainWindow.loadURL(server_url, {userAgent: 'swingmusic-desktop'}) } @@ -36,9 +68,10 @@ app.whenReady().then(() => { }) }) -// Quit when all windows are closed, except on macOS. There, it's common -// for applications and their menu bar to stay active until the user quits -// explicitly with Cmd + Q. +app.on("before-quit", function() { + server.kill() // Killing the server if started +}) + app.on('window-all-closed', function () { app.quit() }) diff --git a/package.json b/package.json index d166571..6f9afbd 100644 --- a/package.json +++ b/package.json @@ -18,5 +18,8 @@ "license": "CC0-1.0", "devDependencies": { "electron": "^28.2.0" + }, + "dependencies": { + "dotenv": "^16.4.1" } } diff --git a/run b/run deleted file mode 100755 index 63cc436..0000000 --- a/run +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/python -import os -import sys -import signal -import subprocess -import time -import psutil - -BINARY_PATH = os.path.join(os.path.dirname(__file__), "swingmusic_bin/swingmusic") -print(BINARY_PATH) - -# We need to find the swingmusic binary -if not (os.path.exists(BINARY_PATH)): - print("[FATAL] Could not find swingmusic binary in path: " + BINARY_PATH) - sys.exit(1) - -print("[OK] Swingmusic binary found in path: " + BINARY_PATH) - -# Running the binary and getting the pid -pid = subprocess.Popen([BINARY_PATH], stderr=subprocess.PIPE).pid -print(psutil.Process(pid)) -print("[OK] Swingmusic started with pid: " + str(pid)) - -# Running yarn run . -os.system("yarn start .") - -# Kill the binary process -os.kill(pid, signal.SIGINT) - -# Ensure that the process is killed -max_kill_time = 15 # Seconds -kill_time = 0 -while (kill_time < max_kill_time): - if not psutil.pid_exists(pid): - break - # If the process is sleeping, is ok (it means that it is waiting for the next tick and then will die gracefully) - process_status = psutil.Process(pid) - if (process_status.status() == psutil.STATUS_SLEEPING): - print("[WARNING] The subprocess is sleeping, but it is ok, it will die gracefully (maybe with some output)") - break - elif (process_status.status() == psutil.STATUS_ZOMBIE): - print("[WARNING] The subprocess is a zombie, but it is ok, it will die gracefully ") - break - print("[WAITING] For process to stop... " + str(kill_time)) - kill_time += 1 - time.sleep(1) - -if (kill_time >= max_kill_time): - print("[ERROR] The subprocess looks stuck, please kill it manually with: kill -9 " + str(pid)) -else: - print("[OK] Swingmusic stopped (theoretically) with pid: " + str(pid)) \ No newline at end of file diff --git a/run_source b/run_source deleted file mode 100755 index 76ae155..0000000 --- a/run_source +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/python -import os -import sys -import signal - -SOURCE_PATH = os.path.join(os.path.dirname(__file__), "../swingmusic/") -os.chdir(SOURCE_PATH) - -# We need to find the swingmusic binary -if not (os.path.exists(SOURCE_PATH)): - print("[FATAL] Could not find swingmusic source in path: " + SOURCE_PATH) - sys.exit(1) - -print("[OK] Swingmusic source found in path: " + SOURCE_PATH) - -pid = os.fork() - -# pid greater than 0 represents -# the parent process -if pid > 0 : - print("I am parent process:") - print("Process ID:", os.getpid()) - print("Child's process ID:", pid) - os.chdir("../swingmusic-desktop") - os.system("yarn start .") - # Terminating the child when we are done (not gracefully: it has to stop) - os.kill(pid, signal.SIGINT) - print("Bye!") - -# pid equal to 0 represents -# the created child process -else : - print("\nI am child process:") - print("Process ID:", os.getpid()) - print("Parent's process ID:", os.getppid()) - os.system("./run") - -exit(0) - - -# Running the binary and getting the pid -pid = subprocess.Popen([BINARY_PATH]).pid -print("[OK] Swingmusic started with pid: " + str(pid)) - -# Running yarn run . -os.system("yarn start .") - -# Kill the binary process -os.kill(pid, signal.SIGTERM) -print("[OK] Swingmusic stopped (theoretically) with pid: " + str(pid)) \ No newline at end of file