Build cron for data seed demo (#4142)

* Migrate command to cron

* Put back command using job as well

* Build service and module + move into folder

---------

Co-authored-by: Thomas Trompette <thomast@twenty.com>
This commit is contained in:
Thomas Trompette 2024-02-24 12:30:12 +01:00 committed by GitHub
parent a2eca16646
commit b1eb0577bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 96 additions and 13 deletions

View File

@ -0,0 +1 @@
export const dataSeedDemoWorkspaceCronPattern = '0 22 * * *'; // Every day at 10pm

View File

@ -0,0 +1,29 @@
import { Inject } from '@nestjs/common';
import { Command, CommandRunner } from 'nest-commander';
import { dataSeedDemoWorkspaceCronPattern } from 'src/database/commands/data-seed-demo-workspace/crons/data-seed-demo-workspace-cron-pattern';
import { DataSeedDemoWorkspaceJob } from 'src/database/commands/data-seed-demo-workspace/jobs/data-seed-demo-workspace.job';
import { MessageQueue } from 'src/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/integrations/message-queue/services/message-queue.service';
@Command({
name: 'workspace:cron:seed:demo',
description: 'Seed workspace with demo data.',
})
export class DataSeedDemoWorkspaceCronCommand extends CommandRunner {
constructor(
@Inject(MessageQueue.cronQueue)
private readonly messageQueueService: MessageQueueService,
) {
super();
}
async run(): Promise<void> {
await this.messageQueueService.addCron<undefined>(
DataSeedDemoWorkspaceJob.name,
undefined,
dataSeedDemoWorkspaceCronPattern,
);
}
}

View File

@ -0,0 +1,19 @@
import { Command, CommandRunner } from 'nest-commander';
import { DataSeedDemoWorkspaceService } from 'src/database/commands/data-seed-demo-workspace/services/data-seed-demo-workspace.service';
@Command({
name: 'workspace:seed:demo',
description: 'Seed workspace with demo data. Use in development only.',
})
export class DataSeedDemoWorkspaceCommand extends CommandRunner {
constructor(
private readonly dataSeedDemoWorkspaceService: DataSeedDemoWorkspaceService,
) {
super();
}
async run(): Promise<void> {
await this.dataSeedDemoWorkspaceService.seedDemo();
}
}

View File

@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { EnvironmentModule } from 'src/integrations/environment/environment.module';
import { WorkspaceManagerModule } from 'src/workspace/workspace-manager/workspace-manager.module';
import { DataSeedDemoWorkspaceService } from 'src/database/commands/data-seed-demo-workspace/services/data-seed-demo-workspace.service';
@Module({
imports: [WorkspaceManagerModule, EnvironmentModule],
providers: [DataSeedDemoWorkspaceService],
exports: [DataSeedDemoWorkspaceService],
})
export class DataSeedDemoWorkspaceModule {}

View File

@ -0,0 +1,16 @@
import { Injectable } from '@nestjs/common';
import { MessageQueueJob } from 'src/integrations/message-queue/interfaces/message-queue-job.interface';
import { DataSeedDemoWorkspaceService } from 'src/database/commands/data-seed-demo-workspace/services/data-seed-demo-workspace.service';
@Injectable()
export class DataSeedDemoWorkspaceJob implements MessageQueueJob<undefined> {
constructor(
private readonly dataSeedDemoWorkspaceService: DataSeedDemoWorkspaceService,
) {}
async handle(): Promise<void> {
await this.dataSeedDemoWorkspaceService.seedDemo();
}
}

View File

@ -1,27 +1,22 @@
import { Command, CommandRunner } from 'nest-commander';
import { Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import {
deleteCoreSchema,
seedCoreSchema,
} from 'src/database/typeorm-seeds/core/demo';
} from 'src/database/typeorm-seeds/core';
import { EnvironmentService } from 'src/integrations/environment/environment.service';
import { WorkspaceManagerService } from 'src/workspace/workspace-manager/workspace-manager.service';
@Command({
name: 'workspace:seed:demo',
description:
'Seed workspace with demo data. This command is intended for development only.',
})
export class DataSeedDemoWorkspaceCommand extends CommandRunner {
@Injectable()
export class DataSeedDemoWorkspaceService {
constructor(
private readonly environmentService: EnvironmentService,
private readonly workspaceManagerService: WorkspaceManagerService,
) {
super();
}
) {}
async run(): Promise<void> {
async seedDemo(): Promise<void> {
try {
const dataSource = new DataSource({
url: this.environmentService.getPGDatabaseUrl(),

View File

@ -6,11 +6,13 @@ import { DataSourceModule } from 'src/metadata/data-source/data-source.module';
import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
import { WorkspaceModule } from 'src/core/workspace/workspace.module';
import { DataSeedWorkspaceCommand } from 'src/database/commands/data-seed-dev-workspace.command';
import { DataSeedDemoWorkspaceCommand } from 'src/database/commands/data-seed-demo-workspace.command';
import { DataSeedDemoWorkspaceCronCommand } from 'src/database/commands/data-seed-demo-workspace/crons/data-seed-demo-workspace.cron.command';
import { WorkspaceDataSourceModule } from 'src/workspace/workspace-datasource/workspace-datasource.module';
import { WorkspaceSyncMetadataModule } from 'src/workspace/workspace-sync-metadata/workspace-sync-metadata.module';
import { ObjectMetadataModule } from 'src/metadata/object-metadata/object-metadata.module';
import { WorkspaceAddTotalCountCommand } from 'src/database/commands/workspace-add-total-count.command';
import { DataSeedDemoWorkspaceCommand } from 'src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace-command';
import { DataSeedDemoWorkspaceModule } from 'src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace.module';
@Module({
imports: [
@ -21,10 +23,12 @@ import { WorkspaceAddTotalCountCommand } from 'src/database/commands/workspace-a
WorkspaceDataSourceModule,
WorkspaceSyncMetadataModule,
ObjectMetadataModule,
DataSeedDemoWorkspaceModule,
],
providers: [
DataSeedWorkspaceCommand,
DataSeedDemoWorkspaceCommand,
DataSeedDemoWorkspaceCronCommand,
WorkspaceAddTotalCountCommand,
ConfirmationQuestion,
],

View File

@ -24,6 +24,8 @@ import { CreateCompaniesAndContactsAfterSyncJob } from 'src/workspace/messaging/
import { CreateCompaniesAndContactsModule } from 'src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.module';
import { MessageChannelModule } from 'src/workspace/messaging/repositories/message-channel/message-channel.module';
import { MessageParticipantModule } from 'src/workspace/messaging/repositories/message-participant/message-participant.module';
import { DataSeedDemoWorkspaceJob } from 'src/database/commands/data-seed-demo-workspace/jobs/data-seed-demo-workspace.job';
import { DataSeedDemoWorkspaceModule } from 'src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace.module';
@Module({
imports: [
@ -41,6 +43,7 @@ import { MessageParticipantModule } from 'src/workspace/messaging/repositories/m
MessageParticipantModule,
CreateCompaniesAndContactsModule,
MessageChannelModule,
DataSeedDemoWorkspaceModule,
],
providers: [
{
@ -76,6 +79,10 @@ import { MessageParticipantModule } from 'src/workspace/messaging/repositories/m
provide: CreateCompaniesAndContactsAfterSyncJob.name,
useClass: CreateCompaniesAndContactsAfterSyncJob,
},
{
provide: DataSeedDemoWorkspaceJob.name,
useClass: DataSeedDemoWorkspaceJob,
},
],
})
export class JobsModule {