fix: stop engine failed when app quit

This commit is contained in:
Dr_rOot
2023-04-27 10:23:08 +08:00
parent 7a51dbaf5e
commit 003e9ffccb
5 changed files with 23 additions and 11 deletions
+17 -10
View File
@@ -151,8 +151,10 @@ export default class Application extends EventEmitter {
}
async stopEngine () {
logger.info('[Motrix] stopEngine===>')
try {
await this.engineClient.shutdown({ force: true })
logger.info('[Motrix] stopEngine.setImmediate===>')
setImmediate(() => {
this.engine.stop()
})
@@ -509,22 +511,27 @@ export default class Application extends EventEmitter {
this.windowManager.destroyWindow(page)
}
async stop () {
stop () {
try {
await this.shutdownUPnPManager()
const promises = [
this.stopEngine(),
this.shutdownUPnPManager(),
this.energyManager.stopPowerSaveBlocker(),
this.trayManager.destroy()
]
this.energyManager.stopPowerSaveBlocker()
await this.stopEngine()
this.trayManager.destroy()
return promises
} catch (err) {
logger.warn('[Motrix] stop error: ', err.message)
}
}
async stopAllSettled () {
await Promise.allSettled(this.stop())
}
async quit () {
await this.stop()
await this.stopAllSettled()
app.exit()
}
@@ -644,7 +651,7 @@ export default class Application extends EventEmitter {
this.updateManager.on('will-updated', async (event) => {
this.windowManager.setWillQuit(true)
await this.stop()
await this.stopAllSettled()
})
this.updateManager.on('update-error', (event) => {
@@ -654,7 +661,7 @@ export default class Application extends EventEmitter {
}
async relaunch () {
await this.stop()
await this.stopAllSettled()
app.relaunch()
app.exit()
}
+1
View File
@@ -183,6 +183,7 @@ export default class Launcher extends EventEmitter {
app.on('will-quit', () => {
logger.info('[Motrix] will-quit')
if (global.application) {
logger.info('[Motrix] will-quit.application.stop')
global.application.stop()
}
})
+2
View File
@@ -5,6 +5,7 @@ import logger from './Logger'
let psbId
export default class EnergyManager {
startPowerSaveBlocker () {
logger.info('[Motrix] EnergyManager.startPowerSaveBlocker', psbId)
if (psbId && powerSaveBlocker.isStarted(psbId)) {
return
}
@@ -14,6 +15,7 @@ export default class EnergyManager {
}
stopPowerSaveBlocker () {
logger.info('[Motrix] EnergyManager.stopPowerSaveBlocker', psbId)
if (typeof psbId === 'undefined' || !powerSaveBlocker.isStarted(psbId)) {
return
}
+1
View File
@@ -67,6 +67,7 @@ export default class Engine {
}
stop () {
logger.info('[Motrix] engine.stop.instance')
if (this.instance) {
this.instance.kill()
this.instance = null
+2 -1
View File
@@ -5,6 +5,7 @@ import is from 'electron-is'
import { APP_RUN_MODE, APP_THEME } from '@shared/constants'
import { getInverseTheme } from '@shared/utils'
import logger from '../core/Logger'
import { getI18n } from './Locale'
import {
translateTemplate,
@@ -12,7 +13,6 @@ import {
updateStates
} from '../utils/menu'
import { convertArrayBufferToBuffer } from '../utils/index'
// import logger from '../core/Logger'
let tray = null
const { platform } = process
@@ -357,6 +357,7 @@ export default class TrayManager extends EventEmitter {
}
destroy () {
logger.info('[Motrix] TrayManager.destroy')
if (tray) {
this.unbindEvents()
}