feat: 修改项目后自动删除项目工作区相关目录

This commit is contained in:
Ivan Li
2021-06-27 19:35:49 +08:00
parent 7e17de0f15
commit a231a02c28
14 changed files with 201 additions and 24 deletions

View File

@ -3,3 +3,4 @@ export const ROUTE_FETCH = 'fetch';
export const ROUTE_LIST_COMMITS = 'list-commits';
export const QUEUE_LIST_COMMITS = 'list-commits';
export const QUEUE_FETCH = 'repo-fetch';
export const QUEUE_REFRESH_REPO = 'refresh-repo';

View File

@ -7,12 +7,14 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
import { ProjectsModule } from '../projects/projects.module';
import { EXCHANGE_REPO } from './repos.constants';
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
import { CommonsModule } from '../commons/commons.module';
@Module({
imports: [
TypeOrmModule.forFeature([Project]),
ConfigModule,
ProjectsModule,
CommonsModule,
RabbitMQModule.forRootAsync(RabbitMQModule, {
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({

View File

@ -11,13 +11,14 @@ import { Project } from '../projects/project.entity';
import { ListBranchesArgs } from './dtos/list-branches.args';
import { ConfigService } from '@nestjs/config';
import { Commit } from './dtos/log-list.model';
import { Nack, RabbitRPC } from '@golevelup/nestjs-rabbitmq';
import { Nack, RabbitRPC, RabbitSubscribe } from '@golevelup/nestjs-rabbitmq';
import { Pipeline } from '../pipelines/pipeline.entity';
import { InjectPinoLogger, Logger } from 'nestjs-pino';
import { InjectPinoLogger, PinoLogger } from 'nestjs-pino';
import {
EXCHANGE_REPO,
QUEUE_FETCH,
QUEUE_LIST_COMMITS,
QUEUE_REFRESH_REPO,
ROUTE_FETCH,
ROUTE_LIST_COMMITS,
} from './repos.constants';
@ -27,6 +28,12 @@ import {
getSelfInstanceRouteKey,
} from '../commons/utils/rabbit-mq';
import { ApplicationException } from '../commons/exceptions/application.exception';
import {
EXCHANGE_PROJECT_FANOUT,
ROUTE_PROJECT_CHANGE,
} from '../projects/projects.constants';
import { RedisMutexService } from '../commons/redis-mutex/redis-mutex.service';
import { rm } from 'fs/promises';
const DEFAULT_REMOTE_NAME = 'origin';
const INFO_PATH = '@info';
@ -37,7 +44,8 @@ export class ReposService {
private readonly projectRepository: Repository<Project>,
private readonly configService: ConfigService,
@InjectPinoLogger(ReposService.name)
private readonly logger: Logger,
private readonly logger: PinoLogger,
private readonly redisMutexService: RedisMutexService,
) {}
getWorkspaceRoot(project: Project): string {
@ -170,6 +178,9 @@ export class ReposService {
},
})
async fetch(pipeline: Pipeline): Promise<string | null | Nack> {
const unlock = await this.redisMutexService.lock(
`repo-project-${pipeline.projectId}`,
);
try {
const git = await this.getGit(pipeline.project, undefined, {
fetch: false,
@ -179,6 +190,43 @@ export class ReposService {
} catch (error) {
this.logger.error({ error, pipeline }, '[fetch] %s', error?.message);
return new Nack();
} finally {
await unlock();
}
}
@RabbitSubscribe({
exchange: EXCHANGE_PROJECT_FANOUT,
routingKey: ROUTE_PROJECT_CHANGE,
queue: QUEUE_REFRESH_REPO,
queueOptions: {
autoDelete: true,
durable: true,
},
})
async refreshRepo([project]: [Project]) {
this.logger.info({ project }, '[refreshRepo] start');
const unlock = await this.redisMutexService.lock(
`repo-project-${project.id}`,
{
timeout: null,
},
);
try {
const path = join(
this.configService.get<string>('workspaces.root'),
encodeURIComponent(project.name),
);
await rm(path, { recursive: true });
this.logger.info({ project }, '[refreshRepo] success');
} catch (error) {
this.logger.error(
{ project, error },
'[refreshRepo] failed. $s',
error.message,
);
} finally {
await unlock();
}
}
}