Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into feature/cloud-device-studio
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikalli committed Oct 13, 2023
2 parents 249af0c + 7f554f0 commit e63ad3f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DefaultHttpOptions, errorify, Instance, isFilteredAxiosError, transform
import { HostPaths } from '@dogu-tech/node';
import { Injectable } from '@nestjs/common';
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
import AsyncLock from 'async-lock';
import fs from 'fs';
import { ConsoleClientService } from '../console-client/console-client.service';
import { env } from '../env';
Expand All @@ -15,6 +16,7 @@ import { OnDeviceConnectedEvent, OnDeviceResolvedEvent } from './device.events';
@Injectable()
export class DeviceResolver {
private hostResolutionInfo: HostResolutionInfo | null = null;
private readonly creationMutex = new AsyncLock();

constructor(private readonly consoleClientService: ConsoleClientService, private readonly eventEmitter: EventEmitter2, private readonly logger: DoguLogger) {}

Expand All @@ -35,14 +37,17 @@ export class DeviceResolver {
}

const { serial, serialUnique, model, platform, organizationId, isVirtual, memory } = value;
let deviceId = await this.findDeviceId(organizationId, serialUnique);
if (deviceId === null) {
deviceId = await this.createDevice(organizationId, serial, serialUnique, model, platform, isVirtual, memory);
this.logger.info('Device created', {
serial,
deviceId,
});
}
const deviceId = await this.creationMutex.acquire(`${organizationId}:${serialUnique}`, async () => {
let deviceId = await this.findDeviceId(organizationId, serialUnique);
if (deviceId === null) {
deviceId = await this.createDevice(organizationId, serial, serialUnique, model, platform, isVirtual, memory);
this.logger.info('Device created', {
serial,
deviceId,
});
}
return deviceId;
});

const { rootWorkspace, recordWorkspacePath, hostWorkspacePath, pathMap } = this.hostResolutionInfo;
const hostPlatform = this.hostResolutionInfo.platform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class LiveSessionUpdater implements OnModuleInit, OnModuleDestroy {
});

if (toCloses.length > 0) {
await this.liveSessionService.closeInTran(manager, toCloses);
await this.liveSessionService.closeInTransaction(manager, toCloses);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Module } from '@nestjs/common';
import { DeviceModule } from '../organization/device/device.module';
import { LiveSessionHeartbeatGateway } from './live-session-heartbeat.gateway';
import { LiveSessionController } from './live-session.controller';
import { LiveSessionService } from './live-session.service';

@Module({
imports: [DeviceModule],
controllers: [LiveSessionController],
providers: [LiveSessionHeartbeatGateway, LiveSessionService],
exports: [LiveSessionService],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Device } from '../../db/entity/device.entity';
import { LiveSession } from '../../db/entity/live-session.entity';
import { Organization } from '../../db/entity/organization.entity';
import { DoguLogger } from '../logger/logger';
import { DeviceCommandService } from '../organization/device/device-command.service';

@Injectable()
export class LiveSessionService {
Expand All @@ -21,6 +22,7 @@ export class LiveSessionService {
private readonly dataSource: DataSource,
@InjectRedis()
private readonly redis: Redis,
private readonly deviceCommandService: DeviceCommandService,
private readonly logger: DoguLogger,
) {
this.subscriber = redis.duplicate();
Expand Down Expand Up @@ -124,7 +126,7 @@ export class LiveSessionService {
/**
* @description do NOT access this.dataSource in this method
*/
async closeInTran(manager: EntityManager, liveSessions: LiveSession[]): Promise<LiveSession[]> {
async closeInTransaction(manager: EntityManager, liveSessions: LiveSession[]): Promise<LiveSession[]> {
liveSessions.forEach((liveSession) => {
liveSession.state = LiveSessionState.CLOSED;
liveSession.closedAt = new Date();
Expand All @@ -146,13 +148,17 @@ export class LiveSessionService {
},
});
devices.forEach((device) => {
device.usageState = DeviceUsageState.AVAILABLE;
device.usageState = DeviceUsageState.PREPARING;
});
await manager.save(devices);
this.logger.debug('LiveSessionService.close.devices', {
devices,
});

devices.forEach((device) => {
this.deviceCommandService.reboot(device.organizationId, device.deviceId, device.serial);
});

return closeds;
}

Expand All @@ -167,7 +173,7 @@ export class LiveSessionService {
return liveSession;
}

const closeds = await this.closeInTran(manager, [liveSession]);
const closeds = await this.closeInTransaction(manager, [liveSession]);
if (closeds.length !== 1) {
throw new InternalServerErrorException(`LiveSession close failed for liveSessionId: ${liveSessionId}`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DevicePropCamel, OrganizationPropCamel } from '@dogu-private/console';
import { DevicePropCamel, DeviceUsageState, OrganizationPropCamel } from '@dogu-private/console';
import {
CreateDeviceRequestBody,
PrivateDevice,
Expand Down Expand Up @@ -118,7 +118,21 @@ export class PrivateDeviceController {
}
const { serial, hostId, version, model, manufacturer, isVirtual, resolutionWidth, resolutionHeight, browserInstallations, memory } = body;
await this.dataSource.transaction(async (manager) => {
await manager.getRepository(Device).update({ deviceId }, { serial, hostId, version, model, manufacturer, isVirtual, resolutionWidth, resolutionHeight, memory });
await manager.getRepository(Device).update(
{ deviceId },
{
serial,
hostId,
version,
model,
manufacturer,
isVirtual,
resolutionWidth,
resolutionHeight,
memory,
usageState: DeviceUsageState.AVAILABLE,
},
);
await DeviceStatusService.updateDeviceBrowserInstallations(manager, deviceId, browserInstallations);
await DeviceStatusService.updateDeviceRunners(manager, deviceId);
});
Expand Down

0 comments on commit e63ad3f

Please sign in to comment.