Skip to content

Commit bdf4c81

Browse files
committed
Added Node.js content
1 parent e74a693 commit bdf4c81

File tree

3 files changed

+153
-8
lines changed

3 files changed

+153
-8
lines changed

includes/iot-hub-howto-schedule-broadcast-jobs-java.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ private static JobResult scheduleJobSetDesiredProperties(JobClient jobClient, St
133133

134134
### Monitor a job
135135

136-
Use [getJob](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient?#com-microsoft-azure-sdk-iot-service-jobs-jobclient-getjob(java-lang-string)) to access job status.
136+
Use [getJob](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient?#com-microsoft-azure-sdk-iot-service-jobs-jobclient-getjob(java-lang-string)) to access a job status.
137+
138+
For example:
137139

138140
```java
139141
private static void monitorJob(JobClient jobClient, String jobId) {
@@ -162,7 +164,9 @@ private static void monitorJob(JobClient jobClient, String jobId) {
162164

163165
### Query a job status
164166

165-
Use [queryDeviceJob](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient?#com-microsoft-azure-sdk-iot-service-jobs-jobclient-querydevicejob(java-lang-string)) to query job status.
167+
Use [queryDeviceJob](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient?#com-microsoft-azure-sdk-iot-service-jobs-jobclient-querydevicejob(java-lang-string)) to query a job status.
168+
169+
For example:
166170

167171
```java
168172
private static void queryDeviceJobs(JobClient jobClient, String start) throws Exception {

includes/iot-hub-howto-schedule-broadcast-jobs-node.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,142 @@ ms.custom: [amqp, mqtt, devx-track-js]
1616
## Overview
1717

1818
This article describes how to use the [Azure IoT SDK for Node.js](https://github.com/Azure/azure-iot-sdk-node) to create backend service application code for job scheduling.
19+
20+
### Install service SDK package
21+
22+
Run this command to install **azure-iothub** on your development machine:
23+
24+
```cmd/sh
25+
npm install azure-iothub --save
26+
```
27+
28+
The [JobClient](/javascript/api/azure-iothub/jobclient) class exposes all methods required to interact with job scheduling from a backend application.
29+
30+
### Connect to IoT hub
31+
32+
You can connect a backend service to IoT Hub using the following methods:
33+
34+
* Shared access policy
35+
* Microsoft Entra
36+
37+
[!INCLUDE [iot-authentication-service-connection-string.md](iot-authentication-service-connection-string.md)]
38+
39+
#### Connect using a shared access policy
40+
41+
Use [fromConnectionString](/javascript/api/azure-iothub/jobclient?#azure-iothub-jobclient-fromconnectionstring) to connect to IoT hub.
42+
43+
In this article, you create a back-end service that schedules a job to invoke a direct method on a device, schedules a job to update the device twin, and monitors the progress of each job. To perform these operations, your service needs the **registry read** and **registry write permissions**. By default, every IoT hub is created with a shared access policy named **registryReadWrite** that grants these permissions.
44+
45+
For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
46+
47+
```javascript
48+
'use strict';
49+
var JobClient = require('azure-iothub').JobClient;
50+
var connectionString = '{Shared access policy connection string}';
51+
var jobClient = JobClient.fromConnectionString(connectionString);
52+
```
53+
54+
#### Connect using Microsoft Entra
55+
56+
[!INCLUDE [iot-hub-howto-connect-service-iothub-entra-node](iot-hub-howto-connect-service-iothub-entra-node.md)]
57+
58+
### Create a device method update job
59+
60+
Use [scheduleDeviceMethod](/javascript/api/azure-iothub/jobclient?#azure-iothub-jobclient-scheduledevicemethod) to create a new device method job to run a device method on one or multiple devices.
61+
62+
This example schedules a device method call job for a specific job ID.
63+
64+
```javascript
65+
var methodParams = {
66+
methodName: 'lockDoor',
67+
payload: null,
68+
responseTimeoutInSeconds: 15 // Timeout after 15 seconds if device is unable to process method
69+
};
70+
71+
var methodJobId = uuid.v4();
72+
console.log('scheduling Device Method job with id: ' + methodJobId);
73+
jobClient.scheduleDeviceMethod(methodJobId,
74+
queryCondition,
75+
methodParams,
76+
startTime,
77+
maxExecutionTimeInSeconds,
78+
function(err) {
79+
if (err) {
80+
console.error('Could not schedule device method job: ' + err.message);
81+
} else {
82+
monitorJob(methodJobId, function(err, result) {
83+
if (err) {
84+
console.error('Could not monitor device method job: ' + err.message);
85+
} else {
86+
console.log(JSON.stringify(result, null, 2));
87+
}
88+
});
89+
}
90+
});
91+
```
92+
93+
### Schedule a device twin update job
94+
95+
Use [scheduleTwinUpdate](/javascript/api/azure-iothub/jobclient?#azure-iothub-jobclient-scheduletwinupdate) to create a new job to run a device twin update on one or multiple devices.
96+
97+
```javascript
98+
var twinPatch = {
99+
etag: '*',
100+
properties: {
101+
desired: {
102+
building: '43',
103+
floor: 3
104+
}
105+
}
106+
};
107+
108+
var twinJobId = uuid.v4();
109+
110+
console.log('scheduling Twin Update job with id: ' + twinJobId);
111+
jobClient.scheduleTwinUpdate(twinJobId,
112+
queryCondition,
113+
twinPatch,
114+
startTime,
115+
maxExecutionTimeInSeconds,
116+
function(err) {
117+
if (err) {
118+
console.error('Could not schedule twin update job: ' + err.message);
119+
} else {
120+
monitorJob(twinJobId, function(err, result) {
121+
if (err) {
122+
console.error('Could not monitor twin update job: ' + err.message);
123+
} else {
124+
console.log(JSON.stringify(result, null, 2));
125+
}
126+
});
127+
}
128+
});
129+
```
130+
131+
### Monitor a job
132+
133+
Use [getJob](/javascript/api/azure-iothub/jobclient?#azure-iothub-jobclient-getjob) to monitor a job status.
134+
135+
This example checks the job status for a specific job ID periodically until the job is complete or failed.
136+
137+
```javascript
138+
function monitorJob (jobId, callback) {
139+
var jobMonitorInterval = setInterval(function() {
140+
jobClient.getJob(jobId, function(err, result) {
141+
if (err) {
142+
console.error('Could not get job status: ' + err.message);
143+
} else {
144+
console.log('Job: ' + jobId + ' - status: ' + result.status);
145+
if (result.status === 'completed' || result.status === 'failed' || result.status === 'cancelled') {
146+
clearInterval(jobMonitorInterval);
147+
callback(null, result);
148+
}
149+
}
150+
});
151+
}, 5000);
152+
}
153+
```
154+
155+
### SDK schedule job example
156+
157+
The Azure IoT SDK for Node.js provides a working sample of a service app that handles job scheduling tasks. For more information, see [Job client E2E test](https://github.com/Azure/azure-iot-sdk-node/blob/a85e280350a12954f46672761b0b516d08d374b5/e2etests/test/job_client.js)

includes/iot-hub-howto-schedule-broadcast-jobs-python.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ ms.date: 1/7/2025
1111
ms.custom: mqtt, devx-track-python, py-fresh-zinc
1212
---
1313

14+
* Python SDK - [Python version 3.7 or later](https://www.python.org/downloads/) is recommended. Make sure to use the 32-bit or 64-bit installation as required by your setup. When prompted during the installation, make sure to add Python to your platform-specific environment variable.
15+
1416
## Overview
1517

1618
This article describes how to use the [Azure IoT SDK for Python](https://github.com/Azure/azure-iot-sdk-python) to create backend service application code for job scheduling.
@@ -88,7 +90,7 @@ def device_method_job(job_id, device_id, execution_time):
8890

8991
Use [create_scheduled_job](/python/api/azure-iot-hub/azure.iot.hub.iothubjobmanager?#azure-iot-hub-iothubjobmanager-create-scheduled-job) to create a new job to run a device twin update on one or multiple devices.
9092

91-
This example schedules a device twin update job for a specific job Id.
93+
This example schedules a device twin update job for a specific job ID.
9294

9395
```python
9496
def device_twin_job(job_id, device_id, execution_time):
@@ -108,22 +110,22 @@ def device_twin_job(job_id, device_id, execution_time):
108110

109111
### Monitor a job
110112

111-
Use [get_scheduled_job](/python/api/azure-iot-hub/azure.iot.hub.iothubjobmanager?#azure-iot-hub-iothubjobmanager-get-scheduled-job) to retrieve the details of a scheduled job on an IoT Hub.
113+
Use [get_scheduled_job](/python/api/azure-iot-hub/azure.iot.hub.iothubjobmanager?#azure-iot-hub-iothubjobmanager-get-scheduled-job) to retrieve the details of a specific job on an IoT Hub.
112114

113-
For example:
115+
This example checks the job status every five seconds until the job is complete.
114116

115117
```python
116118
while True:
117119
get_job_response = iothub_job_manager.get_scheduled_job(job_request.job_id)
118120
print_job_response("Get job response: ", get_job_response)
119121
if get_job_response.status == "completed":
120-
break
122+
print ( "Job is completed." )
121123
time.sleep(5)
122124
```
123125

124126
### SDK schedule job examples
125127

126128
The Azure IoT SDK for Python provides working samples of service apps that handle job scheduling tasks. For more information, see:
127129

128-
* [Invoke a device method](https://github.com/Azure/azure-iot-hub-python/blob/8c8f315e8b26c65c5517541a7838a20ef8ae668b/samples/iothub_job_manager_method_sample.py)
129-
* [Update a device twin](https://github.com/Azure/azure-iot-hub-python/blob/8c8f315e8b26c65c5517541a7838a20ef8ae668b/samples/iothub_job_manager_twin_update_sample.py)
130+
* [Schedule a device method job](https://github.com/Azure/azure-iot-hub-python/blob/8c8f315e8b26c65c5517541a7838a20ef8ae668b/samples/iothub_job_manager_method_sample.py)
131+
* [Schedule a device twin update](https://github.com/Azure/azure-iot-hub-python/blob/8c8f315e8b26c65c5517541a7838a20ef8ae668b/samples/iothub_job_manager_twin_update_sample.py)

0 commit comments

Comments
 (0)