Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): Cancel all jobs related to a transactionId from the 'cancel' method #4616

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .commitlintrc.json
Expand Up @@ -4,7 +4,7 @@
"subject-case": [
2,
"always",
["sentence-case", "start-case", "pascal-case", "upper-case", "lower-case"]
["sentence-case", "start-case", "pascal-case", "upper-case", "lower-case", "camel-case"]
],
"type-enum": [
2,
Expand Down
23 changes: 14 additions & 9 deletions apps/api/src/app/events/e2e/delay-events.e2e.ts
Expand Up @@ -24,13 +24,13 @@ describe('Trigger event - Delay triggered events - /v1/events/trigger (POST)', f
let standardQueueService: StandardQueueService;
const messageRepository = new MessageRepository();

const triggerEvent = async (payload, transactionId?: string, overrides = {}) => {
const triggerEvent = async (payload, transactionId?: string, overrides = {}, to = [subscriber.subscriberId]) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the ability to test more than 1 subscriber

await axiosInstance.post(
`${session.serverUrl}/v1/events/trigger`,
{
transactionId,
name: template.triggers[0].identifier,
to: [subscriber.subscriberId],
to,
payload,
overrides,
},
Expand Down Expand Up @@ -330,6 +330,8 @@ describe('Trigger event - Delay triggered events - /v1/events/trigger (POST)', f
});

it('should be able to cancel delay', async function () {
const secondSubscriber = await subscriberService.createSubscriber();

const id = MessageRepository.createObjectId();
template = await session.createTemplate({
steps: [
Expand All @@ -342,7 +344,7 @@ describe('Trigger event - Delay triggered events - /v1/events/trigger (POST)', f
content: '',
metadata: {
unit: DigestUnitEnum.SECONDS,
amount: 0.1,
amount: 5,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid flakiness in the test

type: DelayTypeEnum.REGULAR,
},
},
Expand All @@ -357,17 +359,19 @@ describe('Trigger event - Delay triggered events - /v1/events/trigger (POST)', f
{
customVar: 'Testing of User Name',
},
id
id,
{},
[subscriber.subscriberId, secondSubscriber.subscriberId]
);

await session.awaitRunningJobs(template?._id, true, 1);
await session.awaitRunningJobs(template?._id, true, 2);
await axiosInstance.delete(`${session.serverUrl}/v1/events/trigger/${id}`, {
headers: {
authorization: `ApiKey ${session.apiKey}`,
},
});

let delayedJob = await jobRepository.findOne({
let delayedJobs = await jobRepository.find({
_environmentId: session.environment._id,
_templateId: template._id,
type: StepTypeEnum.DELAY,
Expand All @@ -380,14 +384,15 @@ describe('Trigger event - Delay triggered events - /v1/events/trigger (POST)', f
transactionId: id,
});

expect(pendingJobs).to.equal(1);
expect(pendingJobs).to.equal(2);

delayedJob = await jobRepository.findOne({
delayedJobs = await jobRepository.find({
_environmentId: session.environment._id,
_templateId: template._id,
type: StepTypeEnum.DELAY,
transactionId: id,
});
expect(delayedJob!.status).to.equal(JobStatusEnum.CANCELED);
expect(delayedJobs[0]!.status).to.equal(JobStatusEnum.CANCELED);
expect(delayedJobs[1]!.status).to.equal(JobStatusEnum.CANCELED);
});
});
Expand Up @@ -7,20 +7,25 @@ export class CancelDelayed {
constructor(private jobRepository: JobRepository) {}

public async execute(command: CancelDelayedCommand): Promise<boolean> {
const job = await this.jobRepository.findOne({
_environmentId: command.environmentId,
transactionId: command.transactionId,
status: JobStatusEnum.DELAYED,
});
const jobs = await this.jobRepository.find(
{
_environmentId: command.environmentId,
transactionId: command.transactionId,
status: JobStatusEnum.DELAYED,
},
'_id'
);
Comment on lines +10 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will still find only the delay job - as its the only one that has DELAYED status. The others are pending. Or am I missing something?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind. Got you. its multiple subscribers


if (!job) {
if (!jobs?.length) {
return false;
}

await this.jobRepository.update(
{
_environmentId: command.environmentId,
_id: job._id,
_id: {
$in: jobs.map((job) => job._id),
},
},
{
$set: {
Expand Down
81 changes: 22 additions & 59 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.