refactor: 重构部署功能

This commit is contained in:
2026-01-08 19:50:58 +08:00
parent a067d167e9
commit db2b2af0d3
13 changed files with 79 additions and 97 deletions

View File

@@ -1,6 +1,8 @@
import { PipelineRunner } from '../runners/index.ts';
import { prisma } from './prisma.ts';
import { log } from '../libs/logger.ts';
const TAG = 'Queue';
// 存储正在运行的部署任务
const runningDeployments = new Set<number>();
@@ -40,14 +42,14 @@ export class ExecutionQueue {
* 初始化执行队列,包括恢复未完成的任务
*/
public async initialize(): Promise<void> {
console.log('Initializing execution queue...');
log.info(TAG, 'Initializing execution queue...');
// 恢复未完成的任务
await this.recoverPendingDeployments();
// 启动定时轮询
this.startPolling();
console.log('Execution queue initialized');
log.info(TAG, 'Execution queue initialized');
}
/**
@@ -55,7 +57,7 @@ export class ExecutionQueue {
*/
private async recoverPendingDeployments(): Promise<void> {
try {
console.log('Recovering pending deployments from database...');
log.info(TAG, 'Recovering pending deployments from database...');
// 查询数据库中状态为pending的部署任务
const pendingDeployments = await prisma.deployment.findMany({
@@ -69,16 +71,16 @@ export class ExecutionQueue {
},
});
console.log(`Found ${pendingDeployments.length} pending deployments`);
log.info(TAG, `Found ${pendingDeployments.length} pending deployments`);
// 将这些任务添加到执行队列中
for (const deployment of pendingDeployments) {
await this.addTask(deployment.id, deployment.pipelineId);
}
console.log('Pending deployments recovery completed');
log.info(TAG, 'Pending deployments recovery completed');
} catch (error) {
console.error('Failed to recover pending deployments:', error);
log.error(TAG, 'Failed to recover pending deployments:', error);
}
}
@@ -87,12 +89,12 @@ export class ExecutionQueue {
*/
private startPolling(): void {
if (this.isPolling) {
console.log('Polling is already running');
log.info(TAG, 'Polling is already running');
return;
}
this.isPolling = true;
console.log(`Starting polling with interval ${POLLING_INTERVAL}ms`);
log.info(TAG, `Starting polling with interval ${POLLING_INTERVAL}ms`);
// 立即执行一次检查
this.checkPendingDeployments();
@@ -111,7 +113,7 @@ export class ExecutionQueue {
clearInterval(pollingTimer);
pollingTimer = null;
this.isPolling = false;
console.log('Polling stopped');
log.info(TAG, 'Polling stopped');
}
}
@@ -120,7 +122,7 @@ export class ExecutionQueue {
*/
private async checkPendingDeployments(): Promise<void> {
try {
console.log('Checking for pending deployments in database...');
log.info(TAG, 'Checking for pending deployments in database...');
// 查询数据库中状态为pending的部署任务
const pendingDeployments = await prisma.deployment.findMany({
@@ -134,7 +136,8 @@ export class ExecutionQueue {
},
});
console.log(
log.info(
TAG,
`Found ${pendingDeployments.length} pending deployments in polling`,
);
@@ -142,14 +145,15 @@ export class ExecutionQueue {
for (const deployment of pendingDeployments) {
// 检查是否已经在运行队列中
if (!runningDeployments.has(deployment.id)) {
console.log(
log.info(
TAG,
`Adding deployment ${deployment.id} to queue from polling`,
);
await this.addTask(deployment.id, deployment.pipelineId);
}
}
} catch (error) {
console.error('Failed to check pending deployments:', error);
log.error(TAG, 'Failed to check pending deployments:', error);
}
}
@@ -164,7 +168,7 @@ export class ExecutionQueue {
): Promise<void> {
// 检查是否已经在运行队列中
if (runningDeployments.has(deploymentId)) {
console.log(`Deployment ${deploymentId} is already queued or running`);
log.info(TAG, `Deployment ${deploymentId} is already queued or running`);
return;
}
@@ -194,7 +198,7 @@ export class ExecutionQueue {
// 执行流水线
await this.executePipeline(task.deploymentId, task.pipelineId);
} catch (error) {
console.error('执行流水线失败:', error);
log.error(TAG, '执行流水线失败:', error);
// 这里可以添加更多的错误处理逻辑
} finally {
// 从运行队列中移除
@@ -245,7 +249,7 @@ export class ExecutionQueue {
);
await runner.run(pipelineId);
} catch (error) {
console.error('执行流水线失败:', error);
log.error(TAG, '执行流水线失败:', error);
// 错误处理可以在这里添加,比如更新部署状态为失败
throw error;
}

View File

@@ -1,3 +1,7 @@
import { log } from './logger.ts';
const TAG = 'Gitea';
interface TokenResponse {
access_token: string;
token_type: string;
@@ -43,7 +47,7 @@ class Gitea {
async getToken(code: string) {
const { giteaUrl, clientId, clientSecret, redirectUri } = this.config;
console.log('this.config', this.config);
log.debug(TAG, 'Gitea token request started');
const response = await fetch(`${giteaUrl}/login/oauth/access_token`, {
method: 'POST',
headers: this.getHeaders(),
@@ -56,7 +60,15 @@ class Gitea {
}),
});
if (!response.ok) {
console.log(await response.json());
const payload = await response
.json()
.catch(() => null as unknown);
log.error(
TAG,
'Gitea token request failed: status=%d payload=%o',
response.status,
payload,
);
throw new Error(`Fetch failed: ${response.status}`);
}
return (await response.json()) as TokenResponse;

View File

@@ -1,4 +1,7 @@
import { prisma } from './prisma.ts';
import { log } from './logger.ts';
const TAG = 'PipelineTemplate';
// 默认流水线模板
export interface PipelineTemplate {
@@ -52,7 +55,7 @@ export const DEFAULT_PIPELINE_TEMPLATES: PipelineTemplate[] = [
* 初始化系统默认流水线模板
*/
export async function initializePipelineTemplates(): Promise<void> {
console.log('Initializing pipeline templates...');
log.info(TAG, 'Initializing pipeline templates...');
try {
// 检查是否已经存在模板流水线
@@ -67,7 +70,7 @@ export async function initializePipelineTemplates(): Promise<void> {
// 如果没有现有的模板,则创建默认模板
if (existingTemplates.length === 0) {
console.log('Creating default pipeline templates...');
log.info(TAG, 'Creating default pipeline templates...');
for (const template of DEFAULT_PIPELINE_TEMPLATES) {
// 创建模板流水线使用负数ID表示模板
@@ -97,15 +100,15 @@ export async function initializePipelineTemplates(): Promise<void> {
});
}
console.log(`Created template: ${template.name}`);
log.info(TAG, `Created template: ${template.name}`);
}
} else {
console.log('Pipeline templates already exist, skipping initialization');
log.info(TAG, 'Pipeline templates already exist, skipping initialization');
}
console.log('Pipeline templates initialization completed');
log.info(TAG, 'Pipeline templates initialization completed');
} catch (error) {
console.error('Failed to initialize pipeline templates:', error);
log.error(TAG, 'Failed to initialize pipeline templates:', error);
throw error;
}
}
@@ -136,7 +139,7 @@ export async function getAvailableTemplates(): Promise<
description: template.description || '',
}));
} catch (error) {
console.error('Failed to get pipeline templates:', error);
log.error(TAG, 'Failed to get pipeline templates:', error);
throw error;
}
}
@@ -205,12 +208,10 @@ export async function createPipelineFromTemplate(
});
}
console.log(
`Created pipeline from template ${templateId}: ${newPipeline.name}`,
);
log.info(TAG, `Created pipeline from template ${templateId}: ${newPipeline.name}`);
return newPipeline.id;
} catch (error) {
console.error('Failed to create pipeline from template:', error);
log.error(TAG, 'Failed to create pipeline from template:', error);
throw error;
}
}

View File

@@ -6,6 +6,9 @@ import {
type RouteMetadata,
} from '../decorators/route.ts';
import { createSuccessResponse } from '../middlewares/exception.ts';
import { log } from './logger.ts';
const TAG = 'RouteScanner';
/**
* 控制器类型
@@ -79,7 +82,7 @@ export class RouteScanner {
this.router.patch(fullPath, handler);
break;
default:
console.warn(`未支持的HTTP方法: ${route.method}`);
log.info(TAG, `未支持的HTTP方法: ${route.method}`);
}
});
}