1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
FILE: job_queue_crud_ops_async.py
DESCRIPTION:
These samples demonstrates how to create Job Queues used in ACS JobRouter.
You need a valid connection string to an Azure Communication Service to execute the sample
USAGE:
python job_queue_crud_ops_async.py
Set the environment variables with your own values before running the sample:
1) AZURE_COMMUNICATION_SERVICE_ENDPOINT - Communication Service endpoint url
"""
import os
import asyncio
class JobQueueSamplesAsync(object):
endpoint = os.environ.get("AZURE_COMMUNICATION_SERVICE_ENDPOINT", None)
if not endpoint:
raise ValueError("Set AZURE_COMMUNICATION_SERVICE_ENDPOINT env before run this sample.")
_job_queue_id = "sample_q_policy"
_distribution_policy_id = "sample_dp_policy"
async def setup_distribution_policy(self):
connection_string = self.endpoint
distribution_policy_id = self._distribution_policy_id
from azure.communication.jobrouter.aio import RouterAdministrationClient
from azure.communication.jobrouter import (
LongestIdleMode,
DistributionPolicy
)
router_admin_client = RouterAdministrationClient.from_connection_string(conn_str = connection_string)
async with router_admin_client:
distribution_policy = await router_admin_client.create_distribution_policy(
distribution_policy_id = distribution_policy_id,
distribution_policy = DistributionPolicy(
offer_ttl_seconds = 10 * 60,
mode = LongestIdleMode(
min_concurrent_offers = 1,
max_concurrent_offers = 1
)
)
)
print(f"Sample setup completed: Created distribution policy")
async def create_queue(self):
connection_string = self.endpoint
job_queue_id = self._job_queue_id
distribution_policy_id = self._distribution_policy_id
# [START create_queue_async]
from azure.communication.jobrouter.aio import RouterAdministrationClient
from azure.communication.jobrouter import (
JobQueue,
)
# set `connection_string` to an existing ACS endpoint
router_admin_client = RouterAdministrationClient.from_connection_string(conn_str = connection_string)
print("RouterAdministrationClient created successfully!")
async with router_admin_client:
job_queue: JobQueue = await router_admin_client.create_queue(
queue_id = job_queue_id,
queue = JobQueue(
distribution_policy_id = distribution_policy_id,
name = "My job queue"
)
)
print(f"Job queue successfully created with id: {job_queue.id}")
# [END create_queue_async]
async def update_queue(self):
connection_string = self.endpoint
job_queue_id = self._job_queue_id
# [START update_queue_async]
from azure.communication.jobrouter.aio import RouterAdministrationClient
from azure.communication.jobrouter import (
JobQueue,
)
# set `connection_string` to an existing ACS endpoint
router_admin_client = RouterAdministrationClient.from_connection_string(conn_str = connection_string)
print("RouterAdministrationClient created successfully!")
async with router_admin_client:
updated_job_queue: JobQueue = await router_admin_client.update_queue(
queue_id = job_queue_id,
labels = {
"Additional-Queue-Label": "ChatQueue"
}
)
print(f"Router queue successfully update with labels {updated_job_queue.labels}")
# [END update_queue_async]
async def get_queue(self):
connection_string = self.endpoint
job_queue_id = self._job_queue_id
# [START get_queue_async]
from azure.communication.jobrouter.aio import RouterAdministrationClient
router_admin_client = RouterAdministrationClient.from_connection_string(conn_str = connection_string)
async with router_admin_client:
job_queue = await router_admin_client.get_queue(queue_id = job_queue_id)
print(f"Successfully fetched router queue with id: {job_queue.id}")
# [END get_queue_async]
async def get_queue_statistics(self):
connection_string = self.endpoint
job_queue_id = self._job_queue_id
# [START get_queue_statistics_async]
from azure.communication.jobrouter.aio import RouterClient
from azure.communication.jobrouter import (
QueueStatistics
)
router_client: RouterClient = RouterClient.from_connection_string(conn_str = connection_string)
async with router_client:
job_queue_statistics: QueueStatistics = await router_client.get_queue_statistics(queue_id = job_queue_id)
print(f"Successfully fetched queue statistics router queue: {job_queue_statistics}")
# [END get_queue_statistics_async]
async def list_queues(self):
connection_string = self.endpoint
# [START list_queues_async]
from azure.communication.jobrouter.aio import RouterAdministrationClient
router_admin_client = RouterAdministrationClient.from_connection_string(conn_str = connection_string)
async with router_admin_client:
job_queue_iterator = router_admin_client.list_queues()
async for q in job_queue_iterator:
print(f"Retrieved queue policy with id: {q.job_queue.id}")
print(f"Successfully completed fetching job queues")
# [END list_queues_async]
async def list_queues_batched(self):
connection_string = self.endpoint
# [START list_queues_batched_async]
from azure.communication.jobrouter.aio import RouterAdministrationClient
router_admin_client = RouterAdministrationClient.from_connection_string(conn_str = connection_string)
async with router_admin_client:
job_queue_iterator = router_admin_client.list_queues(results_per_page = 10)
async for queue_page in job_queue_iterator.by_page():
job_queues_in_page = [i async for i in queue_page]
print(f"Retrieved {len(job_queues_in_page)} queues in current page")
for q in job_queues_in_page:
print(f"Retrieved queue policy with id: {q.job_queue.id}")
print(f"Successfully completed fetching job queues")
# [END list_queues_batched_async]
async def clean_up(self):
connection_string = self.endpoint
job_queue_id = self._job_queue_id
# [START delete_queue_async]
from azure.communication.jobrouter.aio import RouterAdministrationClient
router_admin_client = RouterAdministrationClient.from_connection_string(conn_str = connection_string)
async with router_admin_client:
await router_admin_client.delete_queue(queue_id = job_queue_id)
# [END delete_queue_async]
async def main():
sample = JobQueueSamplesAsync()
await sample.setup_distribution_policy()
await sample.create_queue()
# await sample.update_queue()
await sample.get_queue()
await sample.get_queue_statistics()
await sample.list_queues()
await sample.list_queues_batched()
await sample.clean_up()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
|