Skip to content

Commit

Permalink
fix: device disconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
oneofthezombies committed Nov 13, 2023
1 parent 0dfb76d commit 59b6744
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@dogu-private/console-host-agent';
import { FindDeviceBySerialQuery, UpdateDeviceRequestBody } from '@dogu-private/console-host-agent/src/http-specs/private-device';
import { DeviceId, DEVICE_DISPLAY_ERROR_MAX_LENGTH, findDeviceModelNameByModelId, LiveSessionState, OrganizationId } from '@dogu-private/types';
import { Instance, transformAndValidate } from '@dogu-tech/common';
import { errorify, Instance, transformAndValidate } from '@dogu-tech/common';
import { Body, ConflictException, Controller, Get, NotFoundException, Param, Patch, Post, Query } from '@nestjs/common';
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
import { DataSource, Not, Repository } from 'typeorm';
Expand All @@ -36,6 +36,7 @@ export class PrivateDeviceController {
private readonly influxDbDeviceService: InfluxDbDeviceService,
private readonly logger: DoguLogger,
private readonly deviceStatusService: DeviceStatusService,
private readonly liveSessionService: LiveSessionService,
) {}

@Get(PrivateDevice.findDeviceBySerial.path)
Expand Down Expand Up @@ -117,6 +118,8 @@ export class PrivateDeviceController {
deviceId,
});
}

const closedLiveSessions: LiveSession[] = [];
const { serial, hostId, version, model, manufacturer, isVirtual, resolutionWidth, resolutionHeight, browserInstallations, memory } = body;
await this.dataSource.transaction(async (manager) => {
await manager.getRepository(Device).update(
Expand All @@ -137,9 +140,20 @@ export class PrivateDeviceController {
await DeviceStatusService.updateDeviceBrowserInstallations(manager, deviceId, browserInstallations);
await DeviceStatusService.updateDeviceRunners(manager, deviceId);
const liveSessions = await manager.getRepository(LiveSession).find({ where: { deviceId, state: Not(LiveSessionState.CLOSED) } });
const toCloseLiveSessions = liveSessions.map((liveSession) => LiveSessionService.updateLiveSessionToClosed(liveSession));
await manager.getRepository(LiveSession).save(toCloseLiveSessions);
if (liveSessions.length > 0) {
const toCloseLiveSessions = liveSessions.map((liveSession) => LiveSessionService.updateLiveSessionToClosed(liveSession));
const savedLiveSessions = await manager.getRepository(LiveSession).save(toCloseLiveSessions);
closedLiveSessions.push(...savedLiveSessions);
}
});

for (const closedSession of closedLiveSessions) {
try {
await this.liveSessionService.publishCloseEvent(closedSession.liveSessionId, 'closed!');
} catch (error) {
this.logger.error(`PrivateDeviceController.updateDevice. publishCloseEvent failed`, { closedSession, error: errorify(error) });
}
}
}

@Patch(PrivateDevice.updateDeviceHeartbeatNow.path)
Expand Down
11 changes: 10 additions & 1 deletion projects/console-web-server/src/module/private/private.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RoutineStep } from '../../db/entity/step.entity';
import { DeviceMessageModule } from '../device-message/device-message.module';
import { EventModule } from '../event/event.module';
import { InfluxDbModule } from '../influxdb/influxdb.module';
import { LiveSessionModule } from '../live-session/live-session.module';
import { DeviceModule } from '../organization/device/device.module';
import { PipelineModule } from '../routine/pipeline/pipeline.module';
import { PrivateDeviceJobController } from './private-device-job-controller';
Expand All @@ -17,7 +18,15 @@ import { PrivateHostController } from './private-host.controller';
import { PrivateStepController } from './private-step-controller';

@Module({
imports: [TypeOrmModule.forFeature([Organization, Device, Host, RoutineStep, RoutineDeviceJob]), DeviceModule, DeviceMessageModule, InfluxDbModule, PipelineModule, EventModule],
imports: [
TypeOrmModule.forFeature([Organization, Device, Host, RoutineStep, RoutineDeviceJob]),
DeviceModule,
DeviceMessageModule,
InfluxDbModule,
PipelineModule,
EventModule,
LiveSessionModule,
],
controllers: [PrivateHostTokenController, PrivateHostController, PrivateDeviceController, PrivateDeviceJobController, PrivateStepController],
})
export class PrivateModule {}

0 comments on commit 59b6744

Please sign in to comment.