From 053cd2780c4da5b328d579dedc28331b25761226 Mon Sep 17 00:00:00 2001 From: Jan Hovancik Date: Tue, 14 Nov 2023 18:27:38 +0100 Subject: [PATCH] Improve DND monitoring better use machine resources fix tests --- .github/workflows/tests.yml | 22 ++++++++++++++++++++++ .nvmrc | 1 + CHANGELOG.md | 3 +++ app/preferences.html | 2 +- app/utils/dndManager.js | 26 +++++++++++++++++--------- package.json | 4 ++-- test/dndManager.js | 17 ++++++++++++----- 7 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 .nvmrc diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index afb4189b4..54b647e5b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -12,6 +12,24 @@ jobs: fail-fast: false steps: + - name: Setup DBUS environment + if: matrix.os == 'ubuntu-latest' + run: | + export DISPLAY=:0 + sudo apt install dbus-x11 + machineId=$(cat /var/lib/dbus/machine-id) + mkdir -p /home/runner/.dbus + mkdir -p /home/runner/.dbus/session-bus/ + touch /home/runner/.dbus/session-bus/${machineId}-0 + sudo dbus-launch --exit-with-session + sleep 5 # Wait for the DBUS session to start + echo $DBUS_SESSION_BUS_ADDRESS + eval `dbus-launch --sh-syntax` + address=$(echo $DBUS_SESSION_BUS_ADDRESS) + echo "DBUS_SESSION_BUS_ADDRESS=${address}" > /home/runner/.dbus/session-bus/${machineId}-0 + - name: Check for file + if: matrix.os == 'ubuntu-latest' + run: cat /home/runner/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0 - name: Set Timezone uses: szenius/set-timezone@v1.1 with: @@ -28,8 +46,12 @@ jobs: node-version: '18.17.1' - run: npm install npm@9 -g - run: npm install + - name: Install missing usocket + if: matrix.os == 'ubuntu-latest' + run: npm install usocket - run: npx nyc --reporter=lcov npm test env: CI: true + DISPLAY: ':0' - name: Codecov uses: codecov/codecov-action@v3 diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 000000000..4a1f488b6 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +18.17.1 diff --git a/CHANGELOG.md b/CHANGELOG.md index f59ac85f7..72856ec07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Fixed +- improve DND monitoring memory usage + ## [1.15.0] - 2023-11-11 ### Added - new end-of-the-break sound diff --git a/app/preferences.html b/app/preferences.html index 7f88a3613..c418c8e8e 100644 --- a/app/preferences.html +++ b/app/preferences.html @@ -69,7 +69,7 @@ -
+
diff --git a/app/utils/dndManager.js b/app/utils/dndManager.js index 06f2c622d..54a0f99a9 100644 --- a/app/utils/dndManager.js +++ b/app/utils/dndManager.js @@ -8,6 +8,17 @@ class DndManager extends EventEmitter { this.monitorDnd = settings.get('monitorDnd') this.timer = null this.isOnDnd = false + + if (process.platform === 'win32') { + this.windowsFocusAssist = require('windows-focus-assist') + this.windowsQuietHours = require('windows-quiet-hours') + } else if (process.platform === 'darwin') { + this.macosNotificationState = require('macos-notification-state') + } else if (process.platform === 'linux') { + this.bus = require('dbus-final').sessionBus() + this.util = require('node:util') + } + if (this.monitorDnd) { this.start() } @@ -31,10 +42,8 @@ class DndManager extends EventEmitter { } async _isDndEnabledLinux () { - const dbus = require('dbus-final') - const bus = dbus.sessionBus() try { - const obj = await bus.getProxyObject('org.freedesktop.Notifications', '/org/freedesktop/Notifications') + const obj = await this.bus.getProxyObject('org.freedesktop.Notifications', '/org/freedesktop/Notifications') const properties = obj.getInterface('org.freedesktop.DBus.Properties') const dndEnabled = await properties.Get('org.freedesktop.Notifications', 'Inhibited') if (await dndEnabled.value) { @@ -45,7 +54,7 @@ class DndManager extends EventEmitter { } try { - const obj = await bus.getProxyObject('org.xfce.Xfconf', '/org/xfce/Xfconf') + const obj = await this.bus.getProxyObject('org.xfce.Xfconf', '/org/xfce/Xfconf') const properties = obj.getInterface('org.xfce.Xfconf') const dndEnabled = await properties.GetProperty('xfce4-notifyd', '/do-not-disturb') if (await dndEnabled.value) { @@ -56,8 +65,7 @@ class DndManager extends EventEmitter { } try { - const util = require('node:util') - const exec = util.promisify(require('node:child_process').exec) + const exec = this.util.promisify(require('node:child_process').exec) const { stdout } = await exec('gsettings get org.gnome.desktop.notifications show-banners') if (stdout.replace(/[^0-9a-zA-Z]/g, '') === 'false') { return true @@ -75,12 +83,12 @@ class DndManager extends EventEmitter { if (process.platform === 'win32') { let wfa = 0 try { - wfa = require('windows-focus-assist').getFocusAssist().value + wfa = this.windowsFocusAssist.getFocusAssist().value } catch (e) { wfa = -1 } // getFocusAssist() throw an error if OS isn't windows - const wqh = require('windows-quiet-hours').getIsQuietHours() + const wqh = this.windowsQuietHours.getIsQuietHours() return wqh || (wfa !== -1 && wfa !== 0) } else if (process.platform === 'darwin') { - return require('macos-notification-state').getDoNotDisturb() + return this.macosNotificationState.getDoNotDisturb() } else if (process.platform === 'linux') { return await this._isDndEnabledLinux() } diff --git a/package.json b/package.json index 3f18c8220..8400ffbb1 100755 --- a/package.json +++ b/package.json @@ -12,8 +12,8 @@ "postinstall": "electron-builder install-app-deps", "pack": "electron-builder build --dir", "dist": "electron-builder build --x64 --ia32", - "test": "mocha test", - "test-single": "mocha $1", + "test": "mocha test --exit", + "test-single": "mocha $1 --exit", "tdd": "mocha --watch test", "lint": "./node_modules/.bin/standard", "prepublishOnly": "pinst --disable", diff --git a/test/dndManager.js b/test/dndManager.js index a7203a98b..c29ff525f 100644 --- a/test/dndManager.js +++ b/test/dndManager.js @@ -4,8 +4,10 @@ const DndManager = require('../app/utils/dndManager') const Store = require('electron-store') chai.should() +const timeout = process.env.CI ? 30000 : 10000 describe('dndManager', function () { + this.timeout(timeout) let settings = null let dndManager = null @@ -18,40 +20,45 @@ describe('dndManager', function () { dndManager = new DndManager(settings) }) - it('should be running with default settings', () => { + it('should be running with default settings', (done) => { dndManager.isOnDnd.should.be.equal(false) dndManager.monitorDnd.should.be.equal(true) + done() }) - it('should not be running with monitorDnd: false', () => { + it('should not be running with monitorDnd: false', (done) => { settings.set('monitorDnd', false) dndManager.stop() dndManager = null dndManager = new DndManager(settings) dndManager.isOnDnd.should.be.equal(false) dndManager.monitorDnd.should.be.equal(false) + done() }) - it('should be running with monitorDnd: true', () => { + it('should be running with monitorDnd: true', (done) => { settings.set('monitorDnd', true) dndManager.stop() dndManager = null dndManager = new DndManager(settings) dndManager.isOnDnd.should.be.equal(false) dndManager.monitorDnd.should.be.equal(true) + done() }) - it('should start when start()', () => { + it('should start when start()', (done) => { dndManager.stop() dndManager.start() dndManager.isOnDnd.should.be.equal(false) dndManager.monitorDnd.should.be.equal(true) + done() }) - it('should stop when stop()', () => { + it('should stop when stop()', (done) => { dndManager.stop() dndManager.monitorDnd.should.be.equal(false) dndManager.isOnDnd.should.be.equal(false) + done() }) afterEach(() => {