Commit 13962ae6 authored by nanahira's avatar nanahira

add force pack

parent aa40d41a
......@@ -23,7 +23,7 @@ import { FileUploadDto } from './dto/FileUpload.dto';
import Busboy from 'busboy';
import { Request } from 'express';
import { PackageResult } from './dto/PackageResult.dto';
import { DepotDto } from './dto/Depot.dto';
import { DepotDto, MakeBuildDto } from './dto/Depot.dto';
import AppClass = AppsJson.AppClass;
@Controller('api')
......@@ -109,7 +109,7 @@ export class AppController {
async makeBuild(
@FetchMyCardUser() user: MyCardUser,
@Param('id') id: string,
@Query(new ValidationPipe({ transform: true })) depot: DepotDto,
@Query(new ValidationPipe({ transform: true })) depot: MakeBuildDto,
@Param('version') version: string,
@Req() req: Request
) {
......
......@@ -8,7 +8,7 @@ import { MyCardUser } from './utility/mycard-auth';
import { PackagerService } from './packager/packager.service';
import internal from 'stream';
import { Depot } from './entities/Depot.entity';
import { DepotDto } from './dto/Depot.dto';
import { DepotDto, MakeBuildDto } from './dto/Depot.dto';
import { Build } from './entities/Build.entity';
import { ConfigService } from '@nestjs/config';
import { Archive, ArchiveType } from './entities/Archive.entity';
......@@ -184,7 +184,7 @@ export class AppService extends ConsoleLogger {
private buildLock = new BetterLock();
async makeBuild(user: MyCardUser, stream: NodeJS.ReadableStream, id: string, depotDto: DepotDto, version: string) {
async makeBuild(user: MyCardUser, stream: NodeJS.ReadableStream, id: string, depotDto: MakeBuildDto, version: string) {
if (!user) {
throw new BlankReturnMessageDto(401, 'Needs login').toException();
}
......@@ -214,7 +214,7 @@ export class AppService extends ConsoleLogger {
.getRepository(Build)
.find({ where: { depot }, order: { id: 'DESC' }, select: ['id', 'checksum'], take: this.packageVersionTraceCount })
).map((b) => b.checksum);
const result = await this.packager.build(stream, app.packagePrefix, previousTracingBuildChecksums);
const result = await this.packager.build(stream, app.packagePrefix, previousTracingBuildChecksums, !!depotDto.force);
this.log(`Saving package info of ${app.id} ${version} for ${JSON.stringify(depot)}`);
build.checksum = result.checksum;
//build.archives = result.archives;
......
......@@ -44,3 +44,8 @@ export class DepotDto implements DepotLike {
});
}
}
export class MakeBuildDto extends DepotDto {
@ApiProperty({ description: '是否强制重新打包', required: false })
force?: boolean;
}
......@@ -28,7 +28,12 @@ const ARG_SIZE_LIMIT = 30000;
class ArchiveTask {
readonly path: string;
constructor(public readonly role: ArchiveType, public readonly files: FileWithHash[], public readonly altFiles?: string[]) {
constructor(
public readonly role: ArchiveType,
public readonly files: FileWithHash[],
public readonly altFiles?: string[],
public readonly force?: boolean
) {
this.path = createHash('sha512')
.update(files.map((f) => `${f.file.path}${f.hash}`).join(''))
.digest('hex');
......@@ -112,7 +117,8 @@ export class PackagerService extends ConsoleLogger {
async build(
stream: NodeJS.ReadableStream,
pathPrefix?: string,
lastBuildChecksums: Record<string, string>[] = []
lastBuildChecksums: Record<string, string>[] = [],
force?: boolean
): Promise<PackageResult> {
this.log(`Start packaging.`);
const root = await fs.promises.mkdtemp(path.join(this.packagerWorkingDirectory, 'mycard-console-'));
......@@ -147,7 +153,7 @@ export class PackagerService extends ConsoleLogger {
const filesWithHash: FileWithHash[] = files.map((file) => ({ file, hash: checksum[file.path] }));
// 整包
new ArchiveTask(ArchiveType.Full, filesWithHash, await fs.promises.readdir(root)).addToTask(archiveTasks);
new ArchiveTask(ArchiveType.Full, filesWithHash, await fs.promises.readdir(root), force).addToTask(archiveTasks);
if (files.length > 1) {
// 整包(512M 一包)
......@@ -170,7 +176,7 @@ export class PackagerService extends ConsoleLogger {
}
if (buckets.length > 1) {
for (const bucket of buckets) {
new ArchiveTask(ArchiveType.Fulls, bucket.files).addToTask(archiveTasks);
new ArchiveTask(ArchiveType.Fulls, bucket.files, undefined, force).addToTask(archiveTasks);
}
this.log(`Created ${buckets.length} fulls archives.`);
} else {
......@@ -182,7 +188,7 @@ export class PackagerService extends ConsoleLogger {
for (const lastChecksum of lastBuildChecksums) {
const changedFiles = filesWithHash.filter((f) => !lastChecksum[f.file.path] || lastChecksum[f.file.path] !== f.hash);
if (changedFiles.length) {
new ArchiveTask(ArchiveType.Update, changedFiles).addToTask(archiveTasks);
new ArchiveTask(ArchiveType.Update, changedFiles, undefined, force).addToTask(archiveTasks);
}
}
}
......@@ -197,17 +203,17 @@ export class PackagerService extends ConsoleLogger {
buckets[extname] ??= new Bucket();
const bucket = buckets[extname];
if (!bucket.canFit(file, this.bucket_max)) {
new ArchiveTask(ArchiveType.Part, bucket.files).addToTask(pendingPartTasks, true);
new ArchiveTask(ArchiveType.Part, bucket.files, undefined, force).addToTask(pendingPartTasks, true);
bucket.empty();
}
bucket.addFile(file);
} else {
new ArchiveTask(ArchiveType.Part, [file]).addToTask(pendingPartTasks, true);
new ArchiveTask(ArchiveType.Part, [file], undefined, force).addToTask(pendingPartTasks, true);
}
}
for (const bucket of Object.values(buckets)) {
if (bucket.files.length) {
new ArchiveTask(ArchiveType.Part, bucket.files).addToTask(pendingPartTasks, true);
new ArchiveTask(ArchiveType.Part, bucket.files, undefined, force).addToTask(pendingPartTasks, true);
}
}
......@@ -288,14 +294,16 @@ export class PackagerService extends ConsoleLogger {
async archive(root: string, archiveTask: ArchiveTask): Promise<Archive> {
const archive = archiveTask.archive;
const archiveName = archiveTask.archiveFullPath;
const existing = await this.s3.fileExists(archiveName);
if (existing) {
const hash = await this.appService.lookForExistingArchiveHash(archiveTask.path);
if (hash) {
archive.hash = hash;
archive.size = existing.ContentLength;
this.log(`Archive ${archiveName} exists, skipping.`);
return archive;
if (!archiveTask.force) {
const existing = await this.s3.fileExists(archiveName);
if (existing) {
const hash = await this.appService.lookForExistingArchiveHash(archiveTask.path);
if (hash) {
archive.hash = hash;
archive.size = existing.ContentLength;
this.log(`Archive ${archiveName} exists, skipping.`);
return archive;
}
}
}
return this.lock.acquire([`archive:${archiveTask.path}`], async () => this.archiveProcess(root, archiveTask));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment