feat(settings): otomatik docker image temizliği ekle

Docker image temizliği için yapılandırılabilir zamanlayıcı ve manuel
tetikleme özelliği eklenmiştir. Kullanıcılar saat, gün veya hafta bazlı
periyotlar belirleyebilir ve anlık temizlik yapabilir.
This commit is contained in:
2026-02-03 09:34:37 +00:00
parent b04ac03739
commit 1f90ce54d4
6 changed files with 198 additions and 9 deletions

View File

@@ -126,6 +126,7 @@ async function start() {
try {
await mongoose.connect(config.mongoUri);
console.log("MongoDB'ye bağlanıldı");
await deploymentService.ensureSettings();
await jobService.bootstrapFromFilesystem();
await jobService.bootstrap();
await deploymentService.normalizeExistingCommitMessages();

View File

@@ -3,6 +3,8 @@ import mongoose, { Schema, Document } from "mongoose";
export interface SettingsDocument extends Document {
webhookToken: string;
webhookSecret: string;
cleanupIntervalValue?: number;
cleanupIntervalUnit?: "saat" | "gün" | "hafta";
createdAt: Date;
updatedAt: Date;
}
@@ -10,7 +12,9 @@ export interface SettingsDocument extends Document {
const SettingsSchema = new Schema<SettingsDocument>(
{
webhookToken: { type: String, required: true },
webhookSecret: { type: String, required: true }
webhookSecret: { type: String, required: true },
cleanupIntervalValue: { type: Number, min: 1 },
cleanupIntervalUnit: { type: String, enum: ["saat", "gün", "hafta"] }
},
{ timestamps: true }
);

View File

@@ -11,6 +11,8 @@ router.get("/", async (_req, res) => {
return res.json({
webhookToken: settings.webhookToken,
webhookSecret: settings.webhookSecret,
cleanupIntervalValue: settings.cleanupIntervalValue,
cleanupIntervalUnit: settings.cleanupIntervalUnit,
updatedAt: settings.updatedAt
});
});
@@ -31,4 +33,29 @@ router.post("/secret/rotate", async (_req, res) => {
});
});
router.post("/cleanup-interval", async (req, res) => {
const settings = await deploymentService.ensureSettings();
const { value, unit } = req.body as {
value?: number;
unit?: "saat" | "gün" | "hafta";
};
if (!value || value < 1 || !unit) {
return res.status(400).json({ message: "Geçerli periyot gerekli" });
}
settings.cleanupIntervalValue = value;
settings.cleanupIntervalUnit = unit;
await settings.save();
await deploymentService.updateCleanupSchedule(value, unit);
return res.json({
cleanupIntervalValue: settings.cleanupIntervalValue,
cleanupIntervalUnit: settings.cleanupIntervalUnit,
updatedAt: settings.updatedAt
});
});
router.post("/cleanup-images", async (_req, res) => {
await deploymentService.cleanupUnusedImages();
return res.json({ success: true });
});
export default router;

View File

@@ -41,6 +41,8 @@ type DeploymentMetadata = {
type SettingsMetadata = {
webhookToken: string;
webhookSecret: string;
cleanupIntervalValue?: number;
cleanupIntervalUnit?: "saat" | "gün" | "hafta";
};
type StoredRun = {
@@ -360,6 +362,7 @@ async function runCompose(project: DeploymentProjectDocument, onData: (line: str
class DeploymentService {
private running: Map<string, boolean> = new Map();
private io: Server | null = null;
private cleanupTimer: NodeJS.Timeout | null = null;
setSocket(io: Server) {
this.io = io;
@@ -451,14 +454,23 @@ class DeploymentService {
async ensureSettings() {
const existing = await Settings.findOne();
if (existing) return existing;
if (existing) {
await this.updateCleanupSchedule(existing.cleanupIntervalValue, existing.cleanupIntervalUnit);
return existing;
}
const fileSettings = await readSettingsFile();
if (fileSettings) {
const createdFromFile = await Settings.create({
webhookToken: fileSettings.webhookToken,
webhookSecret: fileSettings.webhookSecret
webhookSecret: fileSettings.webhookSecret,
cleanupIntervalValue: fileSettings.cleanupIntervalValue,
cleanupIntervalUnit: fileSettings.cleanupIntervalUnit
});
await this.updateCleanupSchedule(
createdFromFile.cleanupIntervalValue,
createdFromFile.cleanupIntervalUnit
);
return createdFromFile;
}
@@ -468,8 +480,11 @@ class DeploymentService {
});
await writeSettingsFile({
webhookToken: created.webhookToken,
webhookSecret: created.webhookSecret
webhookSecret: created.webhookSecret,
cleanupIntervalValue: created.cleanupIntervalValue,
cleanupIntervalUnit: created.cleanupIntervalUnit
});
await this.updateCleanupSchedule(created.cleanupIntervalValue, created.cleanupIntervalUnit);
return created;
}
@@ -479,7 +494,9 @@ class DeploymentService {
await settings.save();
await writeSettingsFile({
webhookToken: settings.webhookToken,
webhookSecret: settings.webhookSecret
webhookSecret: settings.webhookSecret,
cleanupIntervalValue: settings.cleanupIntervalValue,
cleanupIntervalUnit: settings.cleanupIntervalUnit
});
return settings;
}
@@ -490,11 +507,35 @@ class DeploymentService {
await settings.save();
await writeSettingsFile({
webhookToken: settings.webhookToken,
webhookSecret: settings.webhookSecret
webhookSecret: settings.webhookSecret,
cleanupIntervalValue: settings.cleanupIntervalValue,
cleanupIntervalUnit: settings.cleanupIntervalUnit
});
return settings;
}
async updateCleanupSchedule(value?: number, unit?: "saat" | "gün" | "hafta") {
if (this.cleanupTimer) {
clearInterval(this.cleanupTimer);
this.cleanupTimer = null;
}
if (!value || !unit) return;
const intervalMs =
unit === "saat"
? value * 60 * 60 * 1000
: unit === "gün"
? value * 24 * 60 * 60 * 1000
: value * 7 * 24 * 60 * 60 * 1000;
if (!intervalMs || Number.isNaN(intervalMs)) return;
this.cleanupTimer = setInterval(() => {
this.cleanupUnusedImages().catch(() => undefined);
}, intervalMs);
}
async cleanupUnusedImages() {
await runCommand("docker image prune -a -f", process.cwd(), () => undefined);
}
async createProject(input: {
name: string;
repoUrl: string;