Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions apps/api/src/app/events/e2e/throttle-events.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ describe('Trigger event - Throttle triggered events - /v1/events/trigger (POST)
const completedThrottleJobs = throttleJobs.filter((job) => job.status === JobStatusEnum.COMPLETED);
expect(completedThrottleJobs?.length).to.equal(2);

completedThrottleJobs.forEach((job) => {
expect(job.stepOutput).to.be.ok;
expect(job.stepOutput?.throttled).to.equal(false);
expect(job.stepOutput?.threshold).to.equal(3);
expect(job.stepOutput?.executionCount).to.be.a('number');
});

// Both in-app messages should be created
const messages = await messageRepository.find({
_environmentId: session.environment._id,
Expand Down Expand Up @@ -171,13 +178,21 @@ describe('Trigger event - Throttle triggered events - /v1/events/trigger (POST)
expect(completedThrottleJobs?.length).to.equal(2);
expect(skippedThrottleJobs?.length).to.equal(1);

// Check throttle result in skipped job
const skippedJob = skippedThrottleJobs[0];
expect(skippedJob.stepOutput).to.be.ok;
expect(skippedJob.stepOutput?.throttled).to.equal(true);
expect(skippedJob.stepOutput?.threshold).to.equal(2);
// The execution count should be at least the threshold (2) when throttled
expect(skippedJob.stepOutput?.executionCount).to.be.at.least(2);
completedThrottleJobs.forEach((job) => {
expect(job.stepOutput).to.be.ok;
expect(job.stepOutput?.throttled).to.equal(false);
expect(job.stepOutput?.threshold).to.equal(2);
expect(job.stepOutput?.executionCount).to.be.a('number');
});

// Check throttle result in skipped jobs
skippedThrottleJobs.forEach((job) => {
expect(job.stepOutput).to.be.ok;
expect(job.stepOutput?.throttled).to.equal(true);
expect(job.stepOutput?.threshold).to.equal(2);
// The execution count should be at least the threshold (2) when throttled
expect(job.stepOutput?.executionCount).to.be.at.least(2);
});

// Only 2 in-app messages should be created
const messages = await messageRepository.find({
Expand Down Expand Up @@ -255,12 +270,19 @@ describe('Trigger event - Throttle triggered events - /v1/events/trigger (POST)
expect(completedThrottleJobs?.length).to.equal(1);
expect(skippedThrottleJobs?.length).to.equal(19);

completedThrottleJobs.forEach((job) => {
expect(job.stepOutput).to.be.ok;
expect(job.stepOutput?.throttled).to.equal(false);
expect(job.stepOutput?.threshold).to.equal(1);
expect(job.stepOutput?.executionCount).to.equal(1);
});

// Verify throttle results in skipped jobs
for (const job of skippedThrottleJobs) {
skippedThrottleJobs.forEach((job) => {
expect(job.stepOutput).to.be.ok;
expect(job.stepOutput?.throttled).to.equal(true);
expect(job.stepOutput?.threshold).to.equal(1);
}
});

// Only 1 in-app message should be created
const messages = await messageRepository.find({
Expand Down Expand Up @@ -332,6 +354,19 @@ describe('Trigger event - Throttle triggered events - /v1/events/trigger (POST)
expect(completedThrottleJobs?.length).to.equal(2);
expect(skippedThrottleJobs?.length).to.equal(1);

completedThrottleJobs.forEach((job) => {
expect(job.stepOutput).to.be.ok;
expect(job.stepOutput?.throttled).to.equal(false);
expect(job.stepOutput?.threshold).to.equal(1);
expect(job.stepOutput?.executionCount).to.equal(1);
});

skippedThrottleJobs.forEach((job) => {
expect(job.stepOutput).to.be.ok;
expect(job.stepOutput?.throttled).to.equal(true);
expect(job.stepOutput?.threshold).to.equal(1);
});

// Check messages created
const messages = await messageRepository.find({
_environmentId: session.environment._id,
Expand Down
13 changes: 13 additions & 0 deletions apps/worker/src/app/workflow/usecases/add-job/add-job.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,19 @@ export class AddJob {
workflowStatus: WorkflowRunStatusEnum.COMPLETED,
deliveryLifecycleStatus: DeliveryLifecycleStatusEnum.SKIPPED,
};
} else {
await this.jobRepository.updateOne(
{ _id: job._id, _environmentId: command.environmentId },
{
$set: {
stepOutput: {
throttled: false,
executionCount: throttleResult.executionCount,
threshold: throttleResult.threshold,
},
},
}
);
}
} catch (error) {
return await this.handleStepValidationError(
Expand Down
Loading