@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.
@@ -1,3 +1,4 @@
1
+ import { BatchJobVariables } from "../../../plugins/jobs/aws-batch/manager/types";
1
2
  import { JobDefinition, JobSchedule } from "./definition";
2
3
  export interface JobDispatchInput {
3
4
  definition: JobDefinition<unknown, unknown>;
@@ -5,6 +6,7 @@ export interface JobDispatchInput {
5
6
  instanceId: string;
6
7
  payload?: unknown;
7
8
  commandPlaceholders?: Record<string, string>;
9
+ variables?: BatchJobVariables;
8
10
  }
9
11
  export declare enum JobProviderState {
10
12
  Initializing = "initializing",
@@ -0,0 +1,4 @@
1
+ import { KeyValuePair } from "@aws-sdk/client-batch";
2
+ import { BatchJobVariables } from "./types";
3
+ export declare const replaceVariable: (environmentValue: KeyValuePair, variables: BatchJobVariables) => KeyValuePair;
4
+ export declare const replaceVariables: (environment: KeyValuePair[], variables: BatchJobVariables) => KeyValuePair[];
@@ -12,6 +12,7 @@ export declare class AwsBatchService {
12
12
  private getActiveJobDefinitions;
13
13
  private ensureQueue;
14
14
  private createQueue;
15
+ private updateQueue;
15
16
  private getQueue;
16
17
  private client;
17
18
  private get awsSettings();
@@ -9,11 +9,14 @@ export type BathJobDefinition = {
9
9
  };
10
10
  export type BatchJobOverrides = {
11
11
  command?: string[];
12
+ batchComputeEnvironment?: string;
12
13
  };
14
+ export type BatchJobVariables = Record<string, string>;
13
15
  export type SubmitBatchJobInput = {
14
16
  overrides?: BatchJobOverrides;
15
17
  jobUid: string;
16
18
  instanceId: string;
19
+ variables?: BatchJobVariables;
17
20
  };
18
21
  export type GetBatchJobStatusInput = {
19
22
  jobUid: string;
@@ -24,3 +27,6 @@ export type AwsJobDefinition = {
24
27
  infrastructureParams: AwsBatchInfrastructureParams;
25
28
  invocationParams: AwsBatchInvocationParams;
26
29
  };
30
+ export type AwsBatchCreateQueueOptions = {
31
+ batchComputeEnvironment: string;
32
+ };
@@ -14,6 +14,7 @@ export type AwsBatchInfrastructureParams = {
14
14
  assignPublicIp?: boolean;
15
15
  logGroupName?: string;
16
16
  privileged?: boolean;
17
+ batchComputeEnvironment?: string;
17
18
  };
18
19
  export type AwsBatchInvocationParams = {
19
20
  startCommand: string[];
@@ -3,7 +3,8 @@ export type AwsBatchSettings = {
3
3
  awsAccessKeyId?: string;
4
4
  awsSecretAccessKey?: string;
5
5
  region?: string;
6
- batchComputeEnvironment: string;
6
+ defaultFargateBatchComputeEnvironment: string;
7
+ defaultEc2BatchComputeEnvironment?: string;
7
8
  batchExecutionRole: string;
8
9
  batchResourcesPrefix: string;
9
10
  };
package/dist/esm/index.js CHANGED
@@ -20,7 +20,7 @@ import { ListObjectsCommand, PutObjectCommand, GetObjectCommand, DeleteObjectCom
20
20
  import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
21
21
  import { SendEmailCommand, SESClient } from '@aws-sdk/client-ses';
22
22
  import { MailService } from '@sendgrid/mail';
23
- import { BatchClient, CancelJobCommand, ListJobsCommand, SubmitJobCommand, DeregisterJobDefinitionCommand, RegisterJobDefinitionCommand, JobDefinitionType, PlatformCapability, ResourceType, AssignPublicIp, DescribeJobDefinitionsCommand, CreateJobQueueCommand, DescribeJobQueuesCommand, JobStatus as JobStatus$1 } from '@aws-sdk/client-batch';
23
+ import { BatchClient, CancelJobCommand, ListJobsCommand, PlatformCapability, SubmitJobCommand, DeregisterJobDefinitionCommand, RegisterJobDefinitionCommand, JobDefinitionType, ResourceType, AssignPublicIp, DescribeJobDefinitionsCommand, CreateJobQueueCommand, UpdateJobQueueCommand, DescribeJobQueuesCommand, JobStatus as JobStatus$1 } from '@aws-sdk/client-batch';
24
24
  import { CloudWatchLogsClient, DescribeLogGroupsCommand, CreateLogGroupCommand } from '@aws-sdk/client-cloudwatch-logs';
25
25
  import require$$1$1 from 'http';
26
26
  import require$$2 from 'https';
@@ -41506,6 +41506,27 @@ const ensureJobLogGroup = async (logGroupName, awsSettings) => {
41506
41506
  }
41507
41507
  };
41508
41508
 
41509
+ const replaceVariablesInValue = (value, variables) => {
41510
+ // Example matches:
41511
+ // Input: "Hello ${name} and $(user)!" with variables {name: "World", user: "John"}
41512
+ // Output: "Hello World and John!"
41513
+ //
41514
+ // Input: "The ${animal} jumps over $(object)" with variables {animal: "fox", object: "fence"}
41515
+ // Output: "The fox jumps over fence"
41516
+ return value.replace(/[\$][\{\(]([^\}\)]+)[\}\)]/g, (match, p1) => variables[p1]);
41517
+ };
41518
+ const replaceVariable = (environmentValue, variables) => {
41519
+ return {
41520
+ name: environmentValue.name,
41521
+ value: environmentValue.value
41522
+ ? replaceVariablesInValue(environmentValue.value, variables)
41523
+ : environmentValue.value,
41524
+ };
41525
+ };
41526
+ const replaceVariables = (environment, variables) => {
41527
+ return environment.map((e) => replaceVariable(e, variables));
41528
+ };
41529
+
41509
41530
  var AwsBatchService_1;
41510
41531
  const jobDefinitionName = (jobUid) => `${awsBatchSettings.value.batchResourcesPrefix}-job-${jobUid}`;
41511
41532
  const jobInstanceName = (jobUid, instanceId) => `${awsBatchSettings.value.batchResourcesPrefix}-job-${jobUid}-${instanceId}`;
@@ -41548,22 +41569,42 @@ let AwsBatchService = AwsBatchService_1 = class AwsBatchService {
41548
41569
  this.logger.info(`AWS JOB -> submitting job triggered`, {
41549
41570
  input,
41550
41571
  });
41551
- const queue = await this.ensureQueue(jobQueueName(input.jobUid));
41552
41572
  const jobDefName = jobDefinitionName(input.jobUid);
41553
41573
  const jobDefinition = await this.getLatestJobDefinition(jobDefName);
41574
+ if (!jobDefinition) {
41575
+ throw new Error(`Job definition not found -> ${jobDefName}`);
41576
+ }
41577
+ const platformType = jobDefinition.platformCapabilities?.[0];
41578
+ if (!platformType) {
41579
+ throw new Error(`Platform type not found -> ${jobDefName}`);
41580
+ }
41581
+ const defaultComputeEnvironment = platformType === PlatformCapability.FARGATE
41582
+ ? this.awsSettings.defaultFargateBatchComputeEnvironment
41583
+ : this.awsSettings.defaultEc2BatchComputeEnvironment;
41584
+ const batchComputeEnvironment = input.overrides?.batchComputeEnvironment ?? defaultComputeEnvironment;
41585
+ if (!batchComputeEnvironment) {
41586
+ throw new Error(`Compute environment not found -> ${jobDefName}`);
41587
+ }
41588
+ const queue = await this.ensureQueue(jobQueueName(input.jobUid), {
41589
+ batchComputeEnvironment,
41590
+ });
41554
41591
  const jobName = jobInstanceName(input.jobUid, input.instanceId);
41555
41592
  const submitJobRequest = {
41556
41593
  jobQueue: queue.jobQueueArn,
41557
41594
  jobDefinition: jobDefinition.jobDefinitionArn,
41558
41595
  jobName,
41559
- ...(input.overrides?.command || jobDefinition.containerProperties?.command
41560
- ? {
41561
- containerOverrides: {
41596
+ containerOverrides: {
41597
+ ...(input.overrides?.command ||
41598
+ jobDefinition.containerProperties?.command
41599
+ ? {
41562
41600
  command: input.overrides?.command ??
41563
41601
  jobDefinition.containerProperties.command,
41564
- },
41565
- }
41566
- : {}),
41602
+ }
41603
+ : {}),
41604
+ environment: jobDefinition.containerProperties?.environment && input.variables
41605
+ ? replaceVariables(jobDefinition.containerProperties.environment, input.variables)
41606
+ : jobDefinition.containerProperties?.environment ?? [],
41607
+ },
41567
41608
  };
41568
41609
  this.logger.info(`AWS JOB -> invoking job ${jobName} ${queue.jobQueueArn}`, {
41569
41610
  request: submitJobRequest,
@@ -41677,18 +41718,34 @@ let AwsBatchService = AwsBatchService_1 = class AwsBatchService {
41677
41718
  }));
41678
41719
  return result.jobDefinitions ?? [];
41679
41720
  }
41680
- async ensureQueue(queueName) {
41721
+ async ensureQueue(queueName, options) {
41681
41722
  if (!queueName) {
41682
41723
  throw new Error(`Queue name not provided`);
41683
41724
  }
41684
41725
  this.logger.debug(`AWS JOB -> ensuring queue ${queueName}`);
41685
41726
  const queue = await this.getQueue(queueName);
41686
41727
  if (queue) {
41687
- this.logger.debug(`AWS JOB -> queue exists ${queueName}`);
41688
- return queue;
41728
+ if (queue.computeEnvironmentOrder?.[0].computeEnvironment ===
41729
+ options.batchComputeEnvironment) {
41730
+ this.logger.debug(`AWS JOB -> queue exists ${queueName}`);
41731
+ return queue;
41732
+ }
41733
+ this.logger.debug(`AWS JOB -> queue exists with different compute environment ${queueName}`, {
41734
+ queue,
41735
+ options,
41736
+ });
41737
+ await this.updateQueue(queueName, options);
41738
+ this.logger.debug(`AWS JOB -> queue updated ${queueName}`);
41739
+ // Wait for the queue to be active
41740
+ await sleep(10000);
41741
+ const updatedQueue = await this.getQueue(queueName);
41742
+ if (!updatedQueue) {
41743
+ throw new Error(`Queue not updated -> ${queueName}`);
41744
+ }
41745
+ return updatedQueue;
41689
41746
  }
41690
41747
  this.logger.debug(`AWS JOB -> creating queue ${queueName}`);
41691
- await this.createQueue(queueName);
41748
+ await this.createQueue(queueName, options);
41692
41749
  this.logger.debug(`AWS JOB -> queue created ${queueName}`);
41693
41750
  // Wait for the queue to be active
41694
41751
  await sleep(10000);
@@ -41698,8 +41755,9 @@ let AwsBatchService = AwsBatchService_1 = class AwsBatchService {
41698
41755
  }
41699
41756
  return currentQueue;
41700
41757
  }
41701
- async createQueue(queueName) {
41702
- if (!this.awsSettings.batchComputeEnvironment) {
41758
+ async createQueue(queueName, options) {
41759
+ if (!this.awsSettings.defaultFargateBatchComputeEnvironment &&
41760
+ !options?.batchComputeEnvironment) {
41703
41761
  throw new Error(`No compute environment defined`);
41704
41762
  }
41705
41763
  await this.client().send(new CreateJobQueueCommand({
@@ -41707,7 +41765,25 @@ let AwsBatchService = AwsBatchService_1 = class AwsBatchService {
41707
41765
  priority: 0,
41708
41766
  computeEnvironmentOrder: [
41709
41767
  {
41710
- computeEnvironment: this.awsSettings.batchComputeEnvironment,
41768
+ computeEnvironment: options?.batchComputeEnvironment ||
41769
+ this.awsSettings.defaultFargateBatchComputeEnvironment,
41770
+ order: 0,
41771
+ },
41772
+ ],
41773
+ }));
41774
+ }
41775
+ async updateQueue(queueName, options) {
41776
+ if (!this.awsSettings.defaultFargateBatchComputeEnvironment &&
41777
+ !options?.batchComputeEnvironment) {
41778
+ throw new Error(`No compute environment defined`);
41779
+ }
41780
+ await this.client().send(new UpdateJobQueueCommand({
41781
+ jobQueue: queueName,
41782
+ priority: 0,
41783
+ computeEnvironmentOrder: [
41784
+ {
41785
+ computeEnvironment: options?.batchComputeEnvironment ||
41786
+ this.awsSettings.defaultFargateBatchComputeEnvironment,
41711
41787
  order: 0,
41712
41788
  },
41713
41789
  ],
@@ -41752,12 +41828,15 @@ let AwsJobsProvider = class AwsJobsProvider {
41752
41828
  const { definition, schedule, instanceId, payload, commandPlaceholders } = input;
41753
41829
  const awsInvocationParams = schedule?.invocationOverrides ??
41754
41830
  definition.invocationParams;
41831
+ const awsInfrastructureParams = definition.infrastructureParams;
41755
41832
  await this.awsBatchService.submitJob({
41756
41833
  instanceId,
41757
41834
  jobUid: definition.uid,
41758
41835
  overrides: {
41759
41836
  command: awsInvocationParams.startCommand?.map((x) => replacePlaceholders(replacePayload(x, payload), commandPlaceholders)),
41837
+ batchComputeEnvironment: awsInfrastructureParams.batchComputeEnvironment,
41760
41838
  },
41839
+ variables: input.variables,
41761
41840
  });
41762
41841
  }
41763
41842
  async upsertJobDefinition(input) {