Commit 13962ae6 authored by nanahira's avatar nanahira

add force pack

parent aa40d41a
...@@ -23,7 +23,7 @@ import { FileUploadDto } from './dto/FileUpload.dto'; ...@@ -23,7 +23,7 @@ import { FileUploadDto } from './dto/FileUpload.dto';
import Busboy from 'busboy'; import Busboy from 'busboy';
import { Request } from 'express'; import { Request } from 'express';
import { PackageResult } from './dto/PackageResult.dto'; import { PackageResult } from './dto/PackageResult.dto';
import { DepotDto } from './dto/Depot.dto'; import { DepotDto, MakeBuildDto } from './dto/Depot.dto';
import AppClass = AppsJson.AppClass; import AppClass = AppsJson.AppClass;
@Controller('api') @Controller('api')
...@@ -109,7 +109,7 @@ export class AppController { ...@@ -109,7 +109,7 @@ export class AppController {
async makeBuild( async makeBuild(
@FetchMyCardUser() user: MyCardUser, @FetchMyCardUser() user: MyCardUser,
@Param('id') id: string, @Param('id') id: string,
@Query(new ValidationPipe({ transform: true })) depot: DepotDto, @Query(new ValidationPipe({ transform: true })) depot: MakeBuildDto,
@Param('version') version: string, @Param('version') version: string,
@Req() req: Request @Req() req: Request
) { ) {
......
...@@ -8,7 +8,7 @@ import { MyCardUser } from './utility/mycard-auth'; ...@@ -8,7 +8,7 @@ import { MyCardUser } from './utility/mycard-auth';
import { PackagerService } from './packager/packager.service'; import { PackagerService } from './packager/packager.service';
import internal from 'stream'; import internal from 'stream';
import { Depot } from './entities/Depot.entity'; 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 { Build } from './entities/Build.entity';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { Archive, ArchiveType } from './entities/Archive.entity'; import { Archive, ArchiveType } from './entities/Archive.entity';
...@@ -184,7 +184,7 @@ export class AppService extends ConsoleLogger { ...@@ -184,7 +184,7 @@ export class AppService extends ConsoleLogger {
private buildLock = new BetterLock(); 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) { if (!user) {
throw new BlankReturnMessageDto(401, 'Needs login').toException(); throw new BlankReturnMessageDto(401, 'Needs login').toException();
} }
...@@ -214,7 +214,7 @@ export class AppService extends ConsoleLogger { ...@@ -214,7 +214,7 @@ export class AppService extends ConsoleLogger {
.getRepository(Build) .getRepository(Build)
.find({ where: { depot }, order: { id: 'DESC' }, select: ['id', 'checksum'], take: this.packageVersionTraceCount }) .find({ where: { depot }, order: { id: 'DESC' }, select: ['id', 'checksum'], take: this.packageVersionTraceCount })
).map((b) => b.checksum); ).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)}`); this.log(`Saving package info of ${app.id} ${version} for ${JSON.stringify(depot)}`);
build.checksum = result.checksum; build.checksum = result.checksum;
//build.archives = result.archives; //build.archives = result.archives;
......
...@@ -44,3 +44,8 @@ export class DepotDto implements DepotLike { ...@@ -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; ...@@ -28,7 +28,12 @@ const ARG_SIZE_LIMIT = 30000;
class ArchiveTask { class ArchiveTask {
readonly path: string; 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') this.path = createHash('sha512')
.update(files.map((f) => `${f.file.path}${f.hash}`).join('')) .update(files.map((f) => `${f.file.path}${f.hash}`).join(''))
.digest('hex'); .digest('hex');
...@@ -112,7 +117,8 @@ export class PackagerService extends ConsoleLogger { ...@@ -112,7 +117,8 @@ export class PackagerService extends ConsoleLogger {
async build( async build(
stream: NodeJS.ReadableStream, stream: NodeJS.ReadableStream,
pathPrefix?: string, pathPrefix?: string,
lastBuildChecksums: Record<string, string>[] = [] lastBuildChecksums: Record<string, string>[] = [],
force?: boolean
): Promise<PackageResult> { ): Promise<PackageResult> {
this.log(`Start packaging.`); this.log(`Start packaging.`);
const root = await fs.promises.mkdtemp(path.join(this.packagerWorkingDirectory, 'mycard-console-')); const root = await fs.promises.mkdtemp(path.join(this.packagerWorkingDirectory, 'mycard-console-'));
...@@ -147,7 +153,7 @@ export class PackagerService extends ConsoleLogger { ...@@ -147,7 +153,7 @@ export class PackagerService extends ConsoleLogger {
const filesWithHash: FileWithHash[] = files.map((file) => ({ file, hash: checksum[file.path] })); 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) { if (files.length > 1) {
// 整包(512M 一包) // 整包(512M 一包)
...@@ -170,7 +176,7 @@ export class PackagerService extends ConsoleLogger { ...@@ -170,7 +176,7 @@ export class PackagerService extends ConsoleLogger {
} }
if (buckets.length > 1) { if (buckets.length > 1) {
for (const bucket of buckets) { 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.`); this.log(`Created ${buckets.length} fulls archives.`);
} else { } else {
...@@ -182,7 +188,7 @@ export class PackagerService extends ConsoleLogger { ...@@ -182,7 +188,7 @@ export class PackagerService extends ConsoleLogger {
for (const lastChecksum of lastBuildChecksums) { for (const lastChecksum of lastBuildChecksums) {
const changedFiles = filesWithHash.filter((f) => !lastChecksum[f.file.path] || lastChecksum[f.file.path] !== f.hash); const changedFiles = filesWithHash.filter((f) => !lastChecksum[f.file.path] || lastChecksum[f.file.path] !== f.hash);
if (changedFiles.length) { 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 { ...@@ -197,17 +203,17 @@ export class PackagerService extends ConsoleLogger {
buckets[extname] ??= new Bucket(); buckets[extname] ??= new Bucket();
const bucket = buckets[extname]; const bucket = buckets[extname];
if (!bucket.canFit(file, this.bucket_max)) { 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.empty();
} }
bucket.addFile(file); bucket.addFile(file);
} else { } 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)) { for (const bucket of Object.values(buckets)) {
if (bucket.files.length) { 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,6 +294,7 @@ export class PackagerService extends ConsoleLogger { ...@@ -288,6 +294,7 @@ export class PackagerService extends ConsoleLogger {
async archive(root: string, archiveTask: ArchiveTask): Promise<Archive> { async archive(root: string, archiveTask: ArchiveTask): Promise<Archive> {
const archive = archiveTask.archive; const archive = archiveTask.archive;
const archiveName = archiveTask.archiveFullPath; const archiveName = archiveTask.archiveFullPath;
if (!archiveTask.force) {
const existing = await this.s3.fileExists(archiveName); const existing = await this.s3.fileExists(archiveName);
if (existing) { if (existing) {
const hash = await this.appService.lookForExistingArchiveHash(archiveTask.path); const hash = await this.appService.lookForExistingArchiveHash(archiveTask.path);
...@@ -298,6 +305,7 @@ export class PackagerService extends ConsoleLogger { ...@@ -298,6 +305,7 @@ export class PackagerService extends ConsoleLogger {
return archive; return archive;
} }
} }
}
return this.lock.acquire([`archive:${archiveTask.path}`], async () => this.archiveProcess(root, archiveTask)); 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