Skip to content

Commit b557745

Browse files
authored
Merge pull request #184794 from NickKouds/nikoudsi/AzureBatchJsGettingStartedUpdate
Updated Azure Batch JavaScript doc from Node.js repo to JavaScript/TypeScript SDK repo
2 parents d7aaa82 + 2f9af1e commit b557745

File tree

1 file changed

+110
-118
lines changed

1 file changed

+110
-118
lines changed

articles/batch/batch-js-get-started.md

Lines changed: 110 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Now, let us follow the process step by step into building the JavaScript client:
4545

4646
You can install Azure Batch SDK for JavaScript using the npm install command.
4747

48-
`npm install azure-batch`
48+
`npm install @azure/batch`
4949

5050
This command installs the latest version of azure-batch JavaScript SDK.
5151

@@ -79,21 +79,15 @@ Following code snippet first imports the azure-batch JavaScript module and then
7979
```javascript
8080
// Initializing Azure Batch variables
8181

82-
var batch = require('azure-batch');
82+
import { BatchServiceClient, BatchSharedKeyCredentials } from "@azure/batch";
8383

84-
var accountName = '<azure-batch-account-name>';
84+
// Replace values below with Batch Account details
85+
const batchAccountName = '<batch-account-name>';
86+
const batchAccountKey = '<batch-account-key>';
87+
const batchEndpoint = '<batch-account-url>';
8588

86-
var accountKey = '<account-key-downloaded>';
87-
88-
var accountUrl = '<account-url>'
89-
90-
// Create Batch credentials object using account name and account key
91-
92-
var credentials = new batch.SharedKeyCredentials(accountName,accountKey);
93-
94-
// Create Batch service client
95-
96-
var batch_client = new batch.ServiceClient(credentials,accountUrl);
89+
const credentials = new BatchSharedKeyCredentials(batchAccountName, batchAccountKey);
90+
const batchClient = new BatchServiceClient(credentials, batchEndpoint);
9791

9892
```
9993

@@ -120,16 +114,22 @@ The following code snippet creates the configuration parameter objects.
120114

121115
```javascript
122116
// Creating Image reference configuration for Ubuntu Linux VM
123-
var imgRef = {publisher:"Canonical",offer:"UbuntuServer",sku:"14.04.2-LTS",version:"latest"}
124-
117+
const imgRef = {
118+
publisher: "Canonical",
119+
offer: "UbuntuServer",
120+
sku: "18.04-LTS",
121+
version: "latest"
122+
}
125123
// Creating the VM configuration object with the SKUID
126-
var vmconfig = {imageReference:imgRef,nodeAgentSKUId:"batch.node.ubuntu 14.04"}
127-
128-
// Setting the VM size to Standard F4
129-
var vmSize = "STANDARD_F4"
130-
131-
//Setting number of VMs in the pool to 4
132-
var numVMs = 4
124+
const vmConfig = {
125+
imageReference: imgRef,
126+
nodeAgentSKUId: "batch.node.ubuntu 18.04"
127+
};
128+
// Number of VMs to create in a pool
129+
const numVms = 4;
130+
131+
// Setting the VM size
132+
const vmSize = "STANDARD_D1_V2";
133133
```
134134

135135
> [!TIP]
@@ -141,18 +141,28 @@ The following code snippet creates an Azure Batch pool.
141141

142142
```javascript
143143
// Create a unique Azure Batch pool ID
144-
var poolid = "pool" + customerDetails.customerid;
145-
var poolConfig = {id:poolid, displayName:poolid,vmSize:vmSize,virtualMachineConfiguration:vmconfig,targetDedicatedComputeNodes:numVms,enableAutoScale:false };
146-
// Creating the Pool for the specific customer
147-
var pool = batch_client.pool.add(poolConfig,function(error,result){
144+
const now = new Date();
145+
const poolId = `processcsv_${now.getFullYear()}${now.getMonth()}${now.getDay()}${now.getHours()}${now.getSeconds()}`;
146+
147+
const poolConfig = {
148+
id: poolId,
149+
displayName: "Processing csv files",
150+
vmSize: vmSize,
151+
virtualMachineConfiguration: vmConfig,
152+
targetDedicatedNodes: numVms,
153+
enableAutoScale: false
154+
};
155+
156+
// Creating the Pool
157+
var pool = batchClient.pool.add(poolConfig, function (error, result){
148158
if(error!=null){console.log(error.response)};
149159
});
150160
```
151161

152162
You can check the status of the pool created and ensure that the state is in "active" before going ahead with submission of a Job to that pool.
153163

154164
```javascript
155-
var cloudPool = batch_client.pool.get(poolid,function(error,result,request,response){
165+
var cloudPool = batchClient.pool.get(poolId,function(error,result,request,response){
156166
if(error == null)
157167
{
158168

@@ -179,61 +189,36 @@ var cloudPool = batch_client.pool.get(poolid,function(error,result,request,respo
179189
Following is a sample result object returned by the pool.get function.
180190

181191
```
182-
{ id: 'processcsv_201721152',
183-
displayName: 'processcsv_201721152',
184-
url: 'https://<batch-account-name>.centralus.batch.azure.com/pools/processcsv_201721152',
185-
eTag: '<eTag>',
186-
lastModified: 2017-03-27T10:28:02.398Z,
187-
creationTime: 2017-03-27T10:28:02.398Z,
192+
{
193+
id: 'processcsv_2022002321',
194+
displayName: 'Processing csv files',
195+
url: 'https://<batch-account-name>.westus.batch.azure.com/pools/processcsv_2022002321',
196+
eTag: '0x8D9D4088BC56FA1',
197+
lastModified: 2022-01-10T07:12:21.943Z,
198+
creationTime: 2022-01-10T07:12:21.943Z,
188199
state: 'active',
189-
stateTransitionTime: 2017-03-27T10:28:02.398Z,
190-
allocationState: 'resizing',
191-
allocationStateTransitionTime: 2017-03-27T10:28:02.398Z,
192-
vmSize: 'standard_a1',
193-
virtualMachineConfiguration:
194-
{ imageReference:
195-
{ publisher: 'Canonical',
196-
offer: 'UbuntuServer',
197-
sku: '14.04.2-LTS',
198-
version: 'latest' },
199-
nodeAgentSKUId: 'batch.node.ubuntu 14.04' },
200-
resizeTimeout:
201-
{ [Number: 900000]
202-
_milliseconds: 900000,
203-
_days: 0,
204-
_months: 0,
205-
_data:
206-
{ milliseconds: 0,
207-
seconds: 0,
208-
minutes: 15,
209-
hours: 0,
210-
days: 0,
211-
months: 0,
212-
years: 0 },
213-
_locale:
214-
Locale {
215-
_calendar: [Object],
216-
_longDateFormat: [Object],
217-
_invalidDate: 'Invalid date',
218-
ordinal: [Function: ordinal],
219-
_ordinalParse: /\d{1,2}(th|st|nd|rd)/,
220-
_relativeTime: [Object],
221-
_months: [Object],
222-
_monthsShort: [Object],
223-
_week: [Object],
224-
_weekdays: [Object],
225-
_weekdaysMin: [Object],
226-
_weekdaysShort: [Object],
227-
_meridiemParse: /[ap]\.?m?\.?/i,
228-
_abbr: 'en',
229-
_config: [Object],
230-
_ordinalParseLenient: /\d{1,2}(th|st|nd|rd)|\d{1,2}/ } },
231-
currentDedicated: 0,
232-
targetDedicated: 4,
200+
stateTransitionTime: 2022-01-10T07:12:21.943Z,
201+
allocationState: 'steady',
202+
allocationStateTransitionTime: 2022-01-10T07:13:35.103Z,
203+
vmSize: 'standard_d1_v2',
204+
virtualMachineConfiguration: {
205+
imageReference: {
206+
publisher: 'Canonical',
207+
offer: 'UbuntuServer',
208+
sku: '18.04-LTS',
209+
version: 'latest'
210+
},
211+
nodeAgentSKUId: 'batch.node.ubuntu 18.04'
212+
},
213+
resizeTimeout: 'PT15M',
214+
currentDedicatedNodes: 4,
215+
currentLowPriorityNodes: 0,
216+
targetDedicatedNodes: 4,
217+
targetLowPriorityNodes: 0,
233218
enableAutoScale: false,
234219
enableInterNodeCommunication: false,
235220
taskSlotsPerNode: 1,
236-
taskSchedulingPolicy: { nodeFillType: 'Spread' } }
221+
taskSchedulingPolicy: { nodeFillType: 'Spread' }}
237222
```
238223

239224
### Step 4: Submit an Azure Batch job
@@ -243,26 +228,26 @@ An Azure Batch job is a logical group of similar tasks. In our scenario, it is "
243228
These tasks would run in parallel and deployed across multiple nodes, orchestrated by the Azure Batch service.
244229

245230
> [!TIP]
246-
> You can use the taskSlotsPerNode property to specify maximum number of tasks that can run concurrently on a single node.
231+
> You can use the [taskSlotsPerNode](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/batch/arm-batch/src/models/index.ts#L1190-L1191) property to specify maximum number of tasks that can run concurrently on a single node.
247232
248233
#### Preparation task
249234

250235
The VM nodes created are blank Ubuntu nodes. Often, you need to install a set of programs as prerequisites.
251236
Typically, for Linux nodes you can have a shell script that installs the prerequisites before the actual tasks run. However it could be any programmable executable.
252237

253-
The [shell script](https://github.com/shwetams/azure-batchclient-sample-nodejs/blob/master/startup_prereq.sh) in this example installs Python-pip and the Azure Storage SDK for Python.
238+
The [shell script](https://github.com/Azure-Samples/azure-batch-samples/blob/master/JavaScript/Node.js/startup_prereq.sh) in this example installs Python-pip and the Azure Storage Blob SDK for Python.
254239

255240
You can upload the script on an Azure Storage Account and generate a SAS URI to access the script. This process can also be automated using the Azure Storage JavaScript SDK.
256241

257242
> [!TIP]
258-
> A preparation task for a job runs only on the VM nodes where the specific task needs to run. If you want prerequisites to be installed on all nodes irrespective of the tasks that run on it, you can use the startTask property while adding a pool. You can use the following preparation task definition for reference.
243+
> A preparation task for a job runs only on the VM nodes where the specific task needs to run. If you want prerequisites to be installed on all nodes irrespective of the tasks that run on it, you can use the [startTask](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/batch/batch/src/models/index.ts#L1432) property while adding a pool. You can use the following preparation task definition for reference.
259244
260-
A preparation task is specified during the submission of Azure Batch job. Following are the preparation task configuration parameters:
245+
A preparation task is specified during the submission of Azure Batch job. Following are some configurable preparation task parameters:
261246

262247
- **ID**: A unique identifier for the preparation task
263248
- **commandLine**: Command line to execute the task executable
264249
- **resourceFiles**: Array of objects that provide details of files needed to be downloaded for this task to run. Following are its options
265-
- blobSource: The SAS URI of the file
250+
- httpUrl: The URL of the file to download
266251
- filePath: Local path to download and save the file
267252
- fileMode: Only applicable for Linux nodes, fileMode is in octal format with a default value of 0770
268253
- **waitForSuccess**: If set to true, the task does not run on preparation task failures
@@ -271,64 +256,71 @@ A preparation task is specified during the submission of Azure Batch job. Follow
271256
Following code snippet shows the preparation task script configuration sample:
272257

273258
```javascript
274-
var job_prep_task_config = {id:"installprereq",commandLine:"sudo sh startup_prereq.sh > startup.log",resourceFiles:[{'blobSource':'Blob SAS URI','filePath':'startup_prereq.sh'}],waitForSuccess:true,runElevated:true}
259+
var jobPrepTaskConfig = {id:"installprereq",commandLine:"sudo sh startup_prereq.sh > startup.log",resourceFiles: [{ 'httpUrl': 'Blob sh url', 'filePath': 'startup_prereq.sh' }],waitForSuccess:true,runElevated:true, userIdentity: {autoUser: {elevationLevel: "admin", scope: "pool"}}}
275260
```
276261

277262
If there are no prerequisites to be installed for your tasks to run, you can skip the preparation tasks. Following code creates a job with display name "process csv files."
278263

279264
```javascript
280-
// Setting up Batch pool configuration
281-
var pool_config = {poolId:poolid}
282-
// Setting up Job configuration along with preparation task
283-
var jobId = "processcsvjob"
284-
var job_config = {id:jobId,displayName:"process csv files",jobPreparationTask:job_prep_task_config,poolInfo:pool_config}
265+
// Setting Batch Pool ID
266+
const poolInfo = { poolId: poolId };
267+
// Batch job configuration object
268+
const jobId = "processcsvjob";
269+
const jobConfig = {
270+
id: jobId,
271+
displayName: "process csv files",
272+
jobPreparationTask: jobPrepTaskConfig,
273+
poolInfo: poolInfo
274+
};
285275
// Adding Azure batch job to the pool
286-
var job = batch_client.job.add(job_config,function(error,result){
287-
if(error != null)
288-
{
289-
console.log("Error submitting job : " + error.response);
290-
}});
276+
const job = batchClient.job.add(jobConfig, function (error, result) {
277+
if (error !== null) {
278+
console.log("An error occurred while creating the job...");
279+
console.log(error.response);
280+
}
281+
});
291282
```
292283

293284
### Step 5: Submit Azure Batch tasks for a job
294285

295286
Now that our process csv job is created, let us create tasks for that job. Assuming we have four containers, we have to create four tasks, one for each container.
296287

297-
If we look at the [Python script](https://github.com/shwetams/azure-batchclient-sample-nodejs/blob/master/processcsv.py), it accepts two parameters:
288+
If we look at the [Python script](https://github.com/Azure-Samples/azure-batch-samples/blob/master/JavaScript/Node.js/processcsv.py), it accepts two parameters:
298289

299290
- container name: The Storage container to download files from
300291
- pattern: An optional parameter of file name pattern
301292

302-
Assuming we have four containers "con1", "con2", "con3","con4" following code shows submitting for tasks to the Azure batch job "process csv" we created earlier.
293+
Assuming we have four containers "con1", "con2", "con3","con4" following code shows submitting four tasks to the Azure batch job "process csv" we created earlier.
303294

304295
```javascript
305296
// storing container names in an array
306-
var container_list = ["con1","con2","con3","con4"]
307-
container_list.forEach(function(val,index){
308-
309-
var container_name = val;
310-
var taskID = container_name + "_process";
311-
var task_config = {id:taskID,displayName:'process csv in ' + container_name,commandLine:'python processcsv.py --container ' + container_name,resourceFiles:[{'blobSource':'<blob SAS URI>','filePath':'processcsv.py'}]}
312-
var task = batch_client.task.add(poolid,task_config,function(error,result){
313-
if(error != null)
314-
{
315-
console.log(error.response);
316-
}
317-
else
318-
{
319-
console.log("Task for container : " + container_name + "submitted successfully");
320-
}
321-
322-
323-
324-
});
325-
297+
const containerList = ["con1", "con2", "con3", "con4"]; //Replace with list of blob containers within storage account
298+
containerList.forEach(function (val, index) {
299+
console.log("Submitting task for container : " + val);
300+
const containerName = val;
301+
const taskID = containerName + "_process";
302+
// Task configuration object
303+
const taskConfig = {
304+
id: taskID,
305+
displayName: 'process csv in ' + containerName,
306+
commandLine: 'python processcsv.py --container ' + containerName,
307+
resourceFiles: [{ 'httpUrl': 'Blob script url', 'filePath': 'processcsv.py' }]
308+
};
309+
310+
const task = batchClient.task.add(jobId, taskConfig, function (error, result) {
311+
if (error !== null) {
312+
console.log("Error occured while creating task for container " + containerName + ". Details : " + error.response);
313+
}
314+
else {
315+
console.log("Task for container : " + containerName + " submitted successfully");
316+
}
326317
});
318+
});
327319
```
328320

329321
The code adds multiple tasks to the pool. And each of the tasks is executed on a node in the pool of VMs created. If the number of tasks exceeds the number of VMs in a pool or the taskSlotsPerNode property, the tasks wait until a node is made available. This orchestration is handled by Azure Batch automatically.
330322

331-
The portal has detailed views on the tasks and job statuses. You can also use the list and get functions in the Azure JavaScript SDK..
323+
The portal has detailed views on the tasks and job statuses. You can also use the list and get functions in the Azure JavaScript SDK. Details are provided in the documentation [link](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/batch/batch/src/operations/job.ts#L114-L149).
332324

333325
## Next steps
334326

0 commit comments

Comments
 (0)