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

Allow consumers stopped with abort: true to be restarted #429

Merged
merged 3 commits into from
Oct 16, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
isConnectionError
} from './errors';
import { validateOption, assertOptions, hasMessages } from './validation';
import { abortController } from './controllers';
import { logger } from './logger';

/**
Expand All @@ -50,6 +49,7 @@ export class Consumer extends TypedEventEmitter {
private authenticationErrorTimeout: number;
private pollingWaitTimeMs: number;
private heartbeatInterval: number;
public abortController: AbortController;

constructor(options: ConsumerOptions) {
super();
Expand Down Expand Up @@ -90,13 +90,26 @@ export class Consumer extends TypedEventEmitter {
*/
public start(): void {
if (this.stopped) {
// Create a new abort controller each time the consumer is started
this.abortController = new AbortController();
logger.debug('starting');
this.stopped = false;
this.emit('started');
this.poll();
}
}

/**
* A reusable options object for sqs.send that's used to avoid duplication.
*/
private get sqsSendOptions(): { abortSignal: AbortSignal } {
return {
// return the current abortController signal or a fresh signal that has not been aborted.
// This effectively defaults the signal sent to the AWS SDK to not aborted
abortSignal: this.abortController?.signal || new AbortController().signal
};
}

/**
* Stop polling the queue for messages (pre existing requests will still be made until concluded).
*/
Expand All @@ -116,7 +129,7 @@ export class Consumer extends TypedEventEmitter {

if (options?.abort) {
logger.debug('aborting');
abortController.abort();
this.abortController.abort();
this.emit('aborted');
}

Expand Down Expand Up @@ -163,13 +176,6 @@ export class Consumer extends TypedEventEmitter {
}
}

/**
* A reusable options object for sqs.send that's used to avoid duplication.
*/
private sqsSendOptions = {
abortSignal: abortController.signal
};

/**
* Poll for new messages from SQS
*/
Expand Down
1 change: 0 additions & 1 deletion src/controllers.ts

This file was deleted.

30 changes: 27 additions & 3 deletions test/tests/consumer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import * as pEvent from 'p-event';

import { AWSError } from '../../src/types';
import { Consumer } from '../../src/consumer';
import { abortController } from '../../src/controllers';
import { logger } from '../../src/logger';

const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -168,6 +167,32 @@ describe('Consumer', () => {
});

describe('.start', () => {
it('uses the correct abort signal', async () => {
sqs.send
.withArgs(mockReceiveMessage)
.resolves(new Promise((res) => setTimeout(res, 100)));

// Starts and abort is false
consumer.start();
assert.isFalse(sqs.send.lastCall.lastArg.abortSignal.aborted);

// normal stop without an abort and abort is false
consumer.stop();
assert.isFalse(sqs.send.lastCall.lastArg.abortSignal.aborted);

// Starts and abort is false
consumer.start();
assert.isFalse(sqs.send.lastCall.lastArg.abortSignal.aborted);

// Stop with abort and abort is true
consumer.stop({ abort: true });
assert.isTrue(sqs.send.lastCall.lastArg.abortSignal.aborted);

// Starts and abort is false
nicholasgriffintn marked this conversation as resolved.
Show resolved Hide resolved
consumer.start();
assert.isFalse(sqs.send.lastCall.lastArg.abortSignal.aborted);
});

it('fires an event when the consumer is started', async () => {
const handleStart = sandbox.stub().returns(null);

Expand Down Expand Up @@ -1303,7 +1328,6 @@ describe('Consumer', () => {
it('aborts requests when the abort param is true', async () => {
const handleStop = sandbox.stub().returns(null);
const handleAbort = sandbox.stub().returns(null);
const abortControllerAbort = sandbox.stub(abortController, 'abort');

consumer.on('stopped', handleStop);
consumer.on('aborted', handleAbort);
Expand All @@ -1313,8 +1337,8 @@ describe('Consumer', () => {

await clock.runAllAsync();

assert.isTrue(consumer.abortController.signal.aborted);
sandbox.assert.calledOnce(handleMessage);
sandbox.assert.calledOnce(abortControllerAbort);
sandbox.assert.calledOnce(handleAbort);
sandbox.assert.calledOnce(handleStop);
});
Expand Down
Loading