-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_process.py
167 lines (135 loc) · 9.2 KB
/
queue_process.py
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
import json
from azure.core.exceptions import AzureError
from azure.servicebus import ServiceBusReceiver, ServiceBusSender
import service_bus_base
from message_parser import ServiceBusMessageParser
class QueueProcess(service_bus_base.ServiceBusBase):
def __init__(self, ctx):
service_bus_base.ServiceBusBase.__init__(self, ctx)
def spying_message_queue(self):
with self.service_bus_client:
if self.ctx.obj['GET_QUEUE_PROPERTIES']:
QueueProcess.get_queue_properties(self)
receiver = QueueProcess.get_receiver(self)
with receiver:
sequence_number = 0
for x in range(self.ctx.obj['PAGES']):
if sequence_number == 0:
received_msgs = receiver.peek_messages(max_message_count=self.ctx.obj['MAX_MESSAGE_COUNT'])
else:
received_msgs = receiver.peek_messages(max_message_count=self.ctx.obj['MAX_MESSAGE_COUNT'],
sequence_number=sequence_number+1)
self.custom_log_obj.log_info("%s %s" % ('Number of messages: ', len(received_msgs),))
for msg in received_msgs:
if self.ctx.obj['PRETTY']:
QueueProcess.log_message_pretty(self, msg)
else:
QueueProcess.log_message(self, msg)
sequence_number = msg.sequence_number
def get_queue_properties(self):
self.custom_log_obj.log_info("-- Get Queue Runtime Properties")
try:
get_queue_runtime_properties = self.servicebus_mgmt_client.get_queue_runtime_properties(self.ctx.obj['QUEUE_NAME'])
get_queue_properties = self.servicebus_mgmt_client.get_queue(self.ctx.obj['QUEUE_NAME'])
self.custom_log_obj.log_info("-- Authorization Rules")
for x in get_queue_properties.authorization_rules:
self.custom_log_obj.log_info("%s %s" % ('Rights:', x.rights,))
self.custom_log_obj.log_info("%s %s" % ('Primary key:', x.primary_key,))
self.custom_log_obj.log_info("%s %s" % ('Secondary key:', x.secondary_key,))
self.custom_log_obj.log_info("%s %s" % ('Key name:', x.key_name,))
self.custom_log_obj.log_info("%s %s" % ('Claim type:', x.claim_type,))
self.custom_log_obj.log_info("%s %s" % ('Claim value:', x.claim_value,))
self.custom_log_obj.log_info("%s %s" % ('Modified time utc:', x.modified_at_utc,))
self.custom_log_obj.log_info("%s %s" % ('Created time utc:', x.created_at_utc,))
self.custom_log_obj.log_info("-- Queue Information")
self.custom_log_obj.log_info("%s %s" % ('Status:', get_queue_properties.status,))
self.custom_log_obj.log_info("%s %s" % ('Queue name:', get_queue_runtime_properties.name,))
self.custom_log_obj.log_info("%s %s" % ('Size in bytes: ', get_queue_runtime_properties.size_in_bytes,))
self.custom_log_obj.log_info("%s %s" % ('Created at utc: ', get_queue_runtime_properties.created_at_utc,))
self.custom_log_obj.log_info("%s %s" % ('Updated at utc: ', get_queue_runtime_properties.updated_at_utc,))
self.custom_log_obj.log_info("%s %s" % ('Accessed at utc: ', get_queue_runtime_properties.accessed_at_utc,))
self.custom_log_obj.log_info("%s %s" % ('Active message count:', get_queue_runtime_properties.active_message_count,))
self.custom_log_obj.log_info("%s %s" % ('Dead letter message count:', get_queue_runtime_properties.dead_letter_message_count,))
self.custom_log_obj.log_info("%s %s" % ('Scheduled message count:', get_queue_runtime_properties.scheduled_message_count,))
self.custom_log_obj.log_info("%s %s" % ('Transfer message count:', get_queue_runtime_properties.transfer_message_count,))
self.custom_log_obj.log_info("%s %s" % ('Transfer DLQ message count:', get_queue_runtime_properties.transfer_dead_letter_message_count,))
self.custom_log_obj.log_info("%s %s" % ('Message count:', get_queue_runtime_properties.total_message_count,))
except AzureError:
self.custom_log_obj.log_info("Not authorized or invalid request to obtaining queue runtime properties")
def get_receiver(self) -> ServiceBusReceiver:
if self.ctx.obj['DEAD_LETTER']:
if self.ctx.obj['USE_SESSION']:
receiver = self.service_bus_client.get_queue_receiver(queue_name=self.ctx.obj['QUEUE_NAME'], session_id=self.ctx.obj['SESSION'],
sub_queue=self.DEAD_LETTER)
else:
receiver = self.service_bus_client.get_queue_receiver(queue_name=self.ctx.obj['QUEUE_NAME'],
sub_queue=self.DEAD_LETTER)
else:
if self.ctx.obj['USE_SESSION']:
receiver = self.service_bus_client.get_queue_receiver(queue_name=self.ctx.obj['QUEUE_NAME'], session_id=self.ctx.obj['SESSION'])
else:
receiver = self.service_bus_client.get_queue_receiver(queue_name=self.ctx.obj['QUEUE_NAME'])
return receiver
def get_receiver_mode(self, receive_mode: str) -> ServiceBusReceiver:
if self.ctx.obj['DEAD_LETTER']:
if self.ctx.obj['USE_SESSION']:
receiver = self.service_bus_client.get_queue_receiver(queue_name=self.ctx.obj['QUEUE_NAME'],
sub_queue=self.DEAD_LETTER, receive_mode=receive_mode, session_id=self.ctx.obj['SESSION'])
else:
receiver = self.service_bus_client.get_queue_receiver(queue_name=self.ctx.obj['QUEUE_NAME'],
sub_queue=self.DEAD_LETTER,
receive_mode=receive_mode)
else:
if self.ctx.obj['USE_SESSION']:
receiver = self.service_bus_client.get_queue_receiver(queue_name=self.ctx.obj['QUEUE_NAME'],
receive_mode=receive_mode, session_id=self.ctx.obj['SESSION'])
else:
receiver = self.service_bus_client.get_queue_receiver(queue_name=self.ctx.obj['QUEUE_NAME'],
receive_mode=receive_mode)
return receiver
def get_sender(self) -> ServiceBusSender:
return self.service_bus_client.get_queue_sender(queue_name=self.ctx.obj['QUEUE_NAME'])
def purge(self):
self.custom_log_obj.log_info("Purge queue started. Wait for completion")
QueueProcess.__purge_recursive(self, self.ctx.obj['MAX_MESSAGE_COUNT'])
self.custom_log_obj.log_info("Purge queue completed")
def __purge_recursive(self, max_message_count):
if self.ctx.obj['TO_DEAD_LETTER']:
receiver = QueueProcess.get_receiver(self)
else:
receiver = QueueProcess.get_receiver_mode(self, self.ServiceBusReceiveMode.RECEIVE_AND_DELETE)
with receiver:
received_msgs = receiver.receive_messages(max_message_count=max_message_count, max_wait_time=5)
len_received_msgs = len(received_msgs)
for msg in received_msgs:
if self.ctx.obj['LOG_PATH'] is not None:
receiver.complete_message(msg)
QueueProcess.log_message(self, msg)
if self.ctx.obj['TO_DEAD_LETTER'] and not self.ctx.obj['DEAD_LETTER']:
receiver.dead_letter_message(msg)
self.custom_log_obj.log_info("%s %s" % ('Length received_msgs: ', len_received_msgs,))
if len_received_msgs is not None and len_received_msgs > 0:
QueueProcess.__purge_recursive(self, max_message_count)
def message(self, input_file):
json_messages = json.load(input_file)
with self.service_bus_client:
sender = self.get_sender()
with sender:
batch_message = sender.create_message_batch()
count = 0
for message in json_messages:
message_parser = ServiceBusMessageParser(**message)
message_obj = message_parser.get_service_bus_message()
if self.ctx.obj['USE_SESSION']:
if message_obj.session_id is None or message_obj.session_id == '':
message_obj.session_id = self.ctx.obj['SESSION']
try:
batch_message.add_message(message_obj)
count += 1
except ValueError:
# ServiceBusMessageBatch object reaches max_size.
# New ServiceBusMessageBatch object can be created here to send more data.
pass
sender.send_messages(batch_message)
self.custom_log_obj.log_info("%s %s %s %s" % ('Messages sent. Size in bytes: ',
batch_message.size_in_bytes, ', Count: ', count))