Commit cdc3e853 authored by nanahira's avatar nanahira

one build per app

parent 3f81a889
Pipeline #39939 passed with stages
in 7 minutes and 15 seconds
......@@ -21,6 +21,7 @@ import { AssetsS3Service } from './assets-s3/assets-s3.service';
import { Redis } from 'ioredis';
import { InjectRedis } from '@nestjs-modules/ioredis';
import _ from 'lodash';
import BetterLock from 'better-lock';
@Injectable()
export class AppService extends ConsoleLogger {
......@@ -181,6 +182,8 @@ export class AppService extends ConsoleLogger {
return this.db.getRepository(Build).findOne({ where: { depot, version }, select: ['id'] });
}
private buildLock = new BetterLock();
async makeBuild(user: MyCardUser, stream: NodeJS.ReadableStream, id: string, depotDto: DepotDto, version: string) {
if (!user) {
throw new BlankReturnMessageDto(401, 'Needs login').toException();
......@@ -203,49 +206,51 @@ export class AppService extends ConsoleLogger {
let build = new Build();
build.depot = depot;
build.version = version;
this.log(`Start packaging ${app.id} ${version} for ${JSON.stringify(depot)}.`);
try {
const previousTracingBuildChecksums = (
await this.db
.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);
this.log(`Saving package info of ${app.id} ${version} for ${JSON.stringify(depot)}`);
build.checksum = result.checksum;
//build.archives = result.archives;
//build = await this.db.getRepository(Build).save(build);
await this.db.transaction(async (edb) => {
this.log(`Saving build info.`);
build = await edb.getRepository(Build).save(build);
let archivePot: Archive[] = [];
let currentSize = 0;
for (const archive of result.archives) {
archive.build = build;
const size = archive.getParamSize();
if (currentSize > 0 && currentSize + size > 60000) {
return this.buildLock.acquire(id, async () => {
this.log(`Start packaging ${app.id} ${version} for ${JSON.stringify(depot)}.`);
try {
const previousTracingBuildChecksums = (
await this.db
.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);
this.log(`Saving package info of ${app.id} ${version} for ${JSON.stringify(depot)}`);
build.checksum = result.checksum;
//build.archives = result.archives;
//build = await this.db.getRepository(Build).save(build);
await this.db.transaction(async (edb) => {
this.log(`Saving build info.`);
build = await edb.getRepository(Build).save(build);
let archivePot: Archive[] = [];
let currentSize = 0;
for (const archive of result.archives) {
archive.build = build;
const size = archive.getParamSize();
if (currentSize > 0 && currentSize + size > 60000) {
this.log(`Saving ${archivePot.length} archive infos.`);
await edb.getRepository(Archive).save(archivePot);
archivePot = [];
currentSize = 0;
}
archivePot.push(archive);
currentSize += size;
}
if (archivePot.length) {
this.log(`Saving ${archivePot.length} archive infos.`);
await edb.getRepository(Archive).save(archivePot);
archivePot = [];
currentSize = 0;
}
archivePot.push(archive);
currentSize += size;
}
if (archivePot.length) {
this.log(`Saving ${archivePot.length} archive infos.`);
await edb.getRepository(Archive).save(archivePot);
}
});
this.mirror.triggerMirror();
this.log(`Finished packaging ${app.id} ${version} for ${JSON.stringify(depot)}`);
return new BlankReturnMessageDto(201, 'success');
} catch (e) {
this.error(`Build ${app.id} ${JSON.stringify(depotDto.toActual)} ${build.version} failed: ${e.toString()}`);
throw new BlankReturnMessageDto(500, 'Build failed').toException();
}
});
this.mirror.triggerMirror();
this.log(`Finished packaging ${app.id} ${version} for ${JSON.stringify(depot)}`);
return new BlankReturnMessageDto(201, 'success');
} catch (e) {
this.error(`Build ${app.id} ${JSON.stringify(depotDto.toActual)} ${build.version} failed: ${e.toString()}`);
throw new BlankReturnMessageDto(500, 'Build failed').toException();
}
})
}
private packageReferenceSubQuery(query: SelectQueryBuilder<any>) {
......
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