@phystack/device-phyos 6.15.1 → 6.15.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +65 -40
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -71,7 +71,7 @@ function getSystemdJournalGid() {
|
|
|
71
71
|
try {
|
|
72
72
|
const groupFile = fs_1.default.readFileSync('/etc/group', 'utf8');
|
|
73
73
|
const groups = groupFile.split('\n');
|
|
74
|
-
const journalGroup = groups.find(line => line.startsWith('systemd-journal:'));
|
|
74
|
+
const journalGroup = groups.find((line) => line.startsWith('systemd-journal:'));
|
|
75
75
|
if (!journalGroup) {
|
|
76
76
|
throw new Error('systemd-journal group not found');
|
|
77
77
|
}
|
|
@@ -166,6 +166,7 @@ class DevicePhyos {
|
|
|
166
166
|
GRID_ENV;
|
|
167
167
|
PHYOS_VERSION;
|
|
168
168
|
firstConnect;
|
|
169
|
+
orphanSweepInFlight = false;
|
|
169
170
|
phyHubInterval;
|
|
170
171
|
logger;
|
|
171
172
|
scheduler = new scheduler_service_1.Scheduler();
|
|
@@ -187,12 +188,12 @@ class DevicePhyos {
|
|
|
187
188
|
await this.initializeTimeSync();
|
|
188
189
|
this.logger.info(`Starting device phyos service 🟢-⚪-⚪-⚪-⚪`, {
|
|
189
190
|
GRID_ENV: this.GRID_ENV,
|
|
190
|
-
PHYOS_VERSION: this.PHYOS_VERSION
|
|
191
|
+
PHYOS_VERSION: this.PHYOS_VERSION,
|
|
191
192
|
});
|
|
192
193
|
}
|
|
193
194
|
async initializeEnvironment() {
|
|
194
195
|
try {
|
|
195
|
-
this.GRID_ENV = await fs_1.default.promises.readFile(phyosEnvPath, 'utf8').then(data => data.trim() || 'LOCAL');
|
|
196
|
+
this.GRID_ENV = await fs_1.default.promises.readFile(phyosEnvPath, 'utf8').then((data) => data.trim() || 'LOCAL');
|
|
196
197
|
this.logger.info('initializeEnvironment(): Setting grid environment', {
|
|
197
198
|
GRID_ENV: this.GRID_ENV,
|
|
198
199
|
});
|
|
@@ -202,9 +203,9 @@ class DevicePhyos {
|
|
|
202
203
|
this.GRID_ENV = 'PROD';
|
|
203
204
|
}
|
|
204
205
|
try {
|
|
205
|
-
this.PHYOS_VERSION = await fs_1.default.promises.readFile(phyosVersionPath, 'utf8').then(data => data.trim());
|
|
206
|
+
this.PHYOS_VERSION = await fs_1.default.promises.readFile(phyosVersionPath, 'utf8').then((data) => data.trim());
|
|
206
207
|
this.logger.info('initializeEnvironment(): Setting PhyOS version', {
|
|
207
|
-
PHYOS_VERSION: this.PHYOS_VERSION
|
|
208
|
+
PHYOS_VERSION: this.PHYOS_VERSION,
|
|
208
209
|
});
|
|
209
210
|
}
|
|
210
211
|
catch (error) {
|
|
@@ -247,6 +248,21 @@ class DevicePhyos {
|
|
|
247
248
|
this.logger.info('initializeCachedInstances(): No valid twins to initialize');
|
|
248
249
|
}
|
|
249
250
|
}
|
|
251
|
+
async sweepOrphanedContainers() {
|
|
252
|
+
if (this.orphanSweepInFlight) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
this.orphanSweepInFlight = true;
|
|
256
|
+
try {
|
|
257
|
+
await instances_1.dockerManager.sweepOrphanedContainers(async () => {
|
|
258
|
+
const twinsByType = (await this.twinManager.getCachedTwins());
|
|
259
|
+
return Object.values(twinsByType).flat();
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
finally {
|
|
263
|
+
this.orphanSweepInFlight = false;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
250
266
|
async connectPhyHub(hubDevice) {
|
|
251
267
|
this.twinManager = await (0, twin_manager_1.getTwinManagerInstance)();
|
|
252
268
|
this.twinManager.setHubDevice(hubDevice);
|
|
@@ -256,10 +272,14 @@ class DevicePhyos {
|
|
|
256
272
|
this.logger.info('connectPhyHub(): Device authenticated, resubscribing twins...');
|
|
257
273
|
await edge_hub_1.EdgeHubServer.getInstance().resubscribeAllTwins();
|
|
258
274
|
try {
|
|
259
|
-
const cachedTwinsInfo = await this.twinManager.getCachedTwins();
|
|
260
|
-
if (cachedTwinsInfo &&
|
|
275
|
+
const cachedTwinsInfo = (await this.twinManager.getCachedTwins());
|
|
276
|
+
if (cachedTwinsInfo &&
|
|
277
|
+
typeof cachedTwinsInfo === 'object' &&
|
|
278
|
+
'Device' in cachedTwinsInfo &&
|
|
279
|
+
Array.isArray(cachedTwinsInfo.Device) &&
|
|
280
|
+
cachedTwinsInfo.Device.length > 0) {
|
|
261
281
|
const deviceTwinId = cachedTwinsInfo.Device[0];
|
|
262
|
-
const deviceTwin = await this.twinManager.getCachedTwins(deviceTwinId);
|
|
282
|
+
const deviceTwin = (await this.twinManager.getCachedTwins(deviceTwinId));
|
|
263
283
|
if (deviceTwin && deviceTwin.type === 'Device' && 'properties' in deviceTwin && deviceTwin.properties) {
|
|
264
284
|
const deviceProps = deviceTwin.properties;
|
|
265
285
|
const jobs = deviceProps.reported?.jobs || [];
|
|
@@ -275,6 +295,33 @@ class DevicePhyos {
|
|
|
275
295
|
});
|
|
276
296
|
await hubDevice.connect();
|
|
277
297
|
this.logger.info('connectPhyHub(): Connected to PhyHub');
|
|
298
|
+
hubDevice.setTwinUpdateHandler(async (twin) => {
|
|
299
|
+
this.logger.info('connectPhyHub() on setTwinUpdateHandler(): Twin update received, updating instance...');
|
|
300
|
+
await this.twinManager.cacheHubTwin(twin);
|
|
301
|
+
instances_1.dockerManager.markTwinAlive(twin.id);
|
|
302
|
+
if (twin.type === 'Device' && twin.properties?.desired?.timezone) {
|
|
303
|
+
try {
|
|
304
|
+
const region = JSON.parse(fs_1.default.readFileSync(regionFilePath, 'utf8'));
|
|
305
|
+
const proxyConfig = {
|
|
306
|
+
region,
|
|
307
|
+
port: 443,
|
|
308
|
+
username: twin.deviceId,
|
|
309
|
+
password: 'device',
|
|
310
|
+
timezone: twin.properties.desired.timezone,
|
|
311
|
+
};
|
|
312
|
+
this.logger.info('connectPhyHub(): Updating environment with:', proxyConfig);
|
|
313
|
+
await (0, environment_1.setTZinEnvFile)(twin.properties.desired.timezone);
|
|
314
|
+
}
|
|
315
|
+
catch (error) {
|
|
316
|
+
this.logger.error('connectPhyHub(): Failed to update environment with new timezone:', error);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
await (0, instances_1.updateInstances)(this.twinManager, [(await this.twinManager.getCachedTwins(twin.id))]);
|
|
320
|
+
});
|
|
321
|
+
hubDevice.setTwinRemoveHandler(async (twin) => {
|
|
322
|
+
this.logger.info('connectPhyHub() on setTwinRemoveHandler(): Twin remove event received, removing instance twin...', twin);
|
|
323
|
+
await (0, instances_1.removeInstances)(this.twinManager, [twin]);
|
|
324
|
+
});
|
|
278
325
|
hubDevice.connectDevice(async (response) => {
|
|
279
326
|
const { twins } = response;
|
|
280
327
|
console.log('connectPhyHub(): response', response);
|
|
@@ -307,7 +354,7 @@ class DevicePhyos {
|
|
|
307
354
|
port: 443,
|
|
308
355
|
username: deviceId,
|
|
309
356
|
password: 'device',
|
|
310
|
-
timezone: deviceTwin?.properties?.desired?.timezone || 'Etc/UTC'
|
|
357
|
+
timezone: deviceTwin?.properties?.desired?.timezone || 'Etc/UTC',
|
|
311
358
|
};
|
|
312
359
|
this.logger.info('connectPhyHub(): Environment contains unprovisioned username, configuring with device credentials:', proxyConfig);
|
|
313
360
|
await (0, environment_1.setEnvironmentConfiguration)(proxyConfig);
|
|
@@ -327,45 +374,23 @@ class DevicePhyos {
|
|
|
327
374
|
if (twins?.length) {
|
|
328
375
|
this.logger.info('connectPhyHub(): Caching twins from server response...');
|
|
329
376
|
await Promise.all(twins.map((twin) => this.twinManager.cacheHubTwin(twin)));
|
|
377
|
+
for (const twin of twins) {
|
|
378
|
+
instances_1.dockerManager.markTwinAlive(twin.id);
|
|
379
|
+
}
|
|
330
380
|
this.logger.info(`connectPhyHub(): Cached ${twins.length} twins from hub`);
|
|
331
|
-
const twinsByType = await this.twinManager.getCachedTwins();
|
|
381
|
+
const twinsByType = (await this.twinManager.getCachedTwins());
|
|
332
382
|
const allTwinIds = Object.values(twinsByType).flat();
|
|
333
383
|
this.logger.info(`connectPhyHub(): Found ${allTwinIds.length} total twins after caching`);
|
|
334
|
-
const allTwins = await Promise.all(allTwinIds.map(id => this.twinManager.getCachedTwins(id)));
|
|
384
|
+
const allTwins = await Promise.all(allTwinIds.map((id) => this.twinManager.getCachedTwins(id)));
|
|
335
385
|
console.log(`connectPhyHub(): allTwins ${allTwins.length}`);
|
|
336
386
|
await (0, instances_1.initInstances)(this.twinManager, allTwins);
|
|
337
387
|
await (0, methods_1.initMethods)(hubDevice);
|
|
388
|
+
this.sweepOrphanedContainers().catch((error) => {
|
|
389
|
+
this.logger.error('connectPhyHub(): Failed to sweep orphaned containers', error);
|
|
390
|
+
});
|
|
338
391
|
}
|
|
339
392
|
});
|
|
340
393
|
await this.initializeCachedInstances(this.twinManager);
|
|
341
|
-
hubDevice.setTwinUpdateHandler(async (twin) => {
|
|
342
|
-
this.logger.info('connectPhyHub() on setTwinUpdateHandler(): Twin update received, updating instance...');
|
|
343
|
-
await this.twinManager.cacheHubTwin(twin);
|
|
344
|
-
if (twin.type === 'Device' && twin.properties?.desired?.timezone) {
|
|
345
|
-
try {
|
|
346
|
-
const region = JSON.parse(fs_1.default.readFileSync(regionFilePath, 'utf8'));
|
|
347
|
-
const proxyConfig = {
|
|
348
|
-
region,
|
|
349
|
-
port: 443,
|
|
350
|
-
username: twin.deviceId,
|
|
351
|
-
password: 'device',
|
|
352
|
-
timezone: twin.properties.desired.timezone
|
|
353
|
-
};
|
|
354
|
-
this.logger.info('connectPhyHub(): Updating environment with:', proxyConfig);
|
|
355
|
-
await (0, environment_1.setTZinEnvFile)(twin.properties.desired.timezone);
|
|
356
|
-
}
|
|
357
|
-
catch (error) {
|
|
358
|
-
this.logger.error('connectPhyHub(): Failed to update environment with new timezone:', error);
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
await (0, instances_1.updateInstances)(this.twinManager, [
|
|
362
|
-
(await this.twinManager.getCachedTwins(twin.id)),
|
|
363
|
-
]);
|
|
364
|
-
});
|
|
365
|
-
hubDevice.setTwinRemoveHandler(async (twin) => {
|
|
366
|
-
this.logger.info('connectPhyHub() on setTwinRemoveHandler(): Twin remove event received, removing instance twin...', twin);
|
|
367
|
-
await (0, instances_1.removeInstances)(this.twinManager, [twin]);
|
|
368
|
-
});
|
|
369
394
|
hubDevice.on('setCACertificate', async (payload, callback) => {
|
|
370
395
|
try {
|
|
371
396
|
const config = payload.data;
|
|
@@ -444,7 +469,7 @@ class DevicePhyos {
|
|
|
444
469
|
this.scheduler = new scheduler_service_1.Scheduler();
|
|
445
470
|
this.logger.info('start(): Created new scheduler instance');
|
|
446
471
|
}
|
|
447
|
-
(0, edge_hub_1.startEdgeHub)().catch(error => {
|
|
472
|
+
(0, edge_hub_1.startEdgeHub)().catch((error) => {
|
|
448
473
|
this.logger.error('start(): Error starting edgeHub messaging', error);
|
|
449
474
|
});
|
|
450
475
|
this.phyHubInterval = setInterval(() => this.getPhyHub(), 30000);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@phystack/device-phyos",
|
|
3
|
-
"version": "6.15.
|
|
3
|
+
"version": "6.15.3",
|
|
4
4
|
"description": "PhyOS device agent",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"format": "prettier --write \"src/**/*.{ts,js,json,md}\""
|
|
26
26
|
},
|
|
27
27
|
"optionalDependencies": {
|
|
28
|
-
"@phystack/phydevice-darwin-arm64": "6.15.
|
|
29
|
-
"@phystack/phydevice-darwin-x64": "6.15.
|
|
30
|
-
"@phystack/phydevice-linux-arm64": "6.15.
|
|
31
|
-
"@phystack/phydevice-linux-x64": "6.15.
|
|
28
|
+
"@phystack/phydevice-darwin-arm64": "6.15.3",
|
|
29
|
+
"@phystack/phydevice-darwin-x64": "6.15.3",
|
|
30
|
+
"@phystack/phydevice-linux-arm64": "6.15.3",
|
|
31
|
+
"@phystack/phydevice-linux-x64": "6.15.3"
|
|
32
32
|
}
|
|
33
33
|
}
|