本文整理汇总了Python中azure.storage.queue.QueueService.get_messages方法的典型用法代码示例。如果您正苦于以下问题:Python QueueService.get_messages方法的具体用法?Python QueueService.get_messages怎么用?Python QueueService.get_messages使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类azure.storage.queue.QueueService
的用法示例。
在下文中一共展示了QueueService.get_messages方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SimulationListener
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
class SimulationListener(Thread):
def __init__(self):
Thread.__init__(self)
self._quit = Event()
self.daemon = True
self.log = logging.getLogger(__name__)
settings = Settings()
self.create_queue = 'create-simulated-machine'
self.destroy_queue = 'destroy-simulated-machine'
self.queue_service = QueueService(
account_name=settings.azure_queue_account,
account_key=settings.azure_queue_key
)
self.queue_service.create_queue(self.create_queue)
self.queue_service.create_queue(self.destroy_queue)
def run(self):
# dislike of unstoppable threads
while not self._quit.is_set():
try:
messages = self.queue_service.get_messages(self.create_queue, numofmessages=10)
for m in messages:
machine_json = b64decode(m.message_text)
machine = json.loads(machine_json)
self.log.info("Creating: " + machine["Name"] + " on " + machine["Provider"])
self.log.debug(machine_json)
requests.post("http://localhost:8080/machines", machine_json)
self.queue_service.delete_message(self.create_queue, m.message_id, m.pop_receipt)
except Exception as e:
self.log.error(e.message)
try:
messages = self.queue_service.get_messages(self.destroy_queue, numofmessages=10)
for m in messages:
machine_json = b64decode(m.message_text)
machine = json.loads(machine_json)
self.log.info("Deleting: " + machine["Name"] + " on " + machine["Provider"])
self.log.debug(machine_json)
requests.delete("http://localhost:8080/machines/" + machine["MachineId"])
self.queue_service.delete_message(self.destroy_queue, m.message_id, m.pop_receipt)
except Exception as e:
self.log.error(e.message)
sleep(1)
def quit(self):
self._quit.set()
示例2: test_sas_process
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
def test_sas_process(self):
# SAS URL is calculated from storage key, so this test runs live only
if TestMode.need_recordingfile(self.test_mode):
return
# Arrange
self.qs.put_message(self.test_queues[0], 'message1')
token = self.qs.generate_shared_access_signature(
self.test_queues[0],
self._get_shared_access_policy(QueueSharedAccessPermissions.PROCESS),
)
# Act
service = QueueService(
account_name=self.settings.STORAGE_ACCOUNT_NAME,
sas_token=token,
)
self._set_service_options(service, self.settings)
result = service.get_messages(self.test_queues[0])
# Assert
self.assertIsNotNone(result)
self.assertEqual(1, len(result))
message = result[0]
self.assertIsNotNone(message)
self.assertNotEqual('', message.message_id)
self.assertEqual('message1', message.message_text)
示例3: test_sas_process
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
def test_sas_process(self):
# SAS URL is calculated from storage key, so this test runs live only
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
queue_name = self._create_queue()
self.qs.put_message(queue_name, u'message1')
token = self.qs.generate_queue_shared_access_signature(
queue_name,
QueuePermissions.PROCESS,
datetime.utcnow() + timedelta(hours=1),
)
# Act
service = QueueService(
account_name=self.settings.STORAGE_ACCOUNT_NAME,
sas_token=token,
)
self._set_service_options(service, self.settings)
result = service.get_messages(queue_name)
# Assert
self.assertIsNotNone(result)
self.assertEqual(1, len(result))
message = result[0]
self.assertIsNotNone(message)
self.assertNotEqual('', message.id)
self.assertEqual(u'message1', message.content)
示例4: sas_with_signed_identifiers
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
def sas_with_signed_identifiers(self):
queue_name = self._create_queue()
self.service.put_message(queue_name, u'message1')
# Set access policy on queue
access_policy = AccessPolicy(permission=QueuePermissions.PROCESS,
expiry=datetime.utcnow() + timedelta(hours=1))
identifiers = {'id': access_policy}
acl = self.service.set_queue_acl(queue_name, identifiers)
# Wait 30 seconds for acl to propagate
time.sleep(30)
# Indicates to use the access policy set on the queue
token = self.service.generate_queue_shared_access_signature(
queue_name,
id='id'
)
# Create a service and use the SAS
sas_service = QueueService(
account_name=self.account.account_name,
sas_token=token,
)
messages = sas_service.get_messages(queue_name)
for message in messages:
print(message.content) # message1
self.service.delete_queue(queue_name)
示例5: AzureProvider
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
class AzureProvider(BaseProvider):
"""Queue Provider for the Microsoft Azure."""
def __init__(self, account_name, account_key):
self.queue_service = QueueService(
account_name=account_name, account_key=account_key
)
def put_message(self, queue_name: str, message: str):
self.queue_service.put_message(
queue_name=queue_name,
message_text=message,
)
def get_message(self, queue_name: str):
try:
queue_message = self.queue_service.get_messages(
queue_name=queue_name, numofmessages=1
).queue_messages[0]
except IndexError:
return {}
else:
return {
attr_name: getattr(queue_message, attr_name)
for attr_name in dir(queue_message)
if not attr_name.startswith('_')
}
def delete_message(self, queue_name: str, message: dict):
message_id = message.get('message_id')
pop_receipt = message.get('pop_receipt')
self.queue_service.delete_message(
queue_name, message_id=message_id, popreceipt=pop_receipt,
)
示例6: CheckinQueueClient
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
class CheckinQueueClient(object):
"""Obtains next checkin object from queue"""
queueName = 'checkinqueue'
def __init__(self, account_name, account_key):
self._queueService = QueueService(account_name, account_key)
def startCheckinProcess(processData):
while(True):
message = self._queueService.get_messages(queueName, num_messages=1, visibility_timeout=30)
processData(message)
示例7: RegistrationQueueClient
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
class RegistrationQueueClient(object):
"""Obtains next registration object from queue"""
queueName = 'registrationsqueue'
def __init__(self, account_name, account_key):
self._queueService = QueueService(account_name, account_key)
print("RegistrationQueue Initialized")
def LookupTicket(self, processData):
message = self._queueService.get_messages(self.queueName, num_messages=1)
if(processData(message)):
self._queueService.delete_message(self.queueName, message.message_id, message.pop_receipt)
示例8: __init__
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
class TimeLineWorker:
QUEUE_KEY = ''
ACCOUNT_NAME = ''
QUEUE_NAME = ''
def __init__(self):
self.queue_service = QueueService(account_name=TimeLineWorker.ACCOUNT_NAME,
account_key=TimeLineWorker.QUEUE_KEY)
def insert_message(self):
obj = {
"message": "test message",
"other_key": 10
}
message = unicode(json.dumps(obj))
self.queue_service.put_message(TimeLineWorker.QUEUE_NAME, message)
def get_next_message(self):
messages = self.queue_service.get_messages(TimeLineWorker.QUEUE_NAME)
for message in messages:
print message.content
self.queue_service.delete_message(TimeLineWorker.QUEUE_NAME, message.id, message.pop_receipt)
示例9: queue_sas
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
def queue_sas(self):
queue_name = self._create_queue()
self.service.put_message(queue_name, u'message1')
# Access only to the messages in the given queue
# Process permissions to access messages
# Expires in an hour
token = self.service.generate_queue_shared_access_signature(
queue_name,
QueuePermissions.PROCESS,
datetime.utcnow() + timedelta(hours=1),
)
# Create a service and use the SAS
sas_service = QueueService(
account_name=self.account.account_name,
sas_token=token,
)
messages = sas_service.get_messages(queue_name)
for message in messages:
print(message.content) # message1
self.service.delete_queue(queue_name)
示例10: __init__
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
class Queue:
def __init__(self, account_name, account_key, queue_name="logqueue"):
"""Initialiaze a queue. The type is set by the
'ACS_LOGGING_QUEUE_TYPE' environment variable. If it is set to
'AzureStorageQueue' then values must be provided for
'account_name' and 'account_key' which are values associated
with the Azure Storage account. 'queue_name' is optional and
defaults to 'logqueue'.
"""
self.log = Log()
self.queue_type = config.ACS_LOGGING_QUEUE_TYPE
self.queue_name = queue_name
# self.log.debug("Queue type: " + self.queue_type + " / " + self.queue_name)
if self.queue_type == "AzureStorageQueue":
self.createAzureQueues(account_name, account_key)
elif self.queue_type == "LocalFile":
self.file_queue = open(config.UNPROCESSED_LOG_FILE, "w+")
else:
self.log.error("Unknown queue type: " + queue_type)
def createAzureQueues(self, account_name, account_key):
"""
Create a queue for unprocessed log messages. Entries in the queue
will be dequeued, processed and deleted upon success.
"""
self.queue_service = QueueService(account_name, account_key)
self.queue_service.create_queue(self.queue_name)
def close(self):
"""Perform any necessary clearnup on the queue
at the end of a run.
"""
if self.queue_type == "AzureStorageQueue":
pass
elif self.queue_type == "LocalFile":
self.file_queue.close()
else:
self.log.error("Unknown queue type: " + queue_type)
def enqueue(self, msg, level="INFO"):
msg = level + " - " + msg
if self.queue_type == "LocalFile":
file_queue.write(msg + "\n")
elif self.queue_type == "AzureStorageQueue":
self.queue_service.put_message(self.queue_name, msg)
self.log.debug(msg)
def dequeue(self):
messages = []
if self.queue_type == "LocalFile":
with open(config.UNPROCESSED_LOG_FILE, "r") as f:
messages = f.readlines()[1]
elif self.queue_type == "AzureStorageQueue":
messages = self.queue_service.get_messages(self.queue_name)
return messages
def delete(self, message):
self.queue_service.delete_message(self.queue_name, message.message_id, message.pop_receipt)
# with open(config.PROCESSED_LOG_FILE, 'a') as processed:
# processed.write(log)
# os.remove(config.UNPROCESSED_LOG_FILE)
def delete_queue(self, queue_name):
self.queue_service.delete_queue(queue_name, False)
def getLength(self):
"""
Get the approximate length of the queue
"""
queue_metadata = self.queue_service.get_queue_metadata(self.queue_name)
count = queue_metadata["x-ms-approximate-messages-count"]
return count
示例11: QueueService
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
# Pedro Perez - 2015
#
# -----------------------------------------------------------------------------
import sys
import os
import subprocess
from azure.storage.queue import QueueService
account_name = "mcdockerqueue"
account_key = sys.argv[1]
queuename = "servers"
queue_service = QueueService(account_name, account_key)
queue_metadata = queue_service.get_queue_metadata(queuename)
count = queue_metadata['x-ms-approximate-messages-count']
print "There are %s messages in the queue" % count
if count > 0:
messages = queue_service.get_messages(queuename)
for message in messages:
print(message.message_text)
# Remove message from the queue
queue_service.delete_message(queuename, message.message_id, message.pop_receipt)
else:
print "There are no messages to process"
示例12: QueueStorageHandlerTest
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
class QueueStorageHandlerTest(_TestCase):
def setUp(self):
self.service = QueueService(account_name=ACCOUNT_NAME,
account_key=ACCOUNT_KEY,
is_emulated=_EMULATED)
# ensure that there's no message on the queue before each test
queues = set()
for cfg in LOGGING['handlers'].values():
if 'queue' in cfg:
queues.add(cfg['queue'])
for queue in self.service.list_queues():
if queue.name in queues:
self.service.clear_messages(queue.name)
def test_logging(self):
# get the logger for the test
logger_name = 'queue'
logger = logging.getLogger(logger_name)
handler_name = _get_handler_name(logger_name)
# perform logging
log_text = 'logging test'
logger.info(log_text)
# confirm that the massage has correct log text
queue = _get_handler_config_value(handler_name, 'queue')
messages = iter(self.service.get_messages(queue))
message = next(messages)
text_expected = "INFO %s" % log_text
if _get_handler_config_value(handler_name, 'base64_encoding'):
text_expected = _base64_encode(text_expected)
self.assertEqual(message.content, text_expected)
# confirm that there's no more message in the queue
with self.assertRaises(StopIteration):
next(messages)
def test_message_ttl(self):
# get the logger for the test
logger_name = 'message_ttl'
logger = logging.getLogger(logger_name)
handler_name = _get_handler_name(logger_name)
# perform logging
log_text = 'time-to-live test'
logger.info(log_text)
# confirm that the new message is visible till the ttl expires
queue = _get_handler_config_value(handler_name, 'queue')
messages = iter(self.service.get_messages(queue))
message = next(messages)
text_expected = 'INFO %s' % log_text
if _get_handler_config_value(handler_name, 'base64_encoding'):
text_expected = _base64_encode(text_expected)
self.assertEqual(message.content, text_expected)
# confirm that there's no more message in the queue
with self.assertRaises(StopIteration):
next(messages)
# confirm that the new message is invisible after the ttl expires
ttl = _get_handler_config_value(handler_name, 'message_ttl')
time.sleep(int(ttl)+5)
messages = iter(self.service.get_messages(queue))
with self.assertRaises(StopIteration):
next(messages)
def test_visibility_timeout(self):
# get the logger for the test
logger_name = 'visibility_timeout'
logger = logging.getLogger(logger_name)
handler_name = _get_handler_name(logger_name)
# perform logging
log_text = 'visibility test'
logger.info(log_text)
# confirm that the new message is invisible till the timeout expires
queue = _get_handler_config_value(handler_name, 'queue')
messages = iter(self.service.get_messages(queue))
with self.assertRaises(StopIteration):
next(messages)
# confirm that the new message is visible after the timeout expires
timeout = _get_handler_config_value(handler_name, 'visibility_timeout')
time.sleep(int(timeout)+5)
messages = iter(self.service.get_messages(queue))
message = next(messages)
text_expected = 'INFO %s' % log_text
if _get_handler_config_value(handler_name, 'base64_encoding'):
text_expected = _base64_encode(text_expected)
self.assertEqual(message.content, text_expected)
# confirm that there's no more message in the queue
with self.assertRaises(StopIteration):
next(messages)
def test_base64_encoding(self):
# get the logger for the test
#.........这里部分代码省略.........
示例13: QueueService
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
delay = os.environ['delay']
else:
delay = 0
if "docker" in os.environ:
docker = os.environ['docker']
if "chronos" in os.environ:
chronos = os.environ['chronos']
while True:
# set up azure queue
queue_service = QueueService(account_name=azurestoracct, account_key=azurequeuekey)
# get messages from azure queue
messages = queue_service.get_messages(azurequeue, num_messages=5)
# delete from queue, create container, start container
for message in messages:
# delete message from azure queue
queue_service.delete_message(azurequeue, message.id, message.pop_receipt)
if "docker" in os.environ:
# sample json
# {"Image": "neilpeterson/stock-report","Cmd": ["--symbols=msft;lnkd", "[email protected]"],"Env": ["gmuser = [email protected]", "gmpass = TempForDemo2016"]}
s = message.content.split(':')
data = json.loads('{"Image": "' + docker_image + '","Cmd": ["--symbols=' + s[0] +'", "--email=' + s[1] + '","--delay=' + str(delay) + '"]}')
print(data)
示例14: QueueService
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import get_messages [as 别名]
'redis_pass': os.environ['REDIS_PASS'],
'redis_server': os.environ['REDIS_SERVER'],
'sms_user': os.environ['SMS_USER'],
'sms_pass': os.environ['SMS_PASS']
}
stor_acc_name = service_keys['stor_acc_name']
stor_acc_key = service_keys['stor_acc_key']
redis_pass = service_keys['redis_pass']
redis_server = service_keys['redis_server']
sms_user = service_keys['sms_user']
sms_pass = service_keys['sms_pass']
# storage
queue_service = QueueService(account_name=stor_acc_name, account_key=stor_acc_key)
# redis
r = redis.StrictRedis(host=redis_server, port=6380, db=0, password=redis_pass, ssl=True)
while True:
messages = queue_service.get_messages('taskqueue', numofmessages=16, visibilitytimeout=5*60)
for message in messages:
d = json.loads(message.message_text)
suffix = d['suffix']
mobile = r.get(suffix)
image = d['image']
odp = str(image)
payload = {'username': sms_user, 'password': sms_pass, 'from': 'Alert', 'to': mobile, 'message': odp}
post = requests.post('https://api.smsapi.pl/sms.do', data=payload)
queue_service.delete_message('taskqueue', message.message_id, message.pop_receipt)