private async Task StartAsyncCore(CancellationToken cancellationToken)
 {
     ListenerFactoryContext context = new ListenerFactoryContext(cancellationToken);
     _listener = await _factory.CreateAsync(context);
     _cancellationRegistration = _cancellationSource.Token.Register(_listener.Cancel);
     await _listener.StartAsync(cancellationToken);
 }
 public Task<IListener> CreateListenerAsync(ListenerFactoryContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     return Task.FromResult<IListener>(new FileListener(_config, _attribute, context.Executor));
 }
 public Task<IListener> CreateListenerAsync(ListenerFactoryContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     return Task.FromResult<IListener>(new TimerListener(_attribute, context.Descriptor.Id, _config, context.Executor, _trace));
 }
 public async Task CreateAsync_AccessRightsNotManage_DoesNotCreateQueue(AccessRights accessRights)
 {
     ServiceBusAccount account = new ServiceBusAccount();
     Mock<ITriggeredFunctionExecutor> mockExecutor = new Mock<ITriggeredFunctionExecutor>(MockBehavior.Strict);
     ServiceBusQueueListenerFactory factory = new ServiceBusQueueListenerFactory(account, "testqueue", mockExecutor.Object, accessRights);
 
     ListenerFactoryContext context = new ListenerFactoryContext(CancellationToken.None);
     IListener listener = await factory.CreateAsync(context);
     Assert.NotNull(listener);
 }
        public async Task CreateAsync_AccessRightsNotManage_DoesNotCreateTopicOrSubscription(AccessRights accessRights)
        {
            ServiceBusAccount account = new ServiceBusAccount();
            Mock<ITriggeredFunctionExecutor<BrokeredMessage>> mockExecutor = new Mock<ITriggeredFunctionExecutor<BrokeredMessage>>(MockBehavior.Strict);
            ServiceBusSubscriptionListenerFactory factory = new ServiceBusSubscriptionListenerFactory(account, "testtopic", "testsubscription", mockExecutor.Object, accessRights);

            ListenerFactoryContext context = new ListenerFactoryContext(CancellationToken.None);
            IListener listener = await factory.CreateAsync(context);
            Assert.NotNull(listener);
        }
        public async Task<IListener> CreateAsync(ListenerFactoryContext context)
        {
            List<IListener> listeners = new List<IListener>();

            foreach (IListenerFactory listenerFactory in _listenerFactories)
            {
                IListener listener = await listenerFactory.CreateAsync(context);
                listeners.Add(listener);
            }

            return new CompositeListener(listeners);
        }
        public async Task<IListener> CreateAsync(ListenerFactoryContext context)
        {
            if (_accessRights == AccessRights.Manage)
            {
                // Must create all messaging entities before creating message receivers and calling OnMessage.
                // Otherwise, some function could start to execute and try to output messages to entities that don't yet
                // exist.
                await _namespaceManager.CreateQueueIfNotExistsAsync(_queueName, context.CancellationToken);
            }

            ServiceBusTriggerExecutor triggerExecutor = new ServiceBusTriggerExecutor(_executor);
            return new ServiceBusListener(_messagingFactory, _queueName, triggerExecutor);
        }
        public async Task<IDelayedException> TryExecuteAsync(IFunctionInstance instance, CancellationToken cancellationToken)
        {
            IDelayedException result;

            ListenerFactoryContext context = new ListenerFactoryContext(cancellationToken);
            using (IListener listener = await _abortListenerFactory.CreateAsync(context))
            {
                await listener.StartAsync(cancellationToken);

                result = await _innerExecutor.TryExecuteAsync(instance, cancellationToken);

                await listener.StopAsync(cancellationToken);
            }

            return result;
        }
        public async Task<IListener> CreateAsync(ListenerFactoryContext context)
        {
            if (_accessRights == AccessRights.Manage)
            {
                // Must create all messaging entities before creating message receivers and calling OnMessage.
                // Otherwise, some function could start to execute and try to output messages to entities that don't yet
                // exist.
                await _namespaceManager.CreateTopicIfNotExistsAsync(_topicName, context.CancellationToken);
                await _namespaceManager.CreateSubscriptionIfNotExistsAsync(_topicName, _subscriptionName, context.CancellationToken);
            }

            string entityPath = SubscriptionClient.FormatSubscriptionPath(_topicName, _subscriptionName);

            ServiceBusTriggerExecutor triggerExecutor = new ServiceBusTriggerExecutor(_executor);
            return new ServiceBusListener(_messagingFactory, entityPath, triggerExecutor);
        }
 public Task<IListener> CreateListenerAsync(ListenerFactoryContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     
     IListener listener;
     
     if (_mode == Mode.PubSub)
     {
         listener = new RedisChannelListener(_channelOrKey, context.Executor, _config, _trace);
     }
     else
     {
         listener = new RedisCacheListener(_channelOrKey, context.Executor, _config, _trace);
     }
     return Task.FromResult(listener);
 }
        public async Task<IListener> CreateAsync(ListenerFactoryContext context)
        {
            List<IListener> listeners = new List<IListener>();

            foreach (IFunctionDefinition functionDefinition in _functionDefinitions)
            {
                IListenerFactory listenerFactory = functionDefinition.ListenerFactory;

                if (listenerFactory == null)
                {
                    continue;
                }

                IListener listener = await listenerFactory.CreateAsync(context);
                listeners.Add(listener);
            }

            return new CompositeListener(listeners);
        }
        public Task<IListener> CreateAsync(ListenerFactoryContext context)
        {
            ITriggerExecutor<IStorageQueueMessage> triggerExecutor = new HostMessageExecutor(_executor, _functionLookup, _functionInstanceLogger);

            TimeSpan configuredMaximum = _queueConfiguration.MaxPollingInterval;
            // Provide an upper bound on the maximum polling interval for run/abort from dashboard.
            // Use the default maximum for host polling (1 minute) unless the configured overall maximum is even faster.
            TimeSpan maximum = configuredMaximum < DefaultMaximum ? configuredMaximum : DefaultMaximum;
            IDelayStrategy delayStrategy = new RandomizedExponentialBackoffStrategy(Minimum, maximum);

            IListener listener = new QueueListener(_queue,
                poisonQueue: null,
                triggerExecutor: triggerExecutor,
                delayStrategy: delayStrategy,
                backgroundExceptionDispatcher: _backgroundExceptionDispatcher,
                log: _log,
                sharedWatcher: null,
                queueConfiguration: _queueConfiguration);

            return Task.FromResult(listener);
        }
 public Task<IListener> CreateAsync(ListenerFactoryContext context)
 {
     IListener listener = new NullListener();
     return Task.FromResult(listener);
 }
        public async Task<IListener> CreateAsync(ListenerFactoryContext context)
        {
            SharedQueueWatcher sharedQueueWatcher = _sharedContextProvider.GetOrCreate<SharedQueueWatcher>(
                new SharedQueueWatcherFactory(_messageEnqueuedWatcherSetter));
            SharedBlobListener sharedBlobListener = _sharedContextProvider.GetOrCreate<SharedBlobListener>(
                new SharedBlobListenerFactory(_account, _backgroundExceptionDispatcher, _blobWrittenWatcherSetter));

            // Note that these clients are intentionally for the storage account rather than for the dashboard account.
            // We use the storage, not dashboard, account for the blob receipt container and blob trigger queues.
            IStorageQueueClient queueClient = _account.CreateQueueClient();
            IStorageBlobClient blobClient = _account.CreateBlobClient();

            string hostId = await _hostIdProvider.GetHostIdAsync(context.CancellationToken);
            string hostBlobTriggerQueueName = HostQueueNames.GetHostBlobTriggerQueueName(hostId);
            IStorageQueue hostBlobTriggerQueue = queueClient.GetQueueReference(hostBlobTriggerQueueName);

            IListener blobDiscoveryToQueueMessageListener = await CreateBlobDiscoveryToQueueMessageListenerAsync(
                hostId, sharedBlobListener, blobClient, hostBlobTriggerQueue, sharedQueueWatcher, context.CancellationToken);
            IListener queueMessageToTriggerExecutionListener = CreateQueueMessageToTriggerExecutionListener(
                _sharedContextProvider, sharedQueueWatcher, queueClient, hostBlobTriggerQueue, blobClient,
                sharedBlobListener.BlobWritterWatcher);
            IListener compositeListener = new CompositeListener(
                blobDiscoveryToQueueMessageListener,
                queueMessageToTriggerExecutionListener);

            return compositeListener;
        }
        public Task<IListener> CreateListenerAsync(ListenerFactoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            IStorageBlobContainer container = _client.GetContainerReference(_path.ContainerNamePattern);

            var factory = new BlobListenerFactory(_hostIdProvider, _queueConfiguration,
                _backgroundExceptionDispatcher, _blobWrittenWatcherSetter, _messageEnqueuedWatcherSetter,
                _sharedContextProvider, _log, context.Descriptor.Id, _account, container, _path, context.Executor);

            return factory.CreateAsync(context.CancellationToken);
        }
        public Task<IListener> CreateListenerAsync(ListenerFactoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            WebHookListener listener = new WebHookListener(_dispatcher, (MethodInfo)_parameter.Member, _route, context.Executor);
            return Task.FromResult<IListener>(listener);
        }
        public Task<IListener> CreateListenerAsync(ListenerFactoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var factory = new QueueListenerFactory(_queue, _queueConfiguration, _backgroundExceptionDispatcher, 
                    _messageEnqueuedWatcherSetter, _sharedContextProvider, _log, context.Executor);

            return factory.CreateAsync(context.CancellationToken);
        }
 public Task<IListener> CreateListenerAsync(ListenerFactoryContext context)
 {
     return Task.FromResult<IListener>(new Listener(context.Executor));
 }
            public Task<IListener> CreateListenerAsync(ListenerFactoryContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }

                return Task.FromResult<IListener>(new ErrorTriggerListener(_config, _parameter, context.Executor));
            }
            public Task<IListener> CreateListenerAsync(ListenerFactoryContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }

                return Task.FromResult<IListener>(new NullListener());
            }
        public Task<IListener> CreateListenerAsync(ListenerFactoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            IListenerFactory factory = null;
            if (_queueName != null)
            {
                factory = new ServiceBusQueueListenerFactory(_account, _queueName, context.Executor, _accessRights);           
            }
            else
            {
                factory = new ServiceBusSubscriptionListenerFactory(_account, _topicName, _subscriptionName, context.Executor, _accessRights);
            }
            return factory.CreateAsync(context.CancellationToken);
        }