-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathanalytics-broker-process.ts
100 lines (78 loc) · 3.92 KB
/
analytics-broker-process.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// NOTE: This file is used to track data in a separate process.
// The instances here are not shared with the ones in main CLI process.
import * as fs from "fs";
import { AnalyticsBroker } from "./analytics-broker";
import { FileLogService } from "../../detached-processes/file-log-service";
const pathToBootstrap = process.argv[2];
if (!pathToBootstrap || !fs.existsSync(pathToBootstrap)) {
throw new Error("Invalid path to bootstrap.");
}
const logFile = process.argv[3];
// After requiring the bootstrap we can use $injector
require(pathToBootstrap);
const analyticsLoggingService = $injector.resolve<IFileLogService>(FileLogService, { logFile });
analyticsLoggingService.logData({ message: "Initializing AnalyticsBroker." });
const analyticsBroker = $injector.resolve<IAnalyticsBroker>(AnalyticsBroker, { pathToBootstrap, analyticsLoggingService });
let trackingQueue: Promise<void> = Promise.resolve();
const sendDataForTracking = async (data: ITrackingInformation) => {
trackingQueue = trackingQueue.then(() => analyticsBroker.sendDataForTracking(data));
await trackingQueue;
};
const finishTracking = async (data?: ITrackingInformation) => {
analyticsLoggingService.logData({ message: `analytics-broker-process finish tracking started` });
await trackingQueue;
analyticsLoggingService.logData({ message: `analytics-broker-process tracking finished` });
};
const killCurrentProcessGracefully = () => {
$injector.dispose();
process.exit();
};
const trackPreviewAppData = async (data: any) => {
const mobileHelper = $injector.resolve<Mobile.IMobileHelper>("mobileHelper");
const devicesService = $injector.resolve<Mobile.IDevicesService>("devicesService");
await devicesService.initialize({ platform: data.platform, skipDeviceDetectionInterval: true, skipEmulatorStart: true });
const devices = await devicesService.getDevicesForPlatform(data.platform);
_.each(devices, async (device: Mobile.IDevice) => {
try {
let previewAppFilePath = null;
if (mobileHelper.isAndroidPlatform(device.deviceInfo.platform)) {
previewAppFilePath = "/sdcard/org.nativescript.preview/device.json";
} else if (mobileHelper.isiOSPlatform(device.deviceInfo.platform)) {
previewAppFilePath = "Documents/device.json";
}
const previewAppFileContent = await device.fileSystem.getFileContent(previewAppFilePath, "org.nativescript.preview");
const previewAppDeviceId = JSON.parse(previewAppFileContent).id;
data.label += `_${previewAppDeviceId}`;
analyticsLoggingService.logData({ message: `analytics-broker-process will send the data from preview app: ${data}` });
await sendDataForTracking(data);
} catch (err) {
// ignore the error
}
});
};
process.on("message", async (data: ITrackingInformation) => {
analyticsLoggingService.logData({ message: `analytics-broker-process received message of type: ${JSON.stringify(data)}` });
if (data.type === TrackingTypes.PreviewAppData) {
await trackPreviewAppData(<IPreviewAppTrackingInformation>data);
return;
}
if (data.type === TrackingTypes.FinishTracking) {
await finishTracking();
if (process.connected) {
analyticsLoggingService.logData({ message: `analytics-broker-process will send ${DetachedProcessMessages.ProcessFinishedTasks} message` });
process.send(DetachedProcessMessages.ProcessFinishedTasks, () => {
analyticsLoggingService.logData({ message: `analytics-broker-process sent ${DetachedProcessMessages.ProcessFinishedTasks} message and will exit gracefully now` });
killCurrentProcessGracefully();
});
}
return;
}
await sendDataForTracking(data);
});
process.on("disconnect", async () => {
analyticsLoggingService.logData({ message: "analytics-broker-process received process.disconnect event" });
await finishTracking();
killCurrentProcessGracefully();
});
analyticsLoggingService.logData({ message: `analytics-broker-process will send ${DetachedProcessMessages.ProcessReadyToReceive} message` });
process.send(DetachedProcessMessages.ProcessReadyToReceive);