Skip to content

Commit cc8ddc4

Browse files
authored
chore(auto-update-manager): add logging for autoupdates (#3053)
* chore: add logging for autoupdates * fix logging of platform and product * add missing dep * add MONGODB_COMPASS_AUTO_UPDATE_ENDPOINT * revert env var
1 parent c0f2529 commit cc8ddc4

File tree

5 files changed

+95
-33
lines changed

5 files changed

+95
-33
lines changed

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass/src/main/application.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,8 @@ class CompassApplication {
3131

3232
this.setupUserDirectory();
3333

34-
await Promise.all([
35-
this.setupLogging(),
36-
this.setupAutoUpdate(),
37-
this.setupSecureStore(),
38-
this.setupTelemetry(),
39-
]);
34+
await Promise.all([this.setupLogging(), this.setupTelemetry()]);
35+
await Promise.all([this.setupAutoUpdate(), this.setupSecureStore()]);
4036

4137
await setupCSFLELibrary();
4238
this.setupJavaScriptArguments();
@@ -46,7 +42,7 @@ class CompassApplication {
4642
}
4743

4844
static init(): Promise<void> {
49-
return this.initPromise ??= this._init();
45+
return (this.initPromise ??= this._init());
5046
}
5147

5248
private static async setupSecureStore(): Promise<void> {

packages/compass/src/main/auto-update-manager.ts

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,85 @@
1-
import createDebug from 'debug';
21
import AutoUpdateManager from 'hadron-auto-update-manager';
32
import { ipcMain } from 'hadron-ipc';
43
import COMPASS_ICON from './icon';
54

6-
const debug = createDebug(
7-
'mongodb-compass:main:application:auto-update-manager'
5+
import { createLoggerAndTelemetry } from '@mongodb-js/compass-logging';
6+
const { log, mongoLogId, debug } = createLoggerAndTelemetry(
7+
'COMPASS-AUTO-UPDATES'
88
);
99

1010
/**
1111
* Map package.json product names to API endpoint product names.
1212
*/
13-
const API_PRODUCT = {
13+
const API_PRODUCT: Record<string, string> = {
1414
'mongodb-compass': 'compass',
1515
'mongodb-compass-readonly': 'compass-readonly',
1616
};
1717

18-
function isSupportedProduct(str?: string): str is keyof typeof API_PRODUCT {
19-
return Object.keys(API_PRODUCT).includes(str as string);
20-
}
21-
2218
/**
2319
* Platform API mappings.
2420
*/
25-
const API_PLATFORM = {
21+
const API_PLATFORM: Record<string, string> = {
2622
darwin: 'osx',
2723
win32: 'windows',
2824
linux: 'linux',
2925
};
3026

31-
function isSupportedPlatform(str?: string): str is keyof typeof API_PLATFORM {
32-
return Object.keys(API_PLATFORM).includes(str as string);
33-
}
34-
3527
class CompassAutoUpdateManager {
3628
private static initCalled = false;
3729

3830
private static _init(): void {
39-
if (
40-
!isSupportedPlatform(process.platform) ||
41-
!isSupportedProduct(process.env.HADRON_PRODUCT)
42-
) {
31+
log.info(
32+
mongoLogId(1001000130),
33+
'CompassAutoUpdateManager',
34+
'Initializing'
35+
);
36+
37+
const product = API_PRODUCT[process.env.HADRON_PRODUCT];
38+
if (!product) {
39+
log.info(
40+
mongoLogId(1001000131),
41+
'CompassAutoUpdateManager',
42+
'Skipping setup for unknown product',
43+
{
44+
productId: process.env.HADRON_PRODUCT,
45+
}
46+
);
47+
48+
return;
49+
}
50+
51+
const platform = API_PLATFORM[process.platform];
52+
if (!platform) {
53+
log.info(
54+
mongoLogId(1001000132),
55+
'CompassAutoUpdateManager',
56+
'Skipping setup on unknown platform',
57+
{
58+
platformId: process.platform,
59+
}
60+
);
61+
4362
return;
4463
}
4564

46-
const updateManager = new AutoUpdateManager({
65+
const autoUpdateManagerOptions = {
4766
endpoint: process.env.HADRON_AUTO_UPDATE_ENDPOINT,
4867
icon: COMPASS_ICON,
49-
product: API_PRODUCT[process.env.HADRON_PRODUCT],
68+
product: product,
5069
channel: process.env.HADRON_CHANNEL,
51-
platform: API_PLATFORM[process.platform],
52-
});
70+
platform: platform,
71+
};
72+
73+
log.info(
74+
mongoLogId(1001000133),
75+
'CompassAutoUpdateManager',
76+
'Setting up updateManager',
77+
{
78+
...autoUpdateManagerOptions,
79+
}
80+
);
81+
82+
const updateManager = new AutoUpdateManager(autoUpdateManagerOptions);
5383

5484
updateManager.on('state-change', (newState) => {
5585
debug('new state', newState);

packages/hadron-auto-update-manager/index.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ const dialog = electron.dialog;
44
const _ = require('lodash');
55
const EventEmitter = require('events').EventEmitter;
66
const autoUpdater = require('./auto-updater');
7-
const debug = require('debug')('hadron-auto-update-manager');
7+
8+
const {createLoggerAndTelemetry} = require('@mongodb-js/compass-logging');
9+
const { log, mongoLogId, debug } = createLoggerAndTelemetry('COMPASS-AUTO-UPDATES');
10+
811
const ENOSIGNATURE = 'Could not get code signature for running application';
912
const app = electron.app;
1013

@@ -16,6 +19,7 @@ const NoUpdateAvailableState = 'no-update-available';
1619
// const UnsupportedState = 'unsupported';
1720
const ErrorState = 'error';
1821

22+
1923
function AutoUpdateManager(endpointURL, iconURL, product, channel, platform) {
2024
if (!endpointURL) {
2125
throw new TypeError('endpointURL is required!');
@@ -44,7 +48,17 @@ function AutoUpdateManager(endpointURL, iconURL, product, channel, platform) {
4448
});
4549

4650
process.nextTick(() => {
47-
this.setupAutoUpdater();
51+
try {
52+
this.setupAutoUpdater();
53+
} catch (e) {
54+
log.error(mongoLogId(1001000134),
55+
'AutoUpdateManager',
56+
'Error while setting up the auto updater',
57+
{
58+
error: e.message
59+
}
60+
);
61+
}
4862
});
4963
}
5064
_.extend(AutoUpdateManager.prototype, EventEmitter.prototype);
@@ -54,31 +68,50 @@ AutoUpdateManager.prototype.setupAutoUpdater = function() {
5468
// Else we get the default node.js error event handling:
5569
// die hard if errors are unhandled.
5670
autoUpdater.on('error', (event, message) => {
71+
log.error(mongoLogId(1001000129),
72+
'AutoUpdateManager',
73+
'Error Downloading Update',
74+
{
75+
event, message
76+
}
77+
);
78+
5779
if (message === ENOSIGNATURE) {
5880
debug('no auto updater for unsigned builds');
5981
return this.setState('unsupported');
6082
}
61-
debug('Error Downloading Update: ' + message);
83+
6284
return this.setState(ErrorState);
6385
});
6486

65-
autoUpdater.setFeedURL(this.feedURL);
66-
6787
autoUpdater.on('checking-for-update', () => {
88+
log.info(mongoLogId(1001000125), 'AutoUpdateManager', 'Checking for updates ...');
6889
this.setState(CheckingState);
6990
});
7091

7192
autoUpdater.on('update-not-available', () => {
93+
log.info(mongoLogId(1001000126), 'AutoUpdateManager', 'Update not available');
7294
this.setState(NoUpdateAvailableState);
7395
});
96+
7497
autoUpdater.on('update-available', () => {
98+
log.info(mongoLogId(1001000127), 'AutoUpdateManager', 'Update available');
7599
this.setState(DownloadingState);
76100
});
101+
77102
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseVersion) => {
103+
log.info(mongoLogId(1001000128), 'AutoUpdateManager', 'Update downloaded', {
104+
releaseVersion
105+
});
106+
78107
this.releaseNotes = releaseNotes;
79108
this.releaseVersion = releaseVersion;
80109
this.setState(UpdateAvailableState);
81110
});
111+
112+
autoUpdater.setFeedURL(this.feedURL);
113+
log.info(mongoLogId(1001000124), 'AutoUpdateManager', 'Feed url set',
114+
{feedURL: this.feedURL});
82115
};
83116

84117
/**

packages/hadron-auto-update-manager/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"compass:main": "index.js",
1919
"types": "index.d.ts",
2020
"dependencies": {
21+
"@mongodb-js/compass-logging": "^0.11.0",
2122
"debug": "4.3.0",
2223
"got": "^10.4.0",
2324
"lodash": "^4.17.15"

0 commit comments

Comments
 (0)