fix: retrieve the build org job state

This commit is contained in:
Ahmed Bouhuolia
2025-05-10 22:33:54 +02:00
parent 7506c2f37f
commit a42143a996
8 changed files with 79 additions and 16 deletions

View File

@@ -1,13 +1,14 @@
import { Queue } from 'bullmq';
import { InjectQueue } from '@nestjs/bullmq';
import { Injectable } from '@nestjs/common';
import {
BuildOrganizationResult,
IOrganizationBuildEventPayload,
IOrganizationBuiltEventPayload,
OrganizationBuildQueue,
OrganizationBuildQueueJob,
OrganizationBuildQueueJobPayload,
} from '../Organization.types';
import { Injectable } from '@nestjs/common';
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
import {
throwIfTenantInitizalized,
@@ -48,7 +49,7 @@ export class BuildOrganizationService {
await this.tenantsManager.createDatabase();
await this.tenantsManager.migrateTenant();
await this.tenantsManager.seedTenant()
await this.tenantsManager.seedTenant();
// Throws `onOrganizationBuild` event.
await this.eventPublisher.emitAsync(events.organization.build, {
@@ -77,7 +78,7 @@ export class BuildOrganizationService {
*/
async buildRunJob(
buildDTO: BuildOrganizationDto,
): Promise<{ nextRunAt: Date; jobId: string }> {
): Promise<BuildOrganizationResult> {
const tenant = await this.tenancyContext.getTenant();
const systemUser = await this.tenancyContext.getSystemUser();
@@ -106,8 +107,9 @@ export class BuildOrganizationService {
await this.tenantRepository.markAsBuilding(jobMeta.id).findById(tenant.id);
return {
nextRunAt: jobMeta.data.nextRunAt,
jobId: jobMeta.data.id,
delay: jobMeta.delay,
processedOn: jobMeta.processedOn,
jobId: jobMeta.id,
};
}

View File

@@ -0,0 +1,37 @@
import { Queue } from 'bullmq';
import { Injectable } from '@nestjs/common';
import { InjectQueue } from '@nestjs/bullmq';
import { OrganizationBuildQueue } from '../Organization.types';
import { ServiceError } from '@/modules/Items/ServiceError';
@Injectable()
export class GetBuildOrganizationBuildJob {
constructor(
@InjectQueue(OrganizationBuildQueue)
private readonly organizationBuildQueue: Queue,
) {}
/**
* Gets the build job details by job ID.
* @param {string} jobId - The ID of the job to retrieve.
* @returns {Promise<any>} - Returns the job details.
*/
async getJobDetails(jobId: string): Promise<any> {
const job = await this.organizationBuildQueue.getJob(jobId);
if (!job) {
throw new ServiceError('Job not found', 'JOB.NOT_FOUND');
}
const state = await job.getState();
return {
id: job.id,
state,
progress: job.progress,
isCompleted: state === 'completed',
isRunning: state === 'active',
isWaiting: state === 'waiting' || state === 'waiting-children',
isFailed: state === 'failed',
};
}
}