Skip to content

Commit fa88790

Browse files
authored
Merge pull request #298 from Azure-Samples/mipatera/js-sample
Switched from node to JS SDK
2 parents 0f9b248 + 4e70c82 commit fa88790

File tree

6 files changed

+133
-128
lines changed

6 files changed

+133
-128
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "nodejsgettingstarted",
3+
"type": "module",
34
"version": "1.0.0",
45
"description": "Getting started sample in Node.js for deploying Azure batch jobs",
56
"dependencies": {
6-
"azure-batch": "^8.0.0",
7-
"package.json": "^2.0.1"
7+
"@azure/batch": "^8.0.0",
8+
"@azure/ms-rest-nodeauth": "^3.0.6"
89
},
9-
"main": "nodejs_batch_client_sample.js",
10+
"main": "sample.js",
1011
"scripts": {
1112
"test": "echo \"Error: no test specified\" && exit 1"
1213
},

JavaScript/Node.js/sample.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import { BatchServiceClient, BatchSharedKeyCredentials } from "@azure/batch";
2+
3+
// Replace values below with Batch Account details
4+
const batchAccountName = '<batch-account-name>';
5+
const batchAccountKey = '<batch-account-key>';
6+
const batchEndpoint = '<batch-account-url>';
7+
8+
// Replace values with SAS URIs of the shell script file
9+
const sh_url = "https://batchdevsgsa.blob.core.windows.net/downloads/startup_prereq.sh?st=2017-04-10T18%3A11%3A00Z&se=2020-03-11T18%3A11%3A00Z&sp=rl&sv=2018-03-28&sr=b&sig=xxxx";
10+
11+
// Replace values with SAS URIs of the Python script file
12+
const scriptURI = "https://batchdevsgsa.blob.core.windows.net/downloads/processcsv.py?st=2017-04-10T18%3A11%3A00Z&se=2020-03-11T18%3A11%3A00Z&sp=rl&sv=2018-03-28&sr=b&sig=xxxx";
13+
14+
15+
const credentials = new BatchSharedKeyCredentials(batchAccountName, batchAccountKey);
16+
const batchClient = new BatchServiceClient(credentials, batchEndpoint);
17+
18+
// Pool ID
19+
const now = new Date();
20+
const poolId = `processcsv_${now.getFullYear()}${now.getMonth()}${now.getDay()}${now.getHours()}${now.getSeconds()}`;
21+
22+
// Job ID
23+
const jobId = "processcsvjob";
24+
25+
// Pool VM Image Reference
26+
const imgRef = {
27+
publisher: "Canonical",
28+
offer: "UbuntuServer",
29+
sku: "16.04-LTS",
30+
version: "latest"
31+
}
32+
// Pool VM configuraion object
33+
const vmConfig = {
34+
imageReference: imgRef,
35+
nodeAgentSKUId: "batch.node.ubuntu 16.04"
36+
};
37+
// Number of VMs to create in a pool
38+
const numVms = 4;
39+
const vmSize = "STANDARD_D1_V2";
40+
// Pool configuration object
41+
const poolConfig = {
42+
id: poolId,
43+
displayName: "Processing csv files",
44+
vmSize: vmSize,
45+
virtualMachineConfiguration: vmConfig,
46+
targetDedicatedNodes: numVms,
47+
enableAutoScale: false
48+
};
49+
50+
// Creating Batch Pool
51+
console.log("Creating pool with ID : " + poolId);
52+
const pool = batchClient.pool.add(poolConfig, function (error, result, request, response) {
53+
if (error !== null) {
54+
console.log("An error occured while creating the pool...");
55+
console.log(error.response);
56+
}
57+
else {
58+
// If there is no error then create the Batch Job
59+
createJob();
60+
}
61+
});
62+
63+
function createJob() {
64+
console.log("Creating job with ID : " + jobId);
65+
// Preparation Task configuraion object
66+
const jobPrepTaskConfig = {
67+
id: "installprereq",
68+
commandLine: "sudo sh startup_prereq.sh > startup.log",
69+
resourceFiles: [{ 'httpUrl': sh_url, 'filePath': 'startup_prereq.sh' }],
70+
waitForSuccess: true, runElevated: true
71+
};
72+
73+
// Setting Batch Pool ID
74+
const poolInfo = { poolId: poolId };
75+
// Batch job configuration object
76+
const jobConfig = {
77+
id: jobId,
78+
displayName: "process csv files",
79+
jobPreparationTask: jobPrepTaskConfig,
80+
poolInfo: poolInfo
81+
};
82+
83+
// Submitting Batch Job
84+
const job = batchClient.job.add(jobConfig, function (error, result) {
85+
if (error !== null) {
86+
console.log("An error occurred while creating the job...");
87+
console.log(error);
88+
}
89+
else {
90+
// Create tasks if the job submitted successfully
91+
createTasks();
92+
}
93+
});
94+
}
95+
96+
function createTasks() {
97+
console.log("Creating tasks....");
98+
const containerList = ["con1", "con2", "con3", "con4"];
99+
containerList.forEach(function (val, index) {
100+
console.log("Submitting task for container : " + val);
101+
const containerName = val;
102+
const taskID = containerName + "_process";
103+
// Task configuration object
104+
const taskConfig = {
105+
id: taskID,
106+
displayName: 'process csv in ' + containerName,
107+
commandLine: 'python processcsv.py --container ' + containerName,
108+
resourceFiles: [{ 'httpUrl': scriptURI, 'filePath': 'processcsv.py' }]
109+
};
110+
111+
const task = batchClient.task.add(jobId, taskConfig, function (error, result) {
112+
if (error !== null) {
113+
console.log("Error occured while creating task for container " + containerName + ". Details : " + error.response);
114+
}
115+
else {
116+
console.log("Task for container : " + containerName + " submitted successfully");
117+
}
118+
});
119+
});
120+
}
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
# Azure Batch Node.js sample
2-
The set of scripts provided in the [GettingStarted](https://github.com/Azure/azure-batch-samples/tree/master/Node.js/GettingStarted) folder is a sample of creating Azure Batch jobs using the Node.js SDK.
1+
# Azure Batch JavaScript sample
2+
The set of scripts provided in the [GettingStarted](https://github.com/Azure/azure-batch-samples/tree/master/JavaScript/GettingStarted) folder is a sample of creating Azure Batch jobs using the JavaScript SDK.
33

44
Please refer to a step by step explanation of these scripts at the [Azure documentation link](https://docs.microsoft.com/en-us/azure/batch/batch-nodejs-get-started).
55

66
You will need to fill in Azure Batch Account details and the resource file SAS URIs
77

8-
// Setting up variables specific to Batch & storage account
8+
```// Setting up variables specific to Batch & storage account
99
var accountName = '<azure-batch-account-name>';
1010
var accountKey = '<account-key-downloaded>';
1111
var accountUrl = '<account-url>';
1212
var sh_url = '<Shell-script-SAS-URI>';
1313
var scriptURI = '<Python-script-SAS-URI'>;
14+
```
1415

15-
Also modify the [processcsv.py](https://github.com/Azure/azure-batch-samples/blob/master/Node.js/GettingStarted/processcsv.py) with your storage account credentials that contain the csv files for conversion.
16+
Also modify the [processcsv.py](https://github.com/Azure/azure-batch-samples/blob/master/Node.js/GettingStarted/processcsv.py) with your storage account credentials that contain the csv files for conversion.
17+
18+
After the above steps are complete, you can run the sample as follows (requires [Node.js](https://nodejs.org/en/) v14 or later):
19+
20+
> node sample

Node.js/GettingStarted/nodejs_batch_client_sample.js

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)