@@ -48,11 +48,18 @@ pip install azure-communication-jobrouter
48
48
Create a new file called ` router-quickstart.py ` and add the basic program structure.
49
49
50
50
``` python
51
- import os
52
- from datetime import datetime, timedelta
51
+ import time
53
52
from azure.communication.jobrouter import (
54
53
JobRouterClient,
55
- JobRouterAdministrationClient
54
+ JobRouterAdministrationClient,
55
+ DistributionPolicy,
56
+ LongestIdleMode,
57
+ RouterQueue,
58
+ RouterJob,
59
+ RouterWorkerSelector,
60
+ LabelOperator,
61
+ RouterWorker,
62
+ ChannelConfiguration
56
63
)
57
64
58
65
class RouterQuickstart (object ):
@@ -70,19 +77,19 @@ Job Router clients can be authenticated using your connection string acquired fr
70
77
``` python
71
78
# Get a connection string to our Azure Communication Services resource.
72
79
connection_string = " your_connection_string"
73
- router_admin_client = JobRouterAdministrationClient( connection_string)
74
- router_client = JobRouterClient( connection_string)
80
+ router_admin_client = JobRouterAdministrationClient.from_connection_string( conn_str = connection_string)
81
+ router_client = JobRouterClient.from_connection_string( conn_str = connection_string)
75
82
```
76
83
77
84
## Create a distribution policy
78
85
79
- Job Router uses a distribution policy to decide how Workers will be notified of available Jobs and the time to live for the notifications, known as ** Offers** . Create the policy by specifying the ** ID ** , a ** name** , an ** offerExpiresAfterUtc ** , and a distribution ** mode** .
86
+ Job Router uses a distribution policy to decide how Workers will be notified of available Jobs and the time to live for the notifications, known as ** Offers** . Create the policy by specifying the ** distribution_policy_id ** , a ** name** , an ** offer_expires_after_seconds ** value , and a distribution ** mode** .
80
87
81
88
``` python
82
89
distribution_policy = router_admin_client.create_distribution_policy(
83
- distribution_policy_id = " distribution-policy-1" ,
90
+ distribution_policy_id = " distribution-policy-1" ,
84
91
distribution_policy = DistributionPolicy(
85
- offer_expires_after = timedelta( minutes = 1 ) ,
92
+ offer_expires_after_seconds = 60 ,
86
93
mode = LongestIdleMode(),
87
94
name = " My distribution policy"
88
95
))
@@ -108,14 +115,14 @@ Now, we can submit a job directly to that queue, with a worker selector that req
108
115
``` python
109
116
job = router_client.create_job(
110
117
job_id = " job-1" ,
111
- job = RouterJob(
118
+ router_job = RouterJob(
112
119
channel_id = " voice" ,
113
120
queue_id = queue.id,
114
121
priority = 1 ,
115
122
requested_worker_selectors = [
116
123
RouterWorkerSelector(
117
124
key = " Some-Skill" ,
118
- label_operator = LabelOperator.GreaterThan ,
125
+ label_operator = LabelOperator.GREATER_THAN ,
119
126
value = 10
120
127
)
121
128
]
@@ -132,14 +139,15 @@ worker = router_client.create_worker(
132
139
router_worker = RouterWorker(
133
140
total_capacity = 1 ,
134
141
queue_assignments = {
135
- " queue-1 " : {}
142
+ queue.id : {}
136
143
},
137
144
labels = {
138
- " Some-Skill" : LabelValue( 11 )
145
+ " Some-Skill" : 11
139
146
},
140
147
channel_configurations = {
141
148
" voice" : ChannelConfiguration(capacity_cost_per_job = 1 )
142
- }
149
+ },
150
+ available_for_offers = True
143
151
))
144
152
```
145
153
@@ -182,6 +190,14 @@ router_client.close_job(job_id = job.id, assignment_id = accept.assignment_id, d
182
190
print (f " Worker { worker.id} has closed job { accept.job_id} " )
183
191
```
184
192
193
+ ## Delete the job
194
+
195
+ Once the job has been closed, we can delete the job so that we can re-create the job with the same ID if we run this sample again
196
+ ``` python
197
+ router_client.delete_job(accept.job_id)
198
+ print (f " Deleting { accept.job_id} " )
199
+ ```
200
+
185
201
## Run the code
186
202
187
203
To run the code, make sure you are on the directory where your ` router-quickstart.py ` file is.
0 commit comments