@punks/backend-entity-manager 0.0.471 → 0.0.473

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -41521,6 +41521,27 @@ const ensureJobLogGroup = async (logGroupName, awsSettings) => {
41521
41521
  }
41522
41522
  };
41523
41523
 
41524
+ const replaceVariablesInValue = (value, variables) => {
41525
+ // Example matches:
41526
+ // Input: "Hello ${name} and $(user)!" with variables {name: "World", user: "John"}
41527
+ // Output: "Hello World and John!"
41528
+ //
41529
+ // Input: "The ${animal} jumps over $(object)" with variables {animal: "fox", object: "fence"}
41530
+ // Output: "The fox jumps over fence"
41531
+ return value.replace(/[\$][\{\(]([^\}\)]+)[\}\)]/g, (match, p1) => variables[p1]);
41532
+ };
41533
+ const replaceVariable = (environmentValue, variables) => {
41534
+ return {
41535
+ name: environmentValue.name,
41536
+ value: environmentValue.value
41537
+ ? replaceVariablesInValue(environmentValue.value, variables)
41538
+ : environmentValue.value,
41539
+ };
41540
+ };
41541
+ const replaceVariables = (environment, variables) => {
41542
+ return environment.map((e) => replaceVariable(e, variables));
41543
+ };
41544
+
41524
41545
  var AwsBatchService_1;
41525
41546
  const jobDefinitionName = (jobUid) => `${awsBatchSettings.value.batchResourcesPrefix}-job-${jobUid}`;
41526
41547
  const jobInstanceName = (jobUid, instanceId) => `${awsBatchSettings.value.batchResourcesPrefix}-job-${jobUid}-${instanceId}`;
@@ -41563,22 +41584,42 @@ let AwsBatchService = AwsBatchService_1 = class AwsBatchService {
41563
41584
  this.logger.info(`AWS JOB -> submitting job triggered`, {
41564
41585
  input,
41565
41586
  });
41566
- const queue = await this.ensureQueue(jobQueueName(input.jobUid));
41567
41587
  const jobDefName = jobDefinitionName(input.jobUid);
41568
41588
  const jobDefinition = await this.getLatestJobDefinition(jobDefName);
41589
+ if (!jobDefinition) {
41590
+ throw new Error(`Job definition not found -> ${jobDefName}`);
41591
+ }
41592
+ const platformType = jobDefinition.platformCapabilities?.[0];
41593
+ if (!platformType) {
41594
+ throw new Error(`Platform type not found -> ${jobDefName}`);
41595
+ }
41596
+ const defaultComputeEnvironment = platformType === clientBatch.PlatformCapability.FARGATE
41597
+ ? this.awsSettings.defaultFargateBatchComputeEnvironment
41598
+ : this.awsSettings.defaultEc2BatchComputeEnvironment;
41599
+ const batchComputeEnvironment = input.overrides?.batchComputeEnvironment ?? defaultComputeEnvironment;
41600
+ if (!batchComputeEnvironment) {
41601
+ throw new Error(`Compute environment not found -> ${jobDefName}`);
41602
+ }
41603
+ const queue = await this.ensureQueue(jobQueueName(input.jobUid), {
41604
+ batchComputeEnvironment,
41605
+ });
41569
41606
  const jobName = jobInstanceName(input.jobUid, input.instanceId);
41570
41607
  const submitJobRequest = {
41571
41608
  jobQueue: queue.jobQueueArn,
41572
41609
  jobDefinition: jobDefinition.jobDefinitionArn,
41573
41610
  jobName,
41574
- ...(input.overrides?.command || jobDefinition.containerProperties?.command
41575
- ? {
41576
- containerOverrides: {
41611
+ containerOverrides: {
41612
+ ...(input.overrides?.command ||
41613
+ jobDefinition.containerProperties?.command
41614
+ ? {
41577
41615
  command: input.overrides?.command ??
41578
41616
  jobDefinition.containerProperties.command,
41579
- },
41580
- }
41581
- : {}),
41617
+ }
41618
+ : {}),
41619
+ environment: jobDefinition.containerProperties?.environment && input.variables
41620
+ ? replaceVariables(jobDefinition.containerProperties.environment, input.variables)
41621
+ : jobDefinition.containerProperties?.environment ?? [],
41622
+ },
41582
41623
  };
41583
41624
  this.logger.info(`AWS JOB -> invoking job ${jobName} ${queue.jobQueueArn}`, {
41584
41625
  request: submitJobRequest,
@@ -41692,18 +41733,34 @@ let AwsBatchService = AwsBatchService_1 = class AwsBatchService {
41692
41733
  }));
41693
41734
  return result.jobDefinitions ?? [];
41694
41735
  }
41695
- async ensureQueue(queueName) {
41736
+ async ensureQueue(queueName, options) {
41696
41737
  if (!queueName) {
41697
41738
  throw new Error(`Queue name not provided`);
41698
41739
  }
41699
41740
  this.logger.debug(`AWS JOB -> ensuring queue ${queueName}`);
41700
41741
  const queue = await this.getQueue(queueName);
41701
41742
  if (queue) {
41702
- this.logger.debug(`AWS JOB -> queue exists ${queueName}`);
41703
- return queue;
41743
+ if (queue.computeEnvironmentOrder?.[0].computeEnvironment ===
41744
+ options.batchComputeEnvironment) {
41745
+ this.logger.debug(`AWS JOB -> queue exists ${queueName}`);
41746
+ return queue;
41747
+ }
41748
+ this.logger.debug(`AWS JOB -> queue exists with different compute environment ${queueName}`, {
41749
+ queue,
41750
+ options,
41751
+ });
41752
+ await this.updateQueue(queueName, options);
41753
+ this.logger.debug(`AWS JOB -> queue updated ${queueName}`);
41754
+ // Wait for the queue to be active
41755
+ await backendCore.sleep(10000);
41756
+ const updatedQueue = await this.getQueue(queueName);
41757
+ if (!updatedQueue) {
41758
+ throw new Error(`Queue not updated -> ${queueName}`);
41759
+ }
41760
+ return updatedQueue;
41704
41761
  }
41705
41762
  this.logger.debug(`AWS JOB -> creating queue ${queueName}`);
41706
- await this.createQueue(queueName);
41763
+ await this.createQueue(queueName, options);
41707
41764
  this.logger.debug(`AWS JOB -> queue created ${queueName}`);
41708
41765
  // Wait for the queue to be active
41709
41766
  await backendCore.sleep(10000);
@@ -41713,8 +41770,9 @@ let AwsBatchService = AwsBatchService_1 = class AwsBatchService {
41713
41770
  }
41714
41771
  return currentQueue;
41715
41772
  }
41716
- async createQueue(queueName) {
41717
- if (!this.awsSettings.batchComputeEnvironment) {
41773
+ async createQueue(queueName, options) {
41774
+ if (!this.awsSettings.defaultFargateBatchComputeEnvironment &&
41775
+ !options?.batchComputeEnvironment) {
41718
41776
  throw new Error(`No compute environment defined`);
41719
41777
  }
41720
41778
  await this.client().send(new clientBatch.CreateJobQueueCommand({
@@ -41722,7 +41780,25 @@ let AwsBatchService = AwsBatchService_1 = class AwsBatchService {
41722
41780
  priority: 0,
41723
41781
  computeEnvironmentOrder: [
41724
41782
  {
41725
- computeEnvironment: this.awsSettings.batchComputeEnvironment,
41783
+ computeEnvironment: options?.batchComputeEnvironment ||
41784
+ this.awsSettings.defaultFargateBatchComputeEnvironment,
41785
+ order: 0,
41786
+ },
41787
+ ],
41788
+ }));
41789
+ }
41790
+ async updateQueue(queueName, options) {
41791
+ if (!this.awsSettings.defaultFargateBatchComputeEnvironment &&
41792
+ !options?.batchComputeEnvironment) {
41793
+ throw new Error(`No compute environment defined`);
41794
+ }
41795
+ await this.client().send(new clientBatch.UpdateJobQueueCommand({
41796
+ jobQueue: queueName,
41797
+ priority: 0,
41798
+ computeEnvironmentOrder: [
41799
+ {
41800
+ computeEnvironment: options?.batchComputeEnvironment ||
41801
+ this.awsSettings.defaultFargateBatchComputeEnvironment,
41726
41802
  order: 0,
41727
41803
  },
41728
41804
  ],
@@ -41767,12 +41843,15 @@ let AwsJobsProvider = class AwsJobsProvider {
41767
41843
  const { definition, schedule, instanceId, payload, commandPlaceholders } = input;
41768
41844
  const awsInvocationParams = schedule?.invocationOverrides ??
41769
41845
  definition.invocationParams;
41846
+ const awsInfrastructureParams = definition.infrastructureParams;
41770
41847
  await this.awsBatchService.submitJob({
41771
41848
  instanceId,
41772
41849
  jobUid: definition.uid,
41773
41850
  overrides: {
41774
41851
  command: awsInvocationParams.startCommand?.map((x) => replacePlaceholders(replacePayload(x, payload), commandPlaceholders)),
41852
+ batchComputeEnvironment: awsInfrastructureParams.batchComputeEnvironment,
41775
41853
  },
41854
+ variables: input.variables,
41776
41855
  });
41777
41856
  }
41778
41857
  async upsertJobDefinition(input) {