Check for auction if deployment request id is not present

This commit is contained in:
IshaVenikar 2024-10-07 18:04:02 +05:30 committed by Nabarun
parent 2a011a99cd
commit cedad71c44
2 changed files with 45 additions and 32 deletions

View File

@ -256,6 +256,8 @@ export class Registry {
this.registryConfig.privateKey, this.registryConfig.privateKey,
fee fee
); );
log(`Application deployment auction created: ${auctionResult.auction.id}`);
log(`Application deployment auction record published: ${result.id}`); log(`Application deployment auction record published: ${result.id}`);
log('Application deployment auction data:', applicationDeploymentAuction); log('Application deployment auction data:', applicationDeploymentAuction);
@ -337,18 +339,22 @@ export class Registry {
const auctionResult = records[0]; const auctionResult = records[0];
let deployerLrns = []; let deployerLrns = [];
const { winnerAddresses } = auctionResult.auction; const { winnerAddresses } = auctionResult;
for (const auctionWinner of winnerAddresses) { for (const auctionWinner of winnerAddresses) {
const deployerRecord = await this.registry.queryRecords( const deployerRecords = await this.registry.queryRecords(
{ {
paymentAddress: auctionWinner, paymentAddress: auctionWinner,
}, },
true true
); );
const lrn = deployerRecord.names.length > 0 ? deployerRecord.names[0] : null; for (const record of deployerRecords) {
deployerLrns.push(lrn); if (record.names && record.names.length > 0) {
deployerLrns.push(record.names[0]);
break;
}
}
} }
return deployerLrns; return deployerLrns;
@ -449,11 +455,15 @@ export class Registry {
}; };
} }
async getCompletedAuctionIds(auctionIds: string[] | null | undefined): Promise<string[]> { async getCompletedAuctionIds(auctionIds: (string | null | undefined)[]): Promise<string[] | null> {
if(auctionIds === null || auctionIds === undefined) { const validAuctionIds = auctionIds.filter((id): id is string => id !== null && id !== undefined);
return [];
if (!validAuctionIds.length) {
return null;
} }
const auctions = await this.registry.getAuctionsByIds(auctionIds);
const auctions = await this.registry.getAuctionsByIds(validAuctionIds);
const completedAuctions = auctions const completedAuctions = auctions
.filter((auction: Auction) => auction.status === 'completed') .filter((auction: Auction) => auction.status === 'completed')
.map((auction: Auction) => auction.id); .map((auction: Auction) => auction.id);

View File

@ -61,6 +61,8 @@ export class Service {
this.checkDeployRecordsAndUpdate(); this.checkDeployRecordsAndUpdate();
// Start check for ApplicationDeploymentRemovalRecords asynchronously // Start check for ApplicationDeploymentRemovalRecords asynchronously
this.checkDeploymentRemovalRecordsAndUpdate(); this.checkDeploymentRemovalRecordsAndUpdate();
// Start check for Deployment Auctions asynchronously
this.checkAuctionStatus();
} }
/** /**
@ -267,27 +269,27 @@ export class Service {
* Checks for auction status for all ongoing auctions * Checks for auction status for all ongoing auctions
* Calls the createDeploymentFromAuction method for deployments with completed auctions * Calls the createDeploymentFromAuction method for deployments with completed auctions
*/ */
async checkAuctionStatus( async checkAuctionStatus(): Promise<void> {
): Promise<void> {
// Deployment should be in building state and should not have domain.
const projects = await this.db.getProjects({ const projects = await this.db.getProjects({
where: { where: {
deployerLrn: IsNull() deployments: {
applicationDeploymentRequestId: IsNull()
}
}, },
}); });
const auctionIds = projects const auctionIds = projects.map((project) => project.auctionId);
.map(project => project.auctionId) as string[];
// Get all the auctions for those ids with auction status completed
const completedAuctionIds = await this.registry.getCompletedAuctionIds(auctionIds); const completedAuctionIds = await this.registry.getCompletedAuctionIds(auctionIds);
// If the deplyer lrn array is empty then call createDeploymentFromAuction
const auctionProjects = projects.filter(project => if (completedAuctionIds) {
const auctionProjects = projects.filter((project) =>
completedAuctionIds.includes(project.auctionId!) completedAuctionIds.includes(project.auctionId!)
); );
for (const project of auctionProjects) { for (const project of auctionProjects) {
await this.createDeploymentFromAuction(project); await this.createDeploymentFromAuction(project);
} }
}
this.deployRecordCheckTimeout = setTimeout(() => { this.deployRecordCheckTimeout = setTimeout(() => {
this.checkAuctionStatus(); this.checkAuctionStatus();
@ -692,8 +694,7 @@ export class Service {
await this.db.updateProjectById(project.id!, { await this.db.updateProjectById(project.id!, {
deployerLrn: deployerLrns deployerLrn: deployerLrns
}) })
const octokit = await this.getOctokit(project.ownerId!);
const octokit = await this.getOctokit(project.owner!.id!);
const [owner, repo] = project.repository!.split('/'); const [owner, repo] = project.repository!.split('/');
const repoUrl = ( const repoUrl = (
@ -739,7 +740,7 @@ export class Service {
applicationRecordData, applicationRecordData,
domain: deploymentData.domain, domain: deploymentData.domain,
createdBy: Object.assign(new User(), { createdBy: Object.assign(new User(), {
id: project.owner!.id!, id: project.ownerId!,
}), }),
}); });
@ -767,7 +768,7 @@ export class Service {
// So publish project DNS deployment first so that ApplicationDeploymentRecord for the same is available when deleting deployment later // So publish project DNS deployment first so that ApplicationDeploymentRecord for the same is available when deleting deployment later
await this.registry.createApplicationDeploymentRequest({ await this.registry.createApplicationDeploymentRequest({
deployment: newDeployment, deployment: newDeployment,
appName: project.name!, appName: repo,
repository: repoUrl, repository: repoUrl,
environmentVariables: environmentVariablesObj, environmentVariables: environmentVariablesObj,
dns: `${newDeployment.project.name}`, dns: `${newDeployment.project.name}`,
@ -778,7 +779,7 @@ export class Service {
// Create requests for all the deployers // Create requests for all the deployers
await this.registry.createApplicationDeploymentRequest({ await this.registry.createApplicationDeploymentRequest({
deployment: newDeployment, deployment: newDeployment,
appName: project.name!, appName: repo,
repository: repoUrl, repository: repoUrl,
auctionId: project.auctionId!, auctionId: project.auctionId!,
lrn: deployer, lrn: deployer,
@ -882,10 +883,12 @@ export class Service {
commitMessage: latestCommit.commit.message, commitMessage: latestCommit.commit.message,
}; };
await (auctionData if (auctionData) {
? this.registry.createApplicationDeploymentAuction(repo, octokit, auctionData!, deploymentData) const { applicationDeploymentAuctionId } = await this.registry.createApplicationDeploymentAuction(repo, octokit, auctionData!, deploymentData);
: this.createDeployment(user.id, octokit, deploymentData, lrn) await this.updateProject(project.id, { auctionId: applicationDeploymentAuctionId })
); } else {
await this.createDeployment(user.id, octokit, deploymentData, lrn);
}
await this.createRepoHook(octokit, project); await this.createRepoHook(octokit, project);