Stopping all local servers when leaving application

This commit is contained in:
ziajka
2019-02-19 15:03:33 +01:00
parent 6759568933
commit 16185d2461
6 changed files with 95 additions and 36 deletions

View File

@ -138,7 +138,10 @@ export class AddServerDialogComponent implements OnInit {
}
getDefaultLocalServerPath() {
return this.electronService.remote.require('./local-server.js').getLocalServerPath();
if(this.electronService.isElectronApp) {
return this.electronService.remote.require('./local-server.js').getLocalServerPath();
}
return;
}
ngOnInit() {

View File

@ -16,7 +16,7 @@ class ElectronServiceMock {
public isElectronApp: boolean;
}
describe('DefaultLayoutComponent', () => {
fdescribe('DefaultLayoutComponent', () => {
let component: DefaultLayoutComponent;
let fixture: ComponentFixture<DefaultLayoutComponent>;
let electronServiceMock: ElectronServiceMock;
@ -89,4 +89,25 @@ describe('DefaultLayoutComponent', () => {
});
expect(toaster.errors).toEqual([]);
});
describe('auto stopping servers', () => {
let event;
beforeEach(() => {
event = new Event('onbeforeunload');
});
it('should close window with no action when not in electron', async () => {
component.shouldStopServersOnClosing = false;
const isClosed = await component.onBeforeUnload(event);
expect(isClosed).toBeUndefined();
});
it('should stop all servers and close window', () => {
component.shouldStopServersOnClosing = true;
const isClosed = component.onBeforeUnload(event);
expect(isClosed).toBeTruthy();
});
});
});

View File

@ -1,8 +1,9 @@
import { ElectronService } from 'ngx-electron';
import { Component, OnInit, ViewEncapsulation, OnDestroy } from '@angular/core';
import { Component, OnInit, ViewEncapsulation, OnDestroy, HostListener } from '@angular/core';
import { ServerManagementService } from '../../services/server-management.service';
import { Subscription } from 'rxjs';
import { ToasterService } from '../../services/toaster.service';
import { ProgressService } from '../../common/progress/progress.service';
@Component({
selector: 'app-default-layout',
@ -14,22 +15,42 @@ export class DefaultLayoutComponent implements OnInit, OnDestroy {
public isInstalledSoftwareAvailable = false;
serverStatusSubscription: Subscription;
shouldStopServersOnClosing = true;
constructor(
private electronService: ElectronService,
private serverManagement: ServerManagementService,
private toasterService: ToasterService
private toasterService: ToasterService,
private progressService: ProgressService
) {}
ngOnInit() {
this.isInstalledSoftwareAvailable = this.electronService.isElectronApp;
// attach to notification stream when any of running local servers experienced issues
this.serverStatusSubscription = this.serverManagement.serverStatusChanged.subscribe((serverStatus) => {
if(serverStatus.status === 'errored') {
this.toasterService.error(serverStatus.message);
}
});
// stop servers only when in Electron
this.shouldStopServersOnClosing = this.electronService.isElectronApp;
}
@HostListener('window:beforeunload', ['$event'])
async onBeforeUnload($event) {
if(!this.shouldStopServersOnClosing) {
return;
}
$event.preventDefault()
$event.returnValue = false;
this.progressService.activate();
await this.serverManagement.stopAll();
this.shouldStopServersOnClosing = false;
this.progressService.deactivate();
window.close();
return false;
}
ngOnDestroy() {

View File

@ -29,15 +29,22 @@ export class ServerManagementService implements OnDestroy {
}
async start(server: Server) {
await this.electronService.remote.require('./local-server.js').startLocalServer(server);
return await this.electronService.remote.require('./local-server.js').startLocalServer(server);
}
async stop(server: Server) {
await this.electronService.remote.require('./local-server.js').stopLocalServer(server);
return await this.electronService.remote.require('./local-server.js').stopLocalServer(server);
}
async stopAll() {
return await this.electronService.remote.require('./local-server.js').stopAllLocalServers();
}
getRunningServers() {
return this.electronService.remote.require('./local-server.js').getRunningServers();
if(this.electronService.isElectronApp) {
return this.electronService.remote.require('./local-server.js').getRunningServers();
}
return [];
}
ngOnDestroy() {