Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix / Forward native ENV variables to child process #129

Merged
merged 19 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions resources/js/electron-plugin/dist/server/api/childProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { utilityProcess } from 'electron';
import state from '../state';
import { notifyLaravel } from "../utils";
import { join } from 'path';
import { getDefaultEnvironmentVariables, getDefaultPhpIniSettings } from "../php";
const router = express.Router();
const killSync = require('kill-sync');
function startProcess(settings) {
Expand All @@ -21,8 +22,8 @@ function startProcess(settings) {
}
const proc = utilityProcess.fork(join(__dirname, '../../electron-plugin/dist/server/childProcess.js'), cmd, {
cwd,
serviceName: alias,
stdio: 'pipe',
serviceName: alias,
env: Object.assign(Object.assign({}, process.env), env)
});
proc.stdout.on('data', (data) => {
Expand Down Expand Up @@ -68,7 +69,7 @@ function startProcess(settings) {
const settings = Object.assign({}, getSettings(alias));
delete state.processes[alias];
if (settings.persistent) {
console.log('Process [' + alias + '] wathchdog restarting...');
console.log('Process [' + alias + '] watchdog restarting...');
startProcess(settings);
}
});
Expand All @@ -78,6 +79,15 @@ function startProcess(settings) {
settings
};
}
function startPhpProcess(settings) {
const defaultEnv = getDefaultEnvironmentVariables(state.randomSecret, state.electronApiPort);
const iniSettings = Object.assign(Object.assign({}, getDefaultPhpIniSettings()), state.phpIni);
const iniArgs = Object.keys(iniSettings).map(key => {
return ['-d', `${key}=${iniSettings[key]}`];
}).flat();
settings = Object.assign(Object.assign({}, settings), { cmd: [state.php, ...iniArgs, ...settings.cmd], env: Object.assign(Object.assign({}, settings.env), defaultEnv) });
return startProcess(settings);
}
function stopProcess(alias) {
const proc = getProcess(alias);
if (proc === undefined) {
Expand Down Expand Up @@ -105,6 +115,10 @@ router.post('/start', (req, res) => {
const proc = startProcess(req.body);
res.json(proc);
});
router.post('/start-php', (req, res) => {
const proc = startPhpProcess(req.body);
res.json(proc);
});
router.post('/stop', (req, res) => {
const { alias } = req.body;
stopProcess(alias);
Expand Down
28 changes: 11 additions & 17 deletions resources/js/electron-plugin/dist/server/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@ function retrieveNativePHPConfig() {
});
}
function callPhp(args, options, phpIniSettings = {}) {
let defaultIniSettings = {
'memory_limit': '512M',
'curl.cainfo': state.caCert,
'openssl.cafile': state.caCert
};
let iniSettings = Object.assign(defaultIniSettings, phpIniSettings);
let iniSettings = Object.assign(getDefaultPhpIniSettings(), phpIniSettings);
Object.keys(iniSettings).forEach(key => {
args.unshift('-d', `${key}=${iniSettings[key]}`);
});
Expand Down Expand Up @@ -101,15 +96,7 @@ function ensureAppFoldersAreAvailable() {
}
}
function startQueueWorker(secret, apiPort, phpIniSettings = {}) {
const env = {
APP_ENV: process.env.NODE_ENV === 'development' ? 'local' : 'production',
APP_DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',
NATIVEPHP_STORAGE_PATH: storagePath,
NATIVEPHP_DATABASE_PATH: databaseFile,
NATIVEPHP_API_URL: `http://localhost:${apiPort}/api/`,
NATIVEPHP_RUNNING: true,
NATIVEPHP_SECRET: secret
};
const env = getDefaultEnvironmentVariables(secret, apiPort);
const phpOptions = {
cwd: appPath,
env
Expand Down Expand Up @@ -139,7 +126,7 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
NATIVEPHP_STORAGE_PATH: storagePath,
NATIVEPHP_DATABASE_PATH: databaseFile,
NATIVEPHP_API_URL: `http://localhost:${apiPort}/api/`,
NATIVEPHP_RUNNING: true,
NATIVEPHP_RUNNING: 'true',
NATIVEPHP_SECRET: secret,
NATIVEPHP_USER_HOME_PATH: getPath('home'),
NATIVEPHP_APP_DATA_PATH: getPath('appData'),
Expand All @@ -153,6 +140,13 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
NATIVEPHP_RECENT_PATH: getPath('recent'),
};
}
function getDefaultPhpIniSettings() {
return {
'memory_limit': '512M',
'curl.cainfo': state.caCert,
'openssl.cafile': state.caCert
};
}
function serveApp(secret, apiPort, phpIniSettings) {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
const appPath = getAppPath();
Expand Down Expand Up @@ -220,4 +214,4 @@ function serveApp(secret, apiPort, phpIniSettings) {
});
}));
}
export { startQueueWorker, startScheduler, serveApp, getAppPath, retrieveNativePHPConfig, retrievePhpIniSettings };
export { startQueueWorker, startScheduler, serveApp, getAppPath, retrieveNativePHPConfig, retrievePhpIniSettings, getDefaultEnvironmentVariables, getDefaultPhpIniSettings };
36 changes: 34 additions & 2 deletions resources/js/electron-plugin/src/server/api/childProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { utilityProcess } from 'electron';
import state from '../state';
import { notifyLaravel } from "../utils";
import { join } from 'path';
import { getDefaultEnvironmentVariables, getDefaultPhpIniSettings } from "../php";


const router = express.Router();
const killSync = require('kill-sync');
Expand All @@ -19,8 +21,8 @@ function startProcess(settings) {
cmd,
{
cwd,
serviceName: alias,
stdio: 'pipe',
serviceName: alias,
env: {
...process.env,
...env,
Expand Down Expand Up @@ -81,7 +83,7 @@ function startProcess(settings) {
delete state.processes[alias];

if (settings.persistent) {
console.log('Process [' + alias + '] wathchdog restarting...');
console.log('Process [' + alias + '] watchdog restarting...');
startProcess(settings);
}
});
Expand All @@ -93,6 +95,30 @@ function startProcess(settings) {
};
}

function startPhpProcess(settings) {
const defaultEnv = getDefaultEnvironmentVariables(
state.randomSecret,
state.electronApiPort
);

// Construct command args from ini settings
const iniSettings = { ...getDefaultPhpIniSettings(), ...state.phpIni };
const iniArgs = Object.keys(iniSettings).map(key => {
return ['-d', `${key}=${iniSettings[key]}`];
}).flat();


settings = {
...settings,
// Prepend cmd with php executable path & ini settings
cmd: [ state.php, ...iniArgs, ...settings.cmd ],
// Mix in the internal NativePHP env
env: { ...settings.env, ...defaultEnv }
};

return startProcess(settings);
}

function stopProcess(alias) {
const proc = getProcess(alias);

Expand Down Expand Up @@ -129,6 +155,12 @@ router.post('/start', (req, res) => {
res.json(proc);
});

router.post('/start-php', (req, res) => {
const proc = startPhpProcess(req.body);

res.json(proc);
});

router.post('/stop', (req, res) => {
const {alias} = req.body;

Expand Down
29 changes: 12 additions & 17 deletions resources/js/electron-plugin/src/server/php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,8 @@ async function retrieveNativePHPConfig() {
}

function callPhp(args, options, phpIniSettings = {}) {
let defaultIniSettings = {
'memory_limit': '512M',
'curl.cainfo': state.caCert,
'openssl.cafile': state.caCert
}

let iniSettings = Object.assign(defaultIniSettings, phpIniSettings);
let iniSettings = Object.assign(getDefaultPhpIniSettings(), phpIniSettings);

Object.keys(iniSettings).forEach(key => {
args.unshift('-d', `${key}=${iniSettings[key]}`);
Expand Down Expand Up @@ -120,15 +115,7 @@ function ensureAppFoldersAreAvailable() {
}

function startQueueWorker(secret, apiPort, phpIniSettings = {}) {
const env = {
APP_ENV: process.env.NODE_ENV === 'development' ? 'local' : 'production',
APP_DEBUG: process.env.NODE_ENV === 'development' ? 'true' : 'false',
NATIVEPHP_STORAGE_PATH: storagePath,
NATIVEPHP_DATABASE_PATH: databaseFile,
NATIVEPHP_API_URL: `http://localhost:${apiPort}/api/`,
NATIVEPHP_RUNNING: true,
NATIVEPHP_SECRET: secret
};
const env = getDefaultEnvironmentVariables(secret, apiPort);

const phpOptions = {
cwd: appPath,
Expand Down Expand Up @@ -165,7 +152,7 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
NATIVEPHP_STORAGE_PATH: storagePath,
NATIVEPHP_DATABASE_PATH: databaseFile,
NATIVEPHP_API_URL: `http://localhost:${apiPort}/api/`,
NATIVEPHP_RUNNING: true,
NATIVEPHP_RUNNING: 'true',
simonhamp marked this conversation as resolved.
Show resolved Hide resolved
NATIVEPHP_SECRET: secret,
NATIVEPHP_USER_HOME_PATH: getPath('home'),
NATIVEPHP_APP_DATA_PATH: getPath('appData'),
Expand All @@ -180,6 +167,14 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
};
}

function getDefaultPhpIniSettings() {
return {
'memory_limit': '512M',
'curl.cainfo': state.caCert,
'openssl.cafile': state.caCert
}
}

function serveApp(secret, apiPort, phpIniSettings): Promise<ProcessResult> {
return new Promise(async (resolve, reject) => {
const appPath = getAppPath();
Expand Down Expand Up @@ -267,4 +262,4 @@ function serveApp(secret, apiPort, phpIniSettings): Promise<ProcessResult> {
})
}

export {startQueueWorker, startScheduler, serveApp, getAppPath, retrieveNativePHPConfig, retrievePhpIniSettings}
export {startQueueWorker, startScheduler, serveApp, getAppPath, retrieveNativePHPConfig, retrievePhpIniSettings, getDefaultEnvironmentVariables, getDefaultPhpIniSettings}