Browse Source

feat: move base create to jobs

pull/6528/head
mertmit 1 year ago
parent
commit
185c90f329
  1. 21
      packages/nocodb/src/controllers/bases.controller.ts
  2. 1
      packages/nocodb/src/interface/Jobs.ts
  3. 6
      packages/nocodb/src/modules/jobs/fallback/fallback-queue.service.ts
  4. 10
      packages/nocodb/src/modules/jobs/jobs.module.ts
  5. 46
      packages/nocodb/src/modules/jobs/jobs/base-create/base-create.controller.ts
  6. 28
      packages/nocodb/src/modules/jobs/jobs/base-create/base-create.processor.ts
  7. 1
      packages/nocodb/src/modules/metas/metas.module.ts

21
packages/nocodb/src/controllers/bases.controller.ts

@ -3,10 +3,8 @@ import {
Controller,
Delete,
Get,
HttpCode,
Param,
Patch,
Post,
UseGuards,
} from '@nestjs/common';
import { BaseReqType } from 'nocodb-sdk';
@ -77,23 +75,4 @@ export class BasesController {
});
return result;
}
@Post('/api/v1/db/meta/projects/:projectId/bases')
@HttpCode(200)
@Acl('baseCreate')
async baseCreate(
@Param('projectId') projectId: string,
@Body() body: BaseReqType,
) {
const base = await this.basesService.baseCreate({
projectId,
base: body,
});
if (base.isMeta()) {
delete base.config;
}
return base;
}
}

1
packages/nocodb/src/interface/Jobs.ts

@ -5,6 +5,7 @@ export enum JobTypes {
DuplicateModel = 'duplicate-model',
AtImport = 'at-import',
MetaSync = 'meta-sync',
BaseCreate = 'base-create',
}
export enum JobStatus {

6
packages/nocodb/src/modules/jobs/fallback/fallback-queue.service.ts

@ -4,6 +4,7 @@ import Emittery from 'emittery';
import { DuplicateProcessor } from '../jobs/export-import/duplicate.processor';
import { AtImportProcessor } from '../jobs/at-import/at-import.processor';
import { MetaSyncProcessor } from '../jobs/meta-sync/meta-sync.processor';
import { BaseCreateProcessor } from '../jobs/base-create/base-create.processor';
import { JobsEventService } from './jobs-event.service';
import { JobStatus, JobTypes } from '~/interface/Jobs';
@ -27,6 +28,7 @@ export class QueueService {
private readonly duplicateProcessor: DuplicateProcessor,
private readonly atImportProcessor: AtImportProcessor,
private readonly metaSyncProcessor: MetaSyncProcessor,
private readonly baseCreateProcessor: BaseCreateProcessor,
) {
this.emitter.on(JobStatus.ACTIVE, (data: { job: Job }) => {
const job = this.queueMemory.find((job) => job.id === data.job.id);
@ -72,6 +74,10 @@ export class QueueService {
this: this.metaSyncProcessor,
fn: this.metaSyncProcessor.job,
},
[JobTypes.BaseCreate]: {
this: this.baseCreateProcessor,
fn: this.baseCreateProcessor.job,
},
};
async jobWrapper(job: Job) {

10
packages/nocodb/src/modules/jobs/jobs.module.ts

@ -10,6 +10,8 @@ import { DuplicateController } from './jobs/export-import/duplicate.controller';
import { DuplicateProcessor } from './jobs/export-import/duplicate.processor';
import { MetaSyncController } from './jobs/meta-sync/meta-sync.controller';
import { MetaSyncProcessor } from './jobs/meta-sync/meta-sync.processor';
import { BaseCreateController } from './jobs/base-create/base-create.controller';
import { BaseCreateProcessor } from './jobs/base-create/base-create.processor';
// Jobs Module Related
import { JobsLogService } from './jobs/jobs-log.service';
@ -47,7 +49,12 @@ import { GlobalModule } from '~/modules/global/global.module';
],
controllers: [
...(process.env.NC_WORKER_CONTAINER !== 'true'
? [DuplicateController, AtImportController, MetaSyncController]
? [
DuplicateController,
AtImportController,
MetaSyncController,
BaseCreateController,
]
: []),
],
providers: [
@ -67,6 +74,7 @@ import { GlobalModule } from '~/modules/global/global.module';
DuplicateProcessor,
AtImportProcessor,
MetaSyncProcessor,
BaseCreateProcessor,
],
})
export class JobsModule {}

46
packages/nocodb/src/modules/jobs/jobs/base-create/base-create.controller.ts

@ -0,0 +1,46 @@
import {
Body,
Controller,
HttpCode,
Inject,
Param,
Post,
UseGuards,
} from '@nestjs/common';
import { BaseReqType } from 'nocodb-sdk';
import { GlobalGuard } from '~/guards/global/global.guard';
import { Acl } from '~/middlewares/extract-ids/extract-ids.middleware';
import { NcError } from '~/helpers/catchError';
import { JobTypes } from '~/interface/Jobs';
@Controller()
@UseGuards(GlobalGuard)
export class BaseCreateController {
constructor(@Inject('JobsService') private readonly jobsService) {}
@Post('/api/v1/db/meta/projects/:projectId/bases')
@HttpCode(200)
@Acl('baseCreate')
async baseCreate(
@Param('projectId') projectId: string,
@Body() body: BaseReqType,
) {
const jobs = await this.jobsService.jobList();
const fnd = jobs.find(
(j) => j.name === JobTypes.BaseCreate && j.data.projectId === projectId,
);
if (fnd) {
NcError.badRequest(
'Another base creation is in progress for this project.',
);
}
const job = await this.jobsService.add(JobTypes.BaseCreate, {
projectId,
base: body,
});
return { id: job.id };
}
}

28
packages/nocodb/src/modules/jobs/jobs/base-create/base-create.processor.ts

@ -0,0 +1,28 @@
import debug from 'debug';
import { Process, Processor } from '@nestjs/bull';
import { Job } from 'bull';
import { JOBS_QUEUE, JobTypes } from '~/interface/Jobs';
import { BasesService } from '~/services/bases.service';
@Processor(JOBS_QUEUE)
export class BaseCreateProcessor {
private readonly debugLog = debug('nc:meta-sync:processor');
constructor(private readonly basesService: BasesService) {}
@Process(JobTypes.BaseCreate)
async job(job: Job) {
const { projectId, base } = job.data;
const createdBase = await this.basesService.baseCreate({
projectId,
base,
});
if (createdBase.isMeta()) {
delete createdBase.config;
}
return createdBase;
}
}

1
packages/nocodb/src/modules/metas/metas.module.ts

@ -180,6 +180,7 @@ export const metaModuleMetadata = {
ProjectUsersService,
HooksService,
MetaDiffsService,
BasesService,
],
};

Loading…
Cancel
Save