Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ceanup browser sync processes #23

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getServiceContainer } from "@getflywheel/local/main";

const { localLogger } = getServiceContainer().cradle;

/**
* An InstantReload scoped child logger
*/
const log = localLogger.child({
addon: 'InstantReload',
});

export default log;
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import * as Local from '@getflywheel/local';
import { asValue } from 'awilix';
import { getServiceContainer, HooksMain, addIpcAsyncListener } from '@getflywheel/local/main';
import InstantReload from './main/InstantReload';
import { IPC_EVENTS } from './constants';
import log from './logger';

const serviceContainer = getServiceContainer().cradle;
const localContainer = getServiceContainer();
const serviceContainer = localContainer.cradle;

export default function (context: typeof serviceContainer.addonLoader.addonContext): void {
const instantReload = new InstantReload();

localContainer.register({ instantReload: asValue(instantReload) } as any);

HooksMain.addAction('siteStarted', async (site: Local.Site) => {
try {

Expand Down Expand Up @@ -94,7 +99,7 @@ export default function (context: typeof serviceContainer.addonLoader.addonConte
try {
result = serviceContainer.siteData.updateSite(siteID, { autoEnableInstantReload });
} catch (err) {
console.error(err);
log.error(`Unable to enable InstantReload for ${siteID}: ${err.message}`);
}

return result;
Expand Down
31 changes: 26 additions & 5 deletions src/main/InstantReload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '@getflywheel/local/main';
import { IPC_EVENTS, INSTANT_RELOAD_EVENTS } from '../constants';
import { STATUSES, InstantReloadStatus } from '../types';
import log from '../logger';

const serviceContainer = getServiceContainer().cradle;

Expand All @@ -26,19 +27,36 @@ export default class InstantReloadService {

private _sendIPCEvent: typeof serviceContainer.sendIPCEvent;

private _localLogger: typeof serviceContainer.localLogger;

/**
* hash map of all browser sync instances by site id
*/
private _browserSyncInstances: { [key: string]: InstanceData | null } = {};

/**
* Handle cleanup tasks when this class is destroyed, for example, when the
* Local App quits.
*/
public onDestroy (): Promise<void[]> {
return this.stopAllConnections();
}

constructor () {
const { localLogger, siteData, sendIPCEvent } = serviceContainer;
const { siteData, sendIPCEvent } = serviceContainer;

this._siteData = siteData;
this._sendIPCEvent = sendIPCEvent;
this._localLogger = localLogger;
}

/**
* Stops all running Browsersync processes
*/
public stopAllConnections (): Promise<void[]> {
const siteIds = Object.keys(this._browserSyncInstances);
const siteStopPromises = siteIds.map((siteId) => {
this.stopConnection(this._siteData.getSite(siteId));
});

return Promise.all(siteStopPromises);
}

/**
Expand Down Expand Up @@ -85,8 +103,11 @@ export default class InstantReloadService {
*/
try {
instanceData.childProcess.kill();
log.info(
`Killing Instant Reload child process for ${site.name}.`,
);
} catch (err) {
this._localLogger.debug(`Error killing Instant Reload child process for ${site.name}. ${err}`);
log.error(`Error killing Instant Reload child process for ${site.name}. ${err}`);
}

this._browserSyncInstances[site.id] = null;
Expand Down