-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4786 from mahendraHegde/refactor-execution-detail
refactor: use execution queue instead of writing directly to execution detail collection
- Loading branch information
Showing
32 changed files
with
738 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
apps/worker/src/app/workflow/services/execution-log.worker.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { expect } from 'chai'; | ||
import { setTimeout } from 'timers/promises'; | ||
|
||
import { TriggerEvent, ExecutionLogQueueService, CreateExecutionDetails } from '@novu/application-generic'; | ||
|
||
import { ExecutionLogWorker } from './execution-log.worker'; | ||
|
||
import { WorkflowModule } from '../workflow.module'; | ||
|
||
let executionLogQueueService: ExecutionLogQueueService; | ||
let executionLogWorker: ExecutionLogWorker; | ||
|
||
describe('ExecutionLog Worker', () => { | ||
before(async () => { | ||
process.env.IN_MEMORY_CLUSTER_MODE_ENABLED = 'false'; | ||
process.env.IS_IN_MEMORY_CLUSTER_MODE_ENABLED = 'false'; | ||
|
||
const moduleRef = await Test.createTestingModule({ | ||
imports: [WorkflowModule], | ||
}).compile(); | ||
|
||
const createExecutionDetails = moduleRef.get<CreateExecutionDetails>(CreateExecutionDetails); | ||
|
||
executionLogWorker = new ExecutionLogWorker(createExecutionDetails); | ||
|
||
executionLogQueueService = new ExecutionLogQueueService(); | ||
await executionLogQueueService.queue.obliterate(); | ||
}); | ||
|
||
after(async () => { | ||
await executionLogQueueService.queue.drain(); | ||
await executionLogWorker.gracefulShutdown(); | ||
}); | ||
|
||
it('should be initialised properly', async () => { | ||
expect(executionLogWorker).to.be.ok; | ||
expect(executionLogWorker).to.have.all.keys('DEFAULT_ATTEMPTS', 'instance', 'topic', 'createExecutionDetails'); | ||
expect(await executionLogWorker.bullMqService.getStatus()).to.deep.equal({ | ||
queueIsPaused: undefined, | ||
queueName: undefined, | ||
workerName: 'execution-logs', | ||
workerIsPaused: false, | ||
workerIsRunning: true, | ||
}); | ||
expect(executionLogWorker.worker.opts).to.deep.include({ | ||
concurrency: 200, | ||
lockDuration: 90000, | ||
}); | ||
}); | ||
|
||
it('should be able to automatically pull a job from the queue', async () => { | ||
const existingJobs = await executionLogQueueService.queue.getJobs(); | ||
expect(existingJobs.length).to.equal(0); | ||
|
||
const jobId = 'execution-logs-queue-job-id'; | ||
const _environmentId = 'execution-logs-queue-environment-id'; | ||
const _organizationId = 'execution-logs-queue-organization-id'; | ||
const _userId = 'execution-logs-queue-user-id'; | ||
const jobData = { | ||
_id: jobId, | ||
test: 'execution-logs-queue-job-data', | ||
_environmentId, | ||
_organizationId, | ||
_userId, | ||
}; | ||
|
||
await executionLogQueueService.add(jobId, jobData, _organizationId); | ||
|
||
expect(await executionLogQueueService.queue.getActiveCount()).to.equal(1); | ||
expect(await executionLogQueueService.queue.getWaitingCount()).to.equal(0); | ||
|
||
// When we arrive to pull the job it has been already pulled by the worker | ||
const nextJob = await executionLogWorker.worker.getNextJob(jobId); | ||
expect(nextJob).to.equal(undefined); | ||
|
||
await setTimeout(100); | ||
|
||
// No jobs left in queue | ||
const queueJobs = await executionLogQueueService.queue.getJobs(); | ||
expect(queueJobs.length).to.equal(0); | ||
}); | ||
|
||
it('should pause the worker', async () => { | ||
const isPaused = await executionLogWorker.worker.isPaused(); | ||
expect(isPaused).to.equal(false); | ||
|
||
const runningStatus = await executionLogWorker.bullMqService.getStatus(); | ||
expect(runningStatus).to.deep.equal({ | ||
queueIsPaused: undefined, | ||
queueName: undefined, | ||
workerName: 'execution-logs', | ||
workerIsPaused: false, | ||
workerIsRunning: true, | ||
}); | ||
|
||
await executionLogWorker.pause(); | ||
|
||
const isNowPaused = await executionLogWorker.worker.isPaused(); | ||
expect(isNowPaused).to.equal(true); | ||
|
||
const runningStatusChanged = await executionLogWorker.bullMqService.getStatus(); | ||
expect(runningStatusChanged).to.deep.equal({ | ||
queueIsPaused: undefined, | ||
queueName: undefined, | ||
workerName: 'execution-logs', | ||
workerIsPaused: true, | ||
workerIsRunning: true, | ||
}); | ||
}); | ||
|
||
it('should resume the worker', async () => { | ||
await executionLogWorker.pause(); | ||
|
||
const isPaused = await executionLogWorker.worker.isPaused(); | ||
expect(isPaused).to.equal(true); | ||
|
||
const runningStatus = await executionLogWorker.bullMqService.getStatus(); | ||
expect(runningStatus).to.deep.equal({ | ||
queueIsPaused: undefined, | ||
queueName: undefined, | ||
workerName: 'execution-logs', | ||
workerIsPaused: true, | ||
workerIsRunning: true, | ||
}); | ||
|
||
await executionLogWorker.resume(); | ||
|
||
const isNowPaused = await executionLogWorker.worker.isPaused(); | ||
expect(isNowPaused).to.equal(false); | ||
|
||
const runningStatusChanged = await executionLogWorker.bullMqService.getStatus(); | ||
expect(runningStatusChanged).to.deep.equal({ | ||
queueIsPaused: undefined, | ||
queueName: undefined, | ||
workerName: 'execution-logs', | ||
workerIsPaused: false, | ||
workerIsRunning: true, | ||
}); | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
apps/worker/src/app/workflow/services/execution-log.worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
const nr = require('newrelic'); | ||
import { | ||
getExecutionLogWorkerOptions, | ||
INovuWorker, | ||
PinoLogger, | ||
storage, | ||
Store, | ||
ExecutionLogWorkerService, | ||
WorkerOptions, | ||
WorkerProcessor, | ||
CreateExecutionDetails, | ||
CreateExecutionDetailsCommand, | ||
} from '@novu/application-generic'; | ||
import { ObservabilityBackgroundTransactionEnum } from '@novu/shared'; | ||
const LOG_CONTEXT = 'ExecutionLogWorker'; | ||
|
||
@Injectable() | ||
export class ExecutionLogWorker extends ExecutionLogWorkerService implements INovuWorker { | ||
constructor(private createExecutionDetails: CreateExecutionDetails) { | ||
super(); | ||
this.initWorker(this.getWorkerProcessor(), this.getWorkerOptions()); | ||
} | ||
gracefulShutdown: () => Promise<void>; | ||
onModuleDestroy: () => Promise<void>; | ||
pause: () => Promise<void>; | ||
resume: () => Promise<void>; | ||
|
||
private getWorkerOptions(): WorkerOptions { | ||
return getExecutionLogWorkerOptions(); | ||
} | ||
|
||
private getWorkerProcessor(): WorkerProcessor { | ||
return async ({ data }: { data: CreateExecutionDetailsCommand }) => { | ||
return await new Promise(async (resolve, reject) => { | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
const _this = this; | ||
|
||
Logger.verbose(`Job ${data.jobId} is being inserted into execution details collection`, LOG_CONTEXT); | ||
|
||
nr.startBackgroundTransaction( | ||
ObservabilityBackgroundTransactionEnum.EXECUTION_LOG_QUEUE, | ||
'Trigger Engine', | ||
function () { | ||
const transaction = nr.getTransaction(); | ||
|
||
storage.run(new Store(PinoLogger.root), () => { | ||
_this.createExecutionDetails | ||
.execute(data) | ||
.then(resolve) | ||
.catch(reject) | ||
.finally(() => { | ||
transaction.end(); | ||
}); | ||
}); | ||
} | ||
); | ||
}); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.