@rlyle1179/serverless-aws-batch 1.1.19 → 1.1.21

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/lib/schedule.js CHANGED
@@ -1,91 +1,100 @@
1
- // This file replaces the lambda function code and can be used to schedule the batch task.
2
- // Requires the following environment variables:
3
- // - EVENT_LOGGING_ENABLED
4
- // - JOB_DEFINITION_ARN
5
- // - FUNCTION_NAME
6
- // - JOB_QUEUE_ARN
7
-
8
- 'use strict';
9
- const process = require("process");
10
- const { BatchClient, SubmitJobCommand } = require('@aws-sdk/client-batch')
11
- const batch = new BatchClient();
12
-
13
- function zeroPad(n) {
14
- return n <= 9 ? "0" + n : n;
15
- }
16
-
17
- function getFormattedNow() {
18
- let currentDate = new Date();
19
- return currentDate.getFullYear()
20
- + "-" + zeroPad(currentDate.getMonth() + 1)
21
- + "-" + zeroPad(currentDate.getDate())
22
- + "-" + zeroPad(currentDate.getHours())
23
- + "-" + zeroPad(currentDate.getMinutes())
24
- + "-" + zeroPad(currentDate.getSeconds());
25
- }
26
-
27
- module.exports.schedule = (event, context, callback) => {
28
-
29
- if (process.env.EVENT_LOGGING_ENABLED === 'true') {
30
- console.log(`Received event: ${JSON.stringify(event, null, 2)}`);
31
- }
32
-
33
- const jobDefinition = process.env.JOB_DEFINITION_ARN;
34
- const jobName = `${process.env.FUNCTION_NAME}-${getFormattedNow()}`;
35
- const jobQueue = process.env.JOB_QUEUE_ARN;
36
-
37
- console.log(`Submitting job: ${jobName} with jobDefinition: ${jobDefinition} to queue: ${jobQueue}`);
38
-
39
- // delete headers from the event, we are exceeding the 8092 limit for container overrides
40
- delete event.headers;
41
- delete event.multiValueHeaders;
42
-
43
- let params = {
44
- jobDefinition: jobDefinition,
45
- jobName: jobName,
46
- jobQueue: jobQueue,
47
- parameters: {
48
- event: JSON.stringify(event)
49
- },
50
- containerOverrides: {
51
- environment: [
52
- { name: "AWS_LAMBDA_FUNCTION_NAME", value: process.env.AWS_LAMBDA_FUNCTION_NAME },
53
- { name: "AWS_LAMBDA_FUNCTION_VERSION", value: process.env.AWS_LAMBDA_FUNCTION_VERSION },
54
- { name: "AWS_REQUEST_ID", value: context.awsRequestId }
55
- ]
56
- }
57
- };
58
-
59
- batch.send( new SubmitJobCommand(params), function(err, data) {
60
- let response;
61
-
62
- const jsonHeaders = {
63
- 'Content-Type': 'application/json',
64
- 'Access-Control-Allow-Origin': '*', // Required for CORS support to work
65
- 'Access-Control-Allow-Credentials': true // Required for cookies, authorization headers with HTTPS
66
- }
67
-
68
- if (err) {
69
- console.log(`Error submitting job: ${err}`, err.stack);
70
- response = {
71
- statusCode: 500,
72
- headers: jsonHeaders,
73
- body: JSON.stringify({
74
- 'error': err
75
- })
76
- }
77
- }
78
- else {
79
- console.log(`Submitted job: ${JSON.stringify(data, null, 2)}`);
80
- response = {
81
- statusCode: 200,
82
- headers: jsonHeaders,
83
- body: JSON.stringify({
84
- data
85
- })
86
- }
87
- }
88
-
89
- callback(null, response);
90
- });
1
+ // This file replaces the lambda function code and can be used to schedule the batch task.
2
+ // Requires the following environment variables:
3
+ // - EVENT_LOGGING_ENABLED
4
+ // - JOB_DEFINITION_ARN
5
+ // - FUNCTION_NAME
6
+ // - JOB_QUEUE_ARN
7
+
8
+ 'use strict';
9
+ const process = require("process");
10
+ const { BatchClient, SubmitJobCommand } = require('@aws-sdk/client-batch')
11
+ const batch = new BatchClient();
12
+
13
+ function zeroPad(n) {
14
+ return n <= 9 ? "0" + n : n;
15
+ }
16
+
17
+ function getFormattedNow() {
18
+ let currentDate = new Date();
19
+ return currentDate.getFullYear()
20
+ + "-" + zeroPad(currentDate.getMonth() + 1)
21
+ + "-" + zeroPad(currentDate.getDate())
22
+ + "-" + zeroPad(currentDate.getHours())
23
+ + "-" + zeroPad(currentDate.getMinutes())
24
+ + "-" + zeroPad(currentDate.getSeconds());
25
+ }
26
+
27
+ // JOB_DEFINITION_ARN is baked at deploy time as the full ARN including :revision. Reduce it to the
28
+ // bare job-definition name so SubmitJob resolves the latest ACTIVE revision: a deploy can leave the
29
+ // env pinned to a revision it just deregistered, which fails with "is not in ACTIVE status".
30
+ function toJobDefinitionName(arn) {
31
+ if (! arn) return arn;
32
+ const name = arn.includes('/') ? arn.slice(arn.lastIndexOf('/') + 1) : arn;
33
+ return name.replace(/:\d+$/, '');
34
+ }
35
+
36
+ module.exports.schedule = (event, context, callback) => {
37
+
38
+ if (process.env.EVENT_LOGGING_ENABLED === 'true') {
39
+ console.log(`Received event: ${JSON.stringify(event, null, 2)}`);
40
+ }
41
+
42
+ const jobDefinition = toJobDefinitionName(process.env.JOB_DEFINITION_ARN);
43
+ const jobName = `${process.env.FUNCTION_NAME}-${getFormattedNow()}`;
44
+ const jobQueue = process.env.JOB_QUEUE_ARN;
45
+
46
+ console.log(`Submitting job: ${jobName} with jobDefinition: ${jobDefinition} to queue: ${jobQueue}`);
47
+
48
+ // delete headers from the event, we are exceeding the 8092 limit for container overrides
49
+ delete event.headers;
50
+ delete event.multiValueHeaders;
51
+
52
+ let params = {
53
+ jobDefinition: jobDefinition,
54
+ jobName: jobName,
55
+ jobQueue: jobQueue,
56
+ parameters: {
57
+ event: JSON.stringify(event)
58
+ },
59
+ containerOverrides: {
60
+ environment: [
61
+ { name: "AWS_LAMBDA_FUNCTION_NAME", value: process.env.AWS_LAMBDA_FUNCTION_NAME },
62
+ { name: "AWS_LAMBDA_FUNCTION_VERSION", value: process.env.AWS_LAMBDA_FUNCTION_VERSION },
63
+ { name: "AWS_REQUEST_ID", value: context.awsRequestId }
64
+ ]
65
+ }
66
+ };
67
+
68
+ batch.send( new SubmitJobCommand(params), function(err, data) {
69
+ let response;
70
+
71
+ const jsonHeaders = {
72
+ 'Content-Type': 'application/json',
73
+ 'Access-Control-Allow-Origin': '*', // Required for CORS support to work
74
+ 'Access-Control-Allow-Credentials': true // Required for cookies, authorization headers with HTTPS
75
+ }
76
+
77
+ if (err) {
78
+ console.log(`Error submitting job: ${err}`, err.stack);
79
+ response = {
80
+ statusCode: 500,
81
+ headers: jsonHeaders,
82
+ body: JSON.stringify({
83
+ 'error': err
84
+ })
85
+ }
86
+ }
87
+ else {
88
+ console.log(`Submitted job: ${JSON.stringify(data, null, 2)}`);
89
+ response = {
90
+ statusCode: 200,
91
+ headers: jsonHeaders,
92
+ body: JSON.stringify({
93
+ data
94
+ })
95
+ }
96
+ }
97
+
98
+ callback(null, response);
99
+ });
91
100
  };
package/package.json CHANGED
@@ -1,35 +1,35 @@
1
- {
2
- "name": "@rlyle1179/serverless-aws-batch",
3
- "version": "1.1.19",
4
- "description": "Serverless Framework Plugin for interacting with AWS Batch",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/rlyle/serverless-aws-batch.git"
12
- },
13
- "keywords": [
14
- "serverless",
15
- "aws",
16
- "batch",
17
- "python"
18
- ],
19
- "author": "Justin Ramsey",
20
- "license": "MIT",
21
- "bugs": {
22
- "url": "https://github.com/rlyle/serverless-aws-batch/issues"
23
- },
24
- "homepage": "https://github.com/rlyle/serverless-aws-batch#readme",
25
- "dependencies": {
26
- "@aws-sdk/client-batch": "^3.609.0",
27
- "bluebird": "^3.7.2",
28
- "cross-spawn": "^6.0.5",
29
- "fs-extra": "^7.0.1",
30
- "jszip": "^3.4.0",
31
- "lodash": "^4.17.13",
32
- "serverless": "^1.70.0"
33
- },
34
- "devDependencies": {}
35
- }
1
+ {
2
+ "name": "@rlyle1179/serverless-aws-batch",
3
+ "version": "1.1.21",
4
+ "description": "Serverless Framework Plugin for interacting with AWS Batch",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/rlyle/serverless-aws-batch.git"
12
+ },
13
+ "keywords": [
14
+ "serverless",
15
+ "aws",
16
+ "batch",
17
+ "python"
18
+ ],
19
+ "author": "Justin Ramsey",
20
+ "license": "MIT",
21
+ "bugs": {
22
+ "url": "https://github.com/rlyle/serverless-aws-batch/issues"
23
+ },
24
+ "homepage": "https://github.com/rlyle/serverless-aws-batch#readme",
25
+ "dependencies": {
26
+ "@aws-sdk/client-batch": "^3.609.0",
27
+ "bluebird": "^3.7.2",
28
+ "cross-spawn": "^6.0.5",
29
+ "fs-extra": "^7.0.1",
30
+ "jszip": "^3.4.0",
31
+ "lodash": "^4.17.13",
32
+ "serverless": "^1.70.0"
33
+ },
34
+ "devDependencies": {}
35
+ }