Skip to content

Commit 0ce13f5

Browse files
fix: replace processService with cleanupService
Replace processService with cleanupService where appropriate. The cleanupService allows execution of code after CLI has died. `processService` was used to handle some events, but it caused more issues than benefits. So remove it from the codebase.
1 parent 6028eae commit 0ce13f5

37 files changed

+53
-321
lines changed

lib/common/bootstrap.ts

-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ $injector.require("iOSEmulatorServices", "./mobile/ios/simulator/ios-emulator-se
8585
$injector.require("wp8EmulatorServices", "./mobile/wp8/wp8-emulator-services");
8686

8787
$injector.require("autoCompletionService", "./services/auto-completion-service");
88-
$injector.require("processService", "./services/process-service");
8988
$injector.requirePublic("settingsService", "./services/settings-service");
9089
$injector.require("opener", "./opener");
9190
$injector.require("microTemplateService", "./services/micro-templating-service");

lib/common/declarations.d.ts

-5
Original file line numberDiff line numberDiff line change
@@ -1466,11 +1466,6 @@ interface INet {
14661466
waitForPortToListen(waitForPortListenData: IWaitForPortListenData): Promise<boolean>;
14671467
}
14681468

1469-
interface IProcessService {
1470-
listenersCount: number;
1471-
attachToProcessExitSignals(context: any, callback: () => void): void;
1472-
}
1473-
14741469
interface IDependencyInformation {
14751470
name: string;
14761471
version?: string;

lib/common/http-client.ts

-6
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@ export class HttpClient implements Server.IHttpClient {
1919
private cleanupData: ICleanupRequestData[];
2020

2121
constructor(private $logger: ILogger,
22-
private $processService: IProcessService,
2322
private $proxyService: IProxyService,
2423
private $staticConfig: Config.IStaticConfig) {
2524
this.cleanupData = [];
26-
this.$processService.attachToProcessExitSignals(this, () => {
27-
this.cleanupData.forEach(d => {
28-
this.cleanupAfterRequest(d);
29-
});
30-
});
3125
}
3226

3327
public async httpRequest(options: any, proxySettings?: IProxySettings): Promise<Server.IResponse> {

lib/common/mobile/android/logcat-helper.ts

-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export class LogcatHelper implements Mobile.ILogcatHelper {
1616
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
1717
private $logger: ILogger,
1818
private $injector: IInjector,
19-
private $processService: IProcessService,
2019
private $devicesService: Mobile.IDevicesService) {
2120
this.mapDevicesLoggingData = Object.create(null);
2221
}
@@ -53,8 +52,6 @@ export class LogcatHelper implements Mobile.ILogcatHelper {
5352
const lineText = line.toString();
5453
this.$deviceLogProvider.logData(lineText, this.$devicePlatformsConstants.Android, deviceIdentifier);
5554
});
56-
57-
this.$processService.attachToProcessExitSignals(this, logcatStream.kill);
5855
}
5956
}
6057

@@ -72,8 +69,6 @@ export class LogcatHelper implements Mobile.ILogcatHelper {
7269
logcatDumpStream.removeAllListeners();
7370
lineStream.removeAllListeners();
7471
});
75-
76-
this.$processService.attachToProcessExitSignals(this, logcatDumpStream.kill);
7772
}
7873

7974
/**

lib/common/mobile/ios/device/ios-device-operations.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,11 @@ export class IOSDeviceOperations extends EventEmitter implements IIOSDeviceOpera
99
public shouldDispose: boolean;
1010
private deviceLib: IOSDeviceLib.IOSDeviceLib;
1111

12-
constructor(private $logger: ILogger,
13-
private $processService: IProcessService) {
12+
constructor(private $logger: ILogger) {
1413
super();
1514

1615
this.isInitialized = false;
1716
this.shouldDispose = true;
18-
this.$processService.attachToProcessExitSignals(this, () => {
19-
this.setShouldDispose(true);
20-
this.dispose();
21-
});
2217
}
2318

2419
public async install(ipaPath: string, deviceIdentifiers: string[], errorHandler: DeviceOperationErrorHandler): Promise<IOSDeviceResponse> {

lib/common/mobile/ios/device/ios-device.ts

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export class IOSDevice extends IOSDeviceBase {
2020
protected $deviceLogProvider: Mobile.IDeviceLogProvider,
2121
protected $lockService: ILockService,
2222
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
23-
protected $processService: IProcessService,
2423
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
2524
private $iOSDeviceProductNameMapper: Mobile.IiOSDeviceProductNameMapper,
2625
private $iosDeviceOperations: IIOSDeviceOperations,

lib/common/mobile/ios/ios-device-base.ts

-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export abstract class IOSDeviceBase implements Mobile.IiOSDevice {
66
protected abstract $errors: IErrors;
77
protected abstract $deviceLogProvider: Mobile.IDeviceLogProvider;
88
protected abstract $iOSDebuggerPortService: IIOSDebuggerPortService;
9-
protected abstract $processService: IProcessService;
109
protected abstract $lockService: ILockService;
1110
abstract deviceInfo: Mobile.IDeviceInfo;
1211
abstract applicationManager: Mobile.IDeviceApplicationManager;
@@ -33,8 +32,6 @@ export abstract class IOSDeviceBase implements Mobile.IiOSDevice {
3332
this.cachedSockets[appId].on("close", async () => {
3433
await this.destroyDebugSocket(appId);
3534
});
36-
37-
this.$processService.attachToProcessExitSignals(this, () => this.destroyDebugSocket(appId));
3835
}
3936

4037
return this.cachedSockets[appId];

lib/common/mobile/ios/simulator/ios-simulator-application-manager.ts

-8
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,10 @@ export class IOSSimulatorApplicationManager extends ApplicationManagerBase {
1515
private device: Mobile.IiOSDevice,
1616
private $options: IOptions,
1717
private $fs: IFileSystem,
18-
private $processService: IProcessService,
1918
private $deviceLogProvider: Mobile.IDeviceLogProvider,
2019
$logger: ILogger,
2120
$hooksService: IHooksService) {
2221
super($logger, $hooksService);
23-
this.$processService.attachToProcessExitSignals(this, () => {
24-
for (const appId in this._lldbProcesses) {
25-
/* tslint:disable:no-floating-promises */
26-
this.detachNativeDebugger(appId);
27-
/* tslint:enable:no-floating-promises */
28-
}
29-
});
3022
}
3123

3224
public async getInstalledApplications(): Promise<string[]> {

lib/common/mobile/ios/simulator/ios-simulator-device.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ export class IOSSimulator extends IOSDeviceBase implements Mobile.IiOSDevice {
2222
private $iOSEmulatorServices: Mobile.IiOSSimulatorService,
2323
private $iOSNotification: IiOSNotification,
2424
private $iOSSimulatorLogProvider: Mobile.IiOSSimulatorLogProvider,
25-
private $logger: ILogger,
26-
protected $processService: IProcessService) {
25+
private $logger: ILogger) {
2726
super();
2827
this.applicationManager = this.$injector.resolve(applicationManagerPath.IOSSimulatorApplicationManager, { iosSim: this.$iOSSimResolver.iOSSim, device: this });
2928
this.fileSystem = this.$injector.resolve(fileSystemPath.IOSSimulatorFileSystem, { iosSim: this.$iOSSimResolver.iOSSim });

lib/common/mobile/ios/simulator/ios-simulator-log-provider.ts

-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export class IOSSimulatorLogProvider extends EventEmitter implements Mobile.IiOS
88

99
constructor(private $iOSSimResolver: Mobile.IiOSSimResolver,
1010
private $logger: ILogger,
11-
private $processService: IProcessService,
1211
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
1312
private $deviceLogProvider: Mobile.IDeviceLogProvider) {
1413
super();
@@ -47,8 +46,6 @@ export class IOSSimulatorLogProvider extends EventEmitter implements Mobile.IiOS
4746
deviceLogChildProcess.stderr.on("data", action.bind(this));
4847
}
4948

50-
this.$processService.attachToProcessExitSignals(this, deviceLogChildProcess.kill);
51-
5249
this.simulatorsLoggingEnabled[deviceId] = true;
5350
this.simulatorsLogProcess[deviceId] = deviceLogChildProcess;
5451
}

lib/common/mobile/mobile-core/android-process-service.ts

+6-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { EOL } from "os";
2-
import * as shelljs from "shelljs";
32
import { DeviceAndroidDebugBridge } from "../android/device-android-debug-bridge";
43
import { TARGET_FRAMEWORK_IDENTIFIERS } from "../../constants";
5-
import { exported, cache } from "../../decorators";
4+
import { exported } from "../../decorators";
65

76
export class AndroidProcessService implements Mobile.IAndroidProcessService {
87
private _devicesAdbs: IDictionary<Mobile.IDeviceAndroidDebugBridge>;
@@ -11,7 +10,8 @@ export class AndroidProcessService implements Mobile.IAndroidProcessService {
1110
constructor(private $errors: IErrors,
1211
private $injector: IInjector,
1312
private $net: INet,
14-
private $processService: IProcessService) {
13+
private $cleanupService: ICleanupService,
14+
private $staticConfig: IStaticConfig) {
1515
this._devicesAdbs = {};
1616
this._forwardedLocalPorts = {};
1717
}
@@ -22,8 +22,6 @@ export class AndroidProcessService implements Mobile.IAndroidProcessService {
2222
}
2323

2424
public async mapAbstractToTcpPort(deviceIdentifier: string, appIdentifier: string, framework: string): Promise<string> {
25-
this.tryAttachToProcessExitSignals();
26-
2725
const adb = await this.setupForPortForwarding({ deviceIdentifier, appIdentifier });
2826

2927
const processId = (await this.getProcessIds(adb, [appIdentifier]))[appIdentifier];
@@ -120,16 +118,16 @@ export class AndroidProcessService implements Mobile.IAndroidProcessService {
120118

121119
if (!localPort) {
122120
localPort = await this.$net.getFreePort();
123-
await adb.executeCommand(["forward", `tcp:${localPort}`, portForwardInputData.abstractPort], {deviceIdentifier: portForwardInputData.deviceIdentifier});
121+
await adb.executeCommand(["forward", `tcp:${localPort}`, portForwardInputData.abstractPort], { deviceIdentifier: portForwardInputData.deviceIdentifier });
124122
}
125123

126124
this._forwardedLocalPorts[portForwardInputData.deviceIdentifier] = localPort;
125+
// TODO: use correct path to adb
126+
await this.$cleanupService.addCleanupAction({ command: await this.$staticConfig.getAdbFilePath(), args: ["-s", portForwardInputData.deviceIdentifier, "forward", "--remove", `tcp:${localPort}`] });
127127
return localPort && +localPort;
128128
}
129129

130130
private async setupForPortForwarding(portForwardInputData?: Mobile.IPortForwardDataBase): Promise<Mobile.IDeviceAndroidDebugBridge> {
131-
this.tryAttachToProcessExitSignals();
132-
133131
const adb = this.getAdb(portForwardInputData.deviceIdentifier);
134132

135133
return adb;
@@ -279,18 +277,6 @@ export class AndroidProcessService implements Mobile.IAndroidProcessService {
279277

280278
return result;
281279
}
282-
283-
@cache()
284-
private tryAttachToProcessExitSignals(): void {
285-
this.$processService.attachToProcessExitSignals(this, () => {
286-
_.each(this._forwardedLocalPorts, (port: number, deviceIdentifier: string) => {
287-
// We need to use shelljs here instead of $adb because listener functions of exit, SIGINT and SIGTERM must only perform synchronous operations.
288-
// The Node.js process will exit immediately after calling the 'exit' event listeners causing any additional work still queued in the event loop to be abandoned.
289-
// See the official documentation for more information and examples - https://nodejs.org/dist/latest-v6.x/docs/api/process.html#process_event_exit.
290-
shelljs.exec(`adb -s ${deviceIdentifier} forward --remove tcp:${port}`);
291-
});
292-
});
293-
}
294280
}
295281

296282
$injector.register("androidProcessService", AndroidProcessService);

lib/common/services/lock-service.ts

+7-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { cache } from "../decorators";
44
import { getHash } from "../helpers";
55

66
export class LockService implements ILockService {
7-
private currentlyLockedFiles: string[] = [];
87

98
@cache()
109
private get defaultLockFilePath(): string {
@@ -28,14 +27,7 @@ export class LockService implements ILockService {
2827

2928
constructor(private $fs: IFileSystem,
3029
private $settingsService: ISettingsService,
31-
private $processService: IProcessService) {
32-
this.$processService.attachToProcessExitSignals(this, () => {
33-
const locksToRemove = _.clone(this.currentlyLockedFiles);
34-
for (const lockToRemove of locksToRemove) {
35-
lockfile.unlockSync(lockToRemove);
36-
this.cleanLock(lockToRemove);
37-
}
38-
});
30+
private $cleanupService: ICleanupService) {
3931
}
4032

4133
public async executeActionWithLock<T>(action: () => Promise<T>, lockFilePath?: string, lockOpts?: ILockOptions): Promise<T> {
@@ -51,29 +43,29 @@ export class LockService implements ILockService {
5143

5244
public async lock(lockFilePath?: string, lockOpts?: ILockOptions): Promise<() => void> {
5345
const { filePath, fileOpts } = this.getLockFileSettings(lockFilePath, lockOpts);
54-
this.currentlyLockedFiles.push(filePath);
46+
await this.$cleanupService.addCleanupDeleteAction(filePath);
5547
this.$fs.writeFile(filePath, "");
5648

5749
try {
5850
const releaseFunc = await lockfile.lock(filePath, fileOpts);
5951
return async () => {
6052
await releaseFunc();
61-
this.cleanLock(filePath);
53+
await this.cleanLock(filePath);
6254
};
6355
} catch (err) {
6456
throw new Error(`Timeout while waiting for lock "${filePath}"`);
6557
}
6658
}
6759

68-
public unlock(lockFilePath?: string): void {
60+
public async unlock(lockFilePath?: string): Promise<void> {
6961
const { filePath } = this.getLockFileSettings(lockFilePath);
7062
lockfile.unlockSync(filePath);
71-
this.cleanLock(filePath);
63+
await this.cleanLock(filePath);
7264
}
7365

74-
private cleanLock(lockPath: string): void {
75-
_.remove(this.currentlyLockedFiles, e => e === lockPath);
66+
private async cleanLock(lockPath: string): Promise<void> {
7667
this.$fs.deleteFile(lockPath);
68+
await this.$cleanupService.removeCleanupDeleteAction(lockPath);
7769
}
7870

7971
private getLockFileSettings(filePath?: string, fileOpts?: ILockOptions): { filePath: string, fileOpts: ILockOptions } {

lib/common/services/process-service.ts

-43
This file was deleted.

lib/common/test/unit-tests/analytics-service.ts

-4
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ function createTestInjector(testScenario: ITestScenario): IInjector {
8989
return testScenario.isInteractive;
9090
};
9191

92-
testInjector.register("processService", {
93-
attachToProcessExitSignals: (context: any, callback: () => void): void => (undefined)
94-
});
95-
9692
testInjector.register("childProcess", {});
9793
testInjector.register("projectDataService", {});
9894
testInjector.register("mobileHelper", {});

lib/common/test/unit-tests/mobile/android/logcat-helper.ts

-5
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ function createTestInjector(): IInjector {
6060
}
6161
});
6262
injector.register("devicePlatformsConstants", { Android: "Android" });
63-
injector.register("processService", {
64-
attachToProcessExitSignals(context: any, callback: () => void): void {
65-
//left blank intentionally because of lint
66-
},
67-
});
6863
injector.register("deviceLogProvider", {
6964
logData(line: string, platform: string, deviceIdentifier: string): void {
7065
//left blank intentionally because of lint

lib/common/test/unit-tests/mobile/devices-service.ts

-3
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,6 @@ function createTestInjector(): IInjector {
160160
testInjector.register("iOSEmulatorServices", IOSEmulatorServices);
161161
testInjector.register("messages", Messages);
162162
testInjector.register("prompter", {});
163-
testInjector.register("processService", {
164-
attachToProcessExitSignals: (context: any, callback: () => Promise<any>) => { /* no implementation required */ }
165-
});
166163

167164
testInjector.register("mobileHelper", {
168165
platformNames: ["ios", "android"],

lib/common/test/unit-tests/mobile/ios-simulator-discovery.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { assert } from "chai";
55
import { DeviceDiscoveryEventNames, CONNECTED_STATUS } from "../../../constants";
66
import { DevicePlatformsConstants } from "../../../mobile/device-platforms-constants";
77
import { ErrorsStub, CommonLoggerStub, HooksServiceStub, LockServiceStub } from "../stubs";
8-
import { FileSystemStub, ChildProcessStub, ProcessServiceStub } from "../../../../../test/stubs";
8+
import { FileSystemStub, ChildProcessStub } from "../../../../../test/stubs";
99

1010
let currentlyRunningSimulators: Mobile.IiSimDevice[];
1111

@@ -41,7 +41,6 @@ function createTestInjector(): IInjector {
4141
injector.register("deviceLogProvider", {});
4242
injector.register("iOSEmulatorServices", {});
4343
injector.register("iOSNotification", {});
44-
injector.register("processService", ProcessServiceStub);
4544
injector.register("options", {});
4645
injector.register("hooksService", HooksServiceStub);
4746
injector.register("logger", CommonLoggerStub);

0 commit comments

Comments
 (0)