Skip to content

Commit

Permalink
Send crash statistics to Sentry. (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
KenCorma authored Sep 25, 2024
1 parent d59d985 commit e10c1bc
Show file tree
Hide file tree
Showing 8 changed files with 848 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
},
"license": "GPL-3.0-only",
"dependencies": {
"@sentry/electron": "^5.4.0",
"@sentry/vite-plugin": "^2.22.4",
"adm-zip": "^0.5.15",
"axios": "^1.7.7",
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/unit/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ jest.mock('node:path', () => ({
}),
}));

jest.mock('@sentry/electron/main', () => ({
init: jest.fn(),
captureException: jest.fn(),
}));

jest.mock('tar', () => ({
extract: jest.fn(),
}));
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ export const IPC_CHANNELS = {
};

export const ELECTRON_BRIDGE_API = 'electronAPI';

export const SENTRY_URL_ENDPOINT =
'https://942cadba58d247c9cab96f45221aa813@o4507954455314432.ingest.us.sentry.io/4508007940685824';
37 changes: 32 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import fs from 'fs';
import axios from 'axios';
import path from 'node:path';
import { SetupTray } from './tray';
import { IPC_CHANNELS } from './constants';
import { IPC_CHANNELS, SENTRY_URL_ENDPOINT } from './constants';
import dotenv from 'dotenv';
import { app, BrowserWindow, webContents, screen, ipcMain } from 'electron';
import { app, BrowserWindow, webContents, screen, ipcMain, crashReporter } from 'electron';
import tar from 'tar';
import log from 'electron-log/main';
import * as Sentry from '@sentry/electron/main';

import { updateElectronApp, UpdateSourceType } from 'update-electron-app';

Expand All @@ -30,6 +31,32 @@ import('electron-squirrel-startup').then((ess) => {
}
});

app.isPackaged &&
Sentry.init({
dsn: SENTRY_URL_ENDPOINT,
/* //WIP gather and send log from main
beforeSend(event, hint) {
hint.attachments = [
{
filename: 'main.log',
attachmentType: 'event.attachment',
data: readLogMain(),
},
];
return event;
}, */
integrations: [
Sentry.childProcessIntegration({
breadcrumbs: ['abnormal-exit', 'killed', 'crashed', 'launch-failed', 'oom', 'integrity-failure'],
events: ['abnormal-exit', 'killed', 'crashed', 'launch-failed', 'oom', 'integrity-failure'],
}),
],
});

function readLogMain() {
return log.transports.file.readAllLogs()[0].lines.slice(-100).join('\n');
}

app.on('ready', () => {
log.info('App is Ready');
});
Expand Down Expand Up @@ -133,9 +160,9 @@ const isComfyServerReady = async (host: string, port: number): Promise<boolean>
};

// Launch Python Server Variables
const maxFailWait: number = 50 * 1000; // 50seconds
const maxFailWait: number = 60 * 1000; // 60seconds
let currentWaitTime = 0;
const spawnServerTimeout: NodeJS.Timeout = null;
let spawnServerTimeout: NodeJS.Timeout = null;

const launchPythonServer = async (
pythonInterpreterPath: string,
Expand Down Expand Up @@ -201,7 +228,7 @@ const launchPythonServer = async (
resolve();
} else {
log.info('Ping failed. Retrying...');
setTimeout(checkServerReady, checkInterval);
spawnServerTimeout = setTimeout(checkServerReady, checkInterval);
}
};

Expand Down
1 change: 1 addition & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const electronAPI = {
log.info('Sending ready event to main process');
ipcRenderer.send(IPC_CHANNELS.RENDERER_READY);
},
isPackaged: !process.argv0.endsWith('electron.exe'), //Emulates app.ispackaged in renderer
};

contextBridge.exposeInMainWorld(ELECTRON_BRIDGE_API, electronAPI);
11 changes: 11 additions & 0 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
import './index.css';
import ReactDOM from 'react-dom/client';
import Home from './renderer/index';
import * as Sentry from '@sentry/electron/renderer';
import { ELECTRON_BRIDGE_API, SENTRY_URL_ENDPOINT } from './constants';

if (ELECTRON_BRIDGE_API in window) {
if ((window as any).electronAPI.isPackaged) {
//TODO set up report dialog
Sentry.init({
dsn: SENTRY_URL_ENDPOINT,
});
}
}

// Generate the the app then render the root
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(Home());
1 change: 1 addition & 0 deletions vite.renderer.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default defineConfig((env) => {
build: {
outDir: `.vite/renderer/${name}`,
},
define: {},
plugins: [pluginExposeRenderer(name)],
resolve: {
alias: [{ find: 'src', replacement: resolve(__dirname, './src/') }],
Expand Down
Loading

0 comments on commit e10c1bc

Please sign in to comment.