Skip to content

Commit

Permalink
Better guard runner/app code (#70)
Browse files Browse the repository at this point in the history
* Better guard runner/app code

* Fix calculation of window interval
  • Loading branch information
eatonphil committed Oct 12, 2021
1 parent ba29c9e commit b50b7a4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 34 deletions.
51 changes: 29 additions & 22 deletions desktop/app.ts
Expand Up @@ -2,38 +2,45 @@ import { app, ipcMain } from 'electron';
import path from 'path';
import { APP_NAME, DEBUG, VERSION } from '../shared/constants';
import log from '../shared/log';
import { IS_DESKTOP_RUNNER } from './constants';
import { configureLogger } from './log';
import { openWindow } from './project';
import { registerRPCHandlers } from './rpc';
import { initialize } from './runner';
import { flushUnwritten, storeHandlers } from './store';

configureLogger();
log.info(APP_NAME, VERSION, DEBUG ? 'DEBUG' : '');
function main() {
configureLogger();
log.info(APP_NAME, VERSION, DEBUG ? 'DEBUG' : '');

['uncaughtException', 'unhandledRejection'].map((sig) =>
process.on(sig, log.error)
);
['uncaughtException', 'unhandledRejection'].map((sig) =>
process.on(sig, log.error)
);

// There doesn't seem to be a catchall signal
['exit', 'SIGUSR1', 'SIGUSR2', 'SIGINT'].map((sig) =>
process.on(sig, flushUnwritten)
);
// There doesn't seem to be a catchall signal
['exit', 'SIGUSR1', 'SIGUSR2', 'SIGINT'].map((sig) =>
process.on(sig, flushUnwritten)
);

// Just for basic unit tests
if (app) {
app.whenReady().then(async () => {
const { handlers, project } = initialize({
subprocess: path.join(__dirname, 'desktop_runner.js'),
additionalHandlers: storeHandlers,
});
// Just for basic unit tests
if (app) {
app.whenReady().then(async () => {
const { handlers, project } = initialize({
subprocess: path.join(__dirname, 'desktop_runner.js'),
additionalHandlers: storeHandlers,
});

await openWindow(project);

await openWindow(project);
registerRPCHandlers(ipcMain, handlers);
});

registerRPCHandlers(ipcMain, handlers);
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});
}
}

app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});
if (!IS_DESKTOP_RUNNER) {
main();
}
4 changes: 4 additions & 0 deletions desktop/constants.ts
Expand Up @@ -29,3 +29,7 @@ export const CODE_ROOT = (() => {

return '';
})();

export const IS_DESKTOP_RUNNER = (process.argv[1] || '').includes(
'desktop_runner.js'
);
24 changes: 15 additions & 9 deletions desktop/panel/eval.ts
Expand Up @@ -74,15 +74,21 @@ export async function evalInSubprocess(
// This means only one user can run a panel at a time
killAllByPanelId(panelId);

const child = execFile(process.argv[0], [
subprocess,
DSPROJ_FLAG,
projectName,
PANEL_FLAG,
panelId,
PANEL_META_FLAG,
tmp.path,
]);
const child = execFile(
process.argv[0],
[
subprocess,
DSPROJ_FLAG,
projectName,
PANEL_FLAG,
panelId,
PANEL_META_FLAG,
tmp.path,
],
{
windowsHide: true,
}
);

pid = child.pid;
if (!runningProcesses[panelId]) {
Expand Down
1 change: 1 addition & 0 deletions desktop/project.ts
Expand Up @@ -89,6 +89,7 @@ const menuTemplate = [
];

export async function openWindow(project: string, newProject: boolean = false) {
// TODO: update last open project on window exit too
if (!newProject) {
if (!project) {
project = SETTINGS.lastProject;
Expand Down
9 changes: 7 additions & 2 deletions desktop/runner.ts
Expand Up @@ -3,7 +3,12 @@ import 'source-map-support/register';
import { APP_NAME, DEBUG, VERSION } from '../shared/constants';
import log from '../shared/log';
import '../shared/polyfill';
import { DSPROJ_FLAG, PANEL_FLAG, PANEL_META_FLAG } from './constants';
import {
DSPROJ_FLAG,
IS_DESKTOP_RUNNER,
PANEL_FLAG,
PANEL_META_FLAG,
} from './constants';
import { configureLogger } from './log';
import { panelHandlers } from './panel';
import { openProjectHandler } from './project';
Expand Down Expand Up @@ -108,7 +113,7 @@ export async function main(additionalHandlers?: RPCHandler<any, any>[]) {
}
}

if ((process.argv[1] || '').includes('desktop_runner.js')) {
if (IS_DESKTOP_RUNNER) {
configureLogger();
log.info(APP_NAME + ' Panel Runner', VERSION, DEBUG ? 'DEBUG' : '');

Expand Down
2 changes: 1 addition & 1 deletion shared/sql.ts
Expand Up @@ -157,7 +157,7 @@ export function buildSQLiteQuery(vp: FilterAggregatePanelInfo): string {
let groupExpression = quote(groupBy, ANSI_SQL_QUOTE.identifier);
if (windowInterval && +windowInterval) {
const intervalSeconds = +windowInterval * 60;
groupExpression = `DATETIME((STRFTIME('%s', ${groupBy}) / ${intervalSeconds}) *${intervalSeconds}, 'unixepoch')`;
groupExpression = `DATETIME(STRFTIME('%s', ${groupBy}) - STRFTIME('%s', ${groupBy}) % ${intervalSeconds}, 'unixepoch')`;
groupColumn = `${groupExpression} ${groupBy}`;
}

Expand Down

0 comments on commit b50b7a4

Please sign in to comment.