Skip to content

Commit

Permalink
fix auto resume and suspend service
Browse files Browse the repository at this point in the history
  • Loading branch information
csuvajit committed May 20, 2024
1 parent c31ffbd commit fd77ebe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ export class AppController {
@UseGuards(ApiKeyGuard)
@Delete('/services/:id')
deleteService(@Param('id') serviceId: string) {
return this.appService.stopService(serviceId);
return this.appService.deleteService(serviceId);
}
}
16 changes: 13 additions & 3 deletions src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,27 @@ export class AppService {
return { message: 'OK' };
}

async suspendService(serviceId: string) {
async deleteService(serviceId: string) {
const service = await this.storageService.findById(serviceId);
if (!service) throw new NotFoundException('Service not found');

try {
await this.dockerService.stopContainer(service.containerId);
await this.dockerService.deleteContainer(service.containerId);
} catch (error) {
this.logger.log(`Service [${service.name}] suspend failed`);
this.logger.log(`Service [${service.name}] stop failed`);
this.logger.error(error);
}

await this.storageService.stopService(serviceId);

return { message: 'OK' };
}

async suspendService(serviceId: string) {
const service = await this.storageService.findById(serviceId);
if (!service) throw new NotFoundException('Service not found');

await this.stopService(serviceId);
await this.storageService.suspendService(serviceId);

return { message: 'OK' };
Expand Down
4 changes: 4 additions & 0 deletions src/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export class StorageService {
);
}

async deleteService(serviceId: string) {
return this.collection.deleteOne({ serviceId });
}

async suspendService(serviceId: string) {
return this.collection.findOneAndUpdate(
{ serviceId },
Expand Down

0 comments on commit fd77ebe

Please sign in to comment.