Exemplo n.º 1
0
 def __init__(self):
     self.queue = QueueService(account_name=self.storage_account_name(),
                               account_key=self.storage_key())
     self.queue_name = os.environ.get('AZURE_STORAGE_QUEUE_NAME',
                                      'training')
     self.queue_message_count = int(
         os.environ.get('AZURE_STORAGE_QUEUE_MESSAGE_COUNT', '1'))
Exemplo n.º 2
0
class StorageQueueTaskSource(TaskSource):
    '''
    Azure Storage Queue implementation of TaskSource.
    '''
    def __init__(self):
        self.queue = QueueService(account_name=self.storage_account_name(),
                                  account_key=self.storage_key())
        self.queue_name = os.environ.get('AZURE_STORAGE_QUEUE_NAME',
                                         'training')
        self.queue_message_count = int(
            os.environ.get('AZURE_STORAGE_QUEUE_MESSAGE_COUNT', '1'))

    @classmethod
    def storage_account_name(self):
        return os.environ.get('AZURE_STORAGE_ACCOUNT_NAME')

    @classmethod
    def storage_key(self):
        return os.environ.get('AZURE_STORAGE_KEY')

    def __str__(self):
        return "Storage Queue (%s)" % self.storage_account_name()

    @classmethod
    def is_supported(self):
        return self.storage_account_name() and self.storage_key()

    def receive(self):
        messages = self.queue.get_messages(self.queue_name,
                                           self.queue_message_count)
        return [
            Task(source=self,
                 content=json.loads(message.content),
                 user_info=message) for message in messages
        ]

    def keep_alive(self, task):
        result = self.queue.update_message(self.queue_name, task.user_info.id,
                                           task.user_info.pop_receipt,
                                           keep_alive_interval_in_seconds)
        task.user_info.pop_receipt = result.pop_receipt

    def commit(self, task):
        self.queue.delete_message(self.queue_name, task.user_info.id,
                                  task.user_info.pop_receipt)
    def create_queue_service(self):
        '''
        Creates a QueueService object with the settings specified in the 
        CloudStorageAccount.

        :return: A service object.
        :rtype: :class:`~azure.storage.queue.queueservice.QueueService`
        '''
        try:
            from azure.storage.queue.queueservice import QueueService
            return QueueService(self.account_name, self.account_key,
                                sas_token=self.sas_token,
                                is_emulated=self.is_emulated)
        except ImportError:
            raise Exception('The package azure-storage-queue is required. '
                            + 'Please install it using "pip install azure-storage-queue"')