Eliminated python dependencies and managed server / instances configuration

This commit is contained in:
thecookingsenpai 2024-02-02 22:10:31 +01:00
parent e3bb0742a4
commit 6e2128e39c
6 changed files with 46 additions and 107 deletions

4
.env Normal file
View File

@ -0,0 +1,4 @@
SERVER_URL="https://play.discus.sh"
START_SERVER="false"
GEOM_X="1280"
GEOM_Y="720"

BIN
assets/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

45
main.js
View File

@ -1,15 +1,47 @@
// Modules to control application life and create native browser window // Modules to control application life and create native browser window
const { app, BrowserWindow, screen } = require('electron') const { app, BrowserWindow, screen } = require('electron')
const path = require('node:path') 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 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 () { function createWindow () {
// Create the browser window. // Create the browser window.
const mainWindow = new BrowserWindow({ const mainWindow = new BrowserWindow({
width: 1280 / factor, icon: "assets/favicon.ico",
height: 1024 / factor, title: "SwingMusic",
center: true,
width: process.env.GEOM_X / factor,
height: process.env.GEOM_Y / factor,
webPreferences: { webPreferences: {
zoomFactor: 1.0 / factor, zoomFactor: 1.0 / factor,
nodeIntegration: true, nodeIntegration: true,
@ -18,7 +50,7 @@ function createWindow () {
// Testing some env variables // Testing some env variables
mainWindow.setMenuBarVisibility(false) 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 app.on("before-quit", function() {
// for applications and their menu bar to stay active until the user quits server.kill() // Killing the server if started
// explicitly with Cmd + Q. })
app.on('window-all-closed', function () { app.on('window-all-closed', function () {
app.quit() app.quit()
}) })

View File

@ -18,5 +18,8 @@
"license": "CC0-1.0", "license": "CC0-1.0",
"devDependencies": { "devDependencies": {
"electron": "^28.2.0" "electron": "^28.2.0"
},
"dependencies": {
"dotenv": "^16.4.1"
} }
} }

51
run
View File

@ -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))

View File

@ -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))