|
| 1 | +import { Message, SQSClient } from '@aws-sdk/client-sqs'; |
| 2 | +import { Context, SQSEvent, SQSRecord } from 'aws-lambda'; |
| 3 | +import { Consumer } from 'sqs-consumer'; |
| 4 | + |
| 5 | +import { handler } from './index'; |
| 6 | +import { logger } from './util/logger'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Test the SQS consumer locally using LocalStack queues |
| 10 | + */ |
| 11 | +const sqsClient = new SQSClient({ region: 'eu-west-1', endpoint: 'http://localhost:4566' }); |
| 12 | + |
| 13 | +const queueUrl = 'http://localhost:4566/000000000000/linkedin-api-import-profile-local.fifo'; |
| 14 | + |
| 15 | +const consumer = Consumer.create({ |
| 16 | + queueUrl, |
| 17 | + handleMessage: async (message: Message) => { |
| 18 | + logger.info('👉 Message received:', message); |
| 19 | + |
| 20 | + try { |
| 21 | + const sqsRecord = { |
| 22 | + messageId: message.MessageId!, |
| 23 | + receiptHandle: message.ReceiptHandle!, |
| 24 | + body: message.Body!, |
| 25 | + attributes: message.Attributes || {}, |
| 26 | + messageAttributes: message.MessageAttributes || {}, |
| 27 | + md5OfBody: message.MD5OfBody || '', |
| 28 | + eventSource: 'aws:sqs', |
| 29 | + eventSourceARN: `arn:aws:sqs:eu-west-1:000000000000:linkedin-api-import-profile-local.fifo`, |
| 30 | + awsRegion: 'eu-west-1' |
| 31 | + } as unknown as SQSRecord; |
| 32 | + |
| 33 | + const event = { Records: [sqsRecord] } as SQSEvent; |
| 34 | + |
| 35 | + const response = await handler(event, {} as Context, () => { |
| 36 | + logger.info('✅ Executed handler'); |
| 37 | + }); |
| 38 | + |
| 39 | + return response; |
| 40 | + } catch (error) { |
| 41 | + logger.error('❌ Error handling message:', error); |
| 42 | + } |
| 43 | + }, |
| 44 | + sqs: sqsClient |
| 45 | +}); |
| 46 | + |
| 47 | +consumer.on('error', (err: Error) => { |
| 48 | + logger.error('❌ Consumer error:', err.message); |
| 49 | +}); |
| 50 | + |
| 51 | +consumer.on('processing_error', (err: Error) => { |
| 52 | + logger.error('❌ Processing error:', err.message); |
| 53 | +}); |
| 54 | + |
| 55 | +consumer.on('empty', () => { |
| 56 | + logger.info('🥱 Waiting for messages...'); |
| 57 | +}); |
| 58 | + |
| 59 | +logger.info(`🔄 Starting SQS consumer for ${queueUrl}...`); |
| 60 | +consumer.start(); |
0 commit comments