mirror of
https://github.com/dadosfera/maestro.git
synced 2026-11-10 23:39:08 +00:00
66 lines
2.1 KiB
TypeScript
66 lines
2.1 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
CanActivate,
|
|
ExecutionContext,
|
|
Inject,
|
|
Injectable,
|
|
OnModuleInit,
|
|
} from '@nestjs/common';
|
|
import { ClientGrpc } from '@nestjs/microservices';
|
|
import { map, Observable } from 'rxjs';
|
|
import { PackTheMetadata } from 'src/utils/PackTheMetadata';
|
|
import {
|
|
ReadService,
|
|
ProtoServices,
|
|
} from '@dadosfera/protospack-v2/dist/lib/PipelineV2';
|
|
import { PipelinesClientConfiguration } from 'src/modules/pipelinesV2/pipelines-client';
|
|
import { PlatformApiService } from 'src/modules/platform-api/platform-api.service';
|
|
import DadosferaLogger from '@dadosfera/dadosfera-logs';
|
|
|
|
@Injectable()
|
|
export class PipelineExecutionGuard implements CanActivate {
|
|
logger: DadosferaLogger;
|
|
|
|
constructor(
|
|
@Inject(DadosferaLogger)
|
|
dadosferaLogger: DadosferaLogger,
|
|
private readonly platformApiService: PlatformApiService,
|
|
) {
|
|
this.logger = dadosferaLogger.logger;
|
|
}
|
|
|
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
try {
|
|
this.logger.info(
|
|
'PipelineExecutionGuard: Checking if pipeline can be executed...',
|
|
);
|
|
const request = context.switchToHttp().getRequest();
|
|
const pipelineId = request.params.pipelineId;
|
|
const user = request.user;
|
|
const idRegex = /[^0-9a-zA-Z_$]+/g;
|
|
const convertedId = pipelineId.replace(idRegex, '_');
|
|
|
|
const status = await this.platformApiService.proxy(
|
|
'GET',
|
|
`/pipeline/${convertedId}/pipeline_run`,
|
|
user,
|
|
);
|
|
|
|
const currentStatus = status[status.length - 1]
|
|
|
|
this.logger.info('Pipeline current status response:' + JSON.stringify(currentStatus));
|
|
|
|
if (currentStatus.last_status.toLowerCase() === 'running') {
|
|
this.logger.error('Pipeline is running, cannot update input now');
|
|
throw new BadRequestException('Pipeline is running, cannot update input now');
|
|
} else {
|
|
return true;
|
|
}
|
|
} catch (error) {
|
|
this.logger.error('Error in PipelineExecutionGuard: ' + error.message);
|
|
throw new BadRequestException('Error checking pipeline status: ' + error.message);
|
|
}
|
|
|
|
}
|
|
}
|