refactor: 重构部署功能
This commit is contained in:
@@ -27,6 +27,6 @@ async function initializeApp() {
|
||||
|
||||
// 启动应用
|
||||
initializeApp().catch((error) => {
|
||||
console.error('Failed to start application:', error);
|
||||
log.error('APP', 'Failed to start application:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import type { Context } from 'koa';
|
||||
import { Controller, Get } from '../../decorators/route.ts';
|
||||
import { gitea } from '../../libs/gitea.ts';
|
||||
import { log } from '../../libs/logger.ts';
|
||||
import { prisma } from '../../libs/prisma.ts';
|
||||
import { BusinessError } from '../../middlewares/exception.ts';
|
||||
import { getBranchesQuerySchema, getCommitsQuerySchema } from './dto.ts';
|
||||
|
||||
const TAG = 'Git';
|
||||
|
||||
@Controller('/git')
|
||||
export class GitController {
|
||||
@Get('/commits')
|
||||
@@ -30,7 +33,7 @@ export class GitController {
|
||||
|
||||
// Get access token from session
|
||||
const accessToken = ctx.session?.gitea?.access_token;
|
||||
console.log('Access token present:', !!accessToken);
|
||||
log.debug(TAG, 'Access token present: %s', !!accessToken);
|
||||
|
||||
if (!accessToken) {
|
||||
throw new BusinessError(
|
||||
@@ -44,7 +47,7 @@ export class GitController {
|
||||
const commits = await gitea.getCommits(owner, repo, accessToken, branch);
|
||||
return commits;
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch commits:', error);
|
||||
log.error(TAG, 'Failed to fetch commits:', error);
|
||||
throw new BusinessError('Failed to fetch commits from Gitea', 1005, 500);
|
||||
}
|
||||
}
|
||||
@@ -80,7 +83,7 @@ export class GitController {
|
||||
const branches = await gitea.getBranches(owner, repo, accessToken);
|
||||
return branches;
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch branches:', error);
|
||||
log.error(TAG, 'Failed to fetch branches:', error);
|
||||
throw new BusinessError('Failed to fetch branches from Gitea', 1006, 500);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ export class PipelineController {
|
||||
const templates = await getAvailableTemplates();
|
||||
return templates;
|
||||
} catch (error) {
|
||||
console.error('Failed to get templates:', error);
|
||||
log.error('pipeline', 'Failed to get templates:', error);
|
||||
throw new BusinessError('获取模板失败', 3002, 500);
|
||||
}
|
||||
}
|
||||
@@ -154,7 +154,7 @@ export class PipelineController {
|
||||
log.info('pipeline', 'Created pipeline from template: %s', pipeline.name);
|
||||
return pipeline;
|
||||
} catch (error) {
|
||||
console.error('Failed to create pipeline from template:', error);
|
||||
log.error('pipeline', 'Failed to create pipeline from template:', error);
|
||||
if (error instanceof BusinessError) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
@@ -68,27 +68,21 @@ export class ProjectController {
|
||||
throw new BusinessError('项目不存在', 1002, 404);
|
||||
}
|
||||
|
||||
// 获取工作目录状态信息
|
||||
// 获取工作目录状态信息(不包含目录大小)
|
||||
let workspaceStatus = null;
|
||||
if (project.projectDir) {
|
||||
try {
|
||||
const status = await GitManager.checkWorkspaceStatus(
|
||||
project.projectDir,
|
||||
);
|
||||
let size = 0;
|
||||
let gitInfo = null;
|
||||
|
||||
if (status.exists && !status.isEmpty) {
|
||||
size = await GitManager.getDirectorySize(project.projectDir);
|
||||
}
|
||||
|
||||
if (status.hasGit) {
|
||||
gitInfo = await GitManager.getGitInfo(project.projectDir);
|
||||
}
|
||||
|
||||
workspaceStatus = {
|
||||
...status,
|
||||
size,
|
||||
gitInfo,
|
||||
};
|
||||
} catch (error) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -221,7 +221,7 @@ export class PipelineRunner {
|
||||
const userEnvVars = JSON.parse(deployment.envVars);
|
||||
Object.assign(envVars, userEnvVars);
|
||||
} catch (error) {
|
||||
console.error('解析环境变量失败:', error);
|
||||
log.error(this.TAG, '解析环境变量失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user