@@ -13,7 +13,6 @@ import {PostDatedNotifyDataItem} from "@psu-common/commonTypes"
1313import { BatchProcessingResult , PostDatedSQSMessage , ReceivedPostDatedSQSResult } from "./types"
1414
1515const sqs = new SQSClient ( { region : process . env . AWS_REGION } )
16- const FAILED_MESSAGE_VISIBILITY_TIMEOUT = 300 // 5 minutes in seconds
1716
1817/**
1918 * Get the SQS queue URL from environment variables.
@@ -210,34 +209,39 @@ export async function removeSQSMessages(
210209}
211210
212211/**
213- * Return failed messages to the queue with a visibility timeout.
212+ * Edit failed that are on the queue to update their visibility timeout.
214213 * This makes the messages invisible for the specified duration before they can be processed again.
214+ * This does not delete the messages, or post new ones; it only alters their visibility.
215215 *
216216 * @param logger - The logging object
217217 * @param messages - The messages that failed processing and should be returned to the queue
218- * @param visibilityTimeoutSeconds - The time in seconds before messages become visible again (default: 300 = 5 minutes)
219218 */
220219export async function returnMessagesToQueue (
221220 logger : Logger ,
222- messages : Array < Message > ,
223- visibilityTimeoutSeconds = 300
221+ messages : Array < Message >
224222) : Promise < void > {
225223 if ( messages . length === 0 ) {
226- logger . info ( "No failed messages to return to queue" )
224+ logger . info ( "No messages to return to queue" )
227225 return
228226 }
229227
230228 const sqsUrl = getQueueUrl ( logger )
231229
230+ // TODO: Each message needs to have an appropriate visibility timeout based on when it is due to be retried.
231+ // For now, use a fixed 5 minute timeout for all messages.
232+ const visibilityTimeoutSeconds = 300
232233 const entries = messages . map ( ( m ) => ( {
233234 Id : m . MessageId ! ,
234235 ReceiptHandle : m . ReceiptHandle ! ,
235236 VisibilityTimeout : visibilityTimeoutSeconds
236237 } ) )
237238
238239 logger . info (
239- `Returning messages to queue with ${ visibilityTimeoutSeconds } s timeout` ,
240- { numberOfMessages : entries . length , messageIds : entries . map ( ( e ) => e . Id ) }
240+ `Returning messages to queue with timeouts` ,
241+ {
242+ numberOfMessages : entries . length ,
243+ idAndTimeouts : entries . map ( ( e ) => ( { id : e . Id , visibilityTimeout : e . VisibilityTimeout } ) )
244+ }
241245 )
242246
243247 const changeVisibilityCmd = new ChangeMessageVisibilityBatchCommand ( {
@@ -258,8 +262,8 @@ export async function returnMessagesToQueue(
258262
259263/**
260264 * Handle the results of message processing:
261- * - Delete successful messages from the queue
262- * - Return failed messages to the queue with a visibility timeout
265+ * - Delete matured messages from the queue
266+ * - Return immature messages to the queue with a visibility timeout
263267 * Does not alter the input result object, only performs side effects.
264268 *
265269 * @param result - The batch processing result
@@ -269,15 +273,16 @@ export async function handleProcessedMessages(
269273 result : BatchProcessingResult ,
270274 logger : Logger
271275) : Promise < void > {
272- const { successful , failed } = result
276+ const { maturedPrescriptionUpdates , immaturePrescriptionUpdates } = result
273277
274- // Delete successful messages
275- if ( successful . length > 0 ) {
276- await removeSQSMessages ( logger , successful )
278+ // Delete matured messages
279+ if ( maturedPrescriptionUpdates . length > 0 ) {
280+ // TODO: Also need to send messages to the notification queue here (do that first, then delete)
281+ await removeSQSMessages ( logger , maturedPrescriptionUpdates )
277282 }
278283
279- // Return failed messages to the queue with a 5 minute timeout
280- if ( failed . length > 0 ) {
281- await returnMessagesToQueue ( logger , failed , FAILED_MESSAGE_VISIBILITY_TIMEOUT )
284+ // Return failed messages to the queue
285+ if ( immaturePrescriptionUpdates . length > 0 ) {
286+ await returnMessagesToQueue ( logger , immaturePrescriptionUpdates )
282287 }
283288}
0 commit comments