/*! * @license * Copyright Squiz Australia Pty Ltd. All Rights Reserved. */ import { SQSClient, SendMessageCommand } from '@aws-sdk/client-sqs'; import { inject, injectable } from 'inversify'; import { InjectTokens } from '../../constants/InjectTokens'; import { JobStartMessage, SendJobStartMessageParams } from './JobStartQueueService.defs'; @injectable() export class JobStartQueueService { public constructor( @inject(SQSClient) private readonly sqsClient: SQSClient, @inject(InjectTokens.JobStartQueueUrl) private readonly queueUrl: string, ) {} /** * Sends a job start message to the SQS queue. * Uses a standard queue (not FIFO) to support DelaySeconds for retry backoff. * Duplicate job starts are prevented by ECS RunTask clientToken. */ public async sendJobStartMessage(params: SendJobStartMessageParams): Promise { const message: JobStartMessage = { tenantId: params.tenantId, executionId: params.executionId, jobName: params.jobName, }; await this.sqsClient.send( new SendMessageCommand({ QueueUrl: this.queueUrl, MessageBody: JSON.stringify(message), DelaySeconds: params.delaySeconds ?? 0, }), ); console.log( `Sent job start message for execution ${params.executionId} (job: ${params.jobName}, tenant: ${params.tenantId}, delay: ${params.delaySeconds ?? 0}s)`, ); } }