Example #1
0
 public FunctionInfoTest(IServiceContainer services)
 {
     _shell         = services.GetService <ICoreShell>();
     _workflow      = UIThreadHelper.Instance.Invoke(() => _shell.GetService <IRInteractiveWorkflowProvider>().GetOrCreate());
     _packageIndex  = _shell.GetService <IPackageIndex>();
     _functionIndex = _shell.GetService <IFunctionIndex>();
 }
Example #2
0
        public PackageIndex(IServiceContainer services, IIntellisenseRSession host, IFunctionIndex functionIndex)
        {
            _host          = host;
            _functionIndex = functionIndex;
            _idleTime      = services.GetService <IIdleTimeService>();

            var interactiveWorkflowProvider = services.GetService <IRInteractiveWorkflowProvider>();
            var workflow = interactiveWorkflowProvider.GetOrCreate();

            var sessionProvider    = workflow.RSessions;
            var interactiveSession = workflow.RSession;

            interactiveSession.Connected         += OnSessionConnected;
            interactiveSession.PackagesInstalled += OnPackagesChanged;
            interactiveSession.PackagesRemoved   += OnPackagesChanged;

            sessionProvider.BrokerStateChanged += OnBrokerStateChanged;

            if (interactiveSession.IsHostRunning)
            {
                BuildIndexAsync().DoNotWait();
            }

            _disposableBag
            .Add(() => interactiveSession.PackagesInstalled -= OnPackagesChanged)
            .Add(() => interactiveSession.PackagesRemoved   -= OnPackagesChanged)
            .Add(() => interactiveSession.Connected         -= OnSessionConnected)
            .Add(() => sessionProvider.BrokerStateChanged   -= OnBrokerStateChanged)
            .Add(_host);
        }
Example #3
0
 protected FunctionIndexBasedTest(AssemblyMefCatalogFixture catalog)
 {
     _exportProvider = catalog.CreateExportProvider();
     _editorShell    = _exportProvider.GetExportedValue <IEditorShell>();
     _packageIndex   = _exportProvider.GetExportedValue <IPackageIndex>();
     _functionIndex  = _exportProvider.GetExportedValue <IFunctionIndex>();
 }
 public RFunctionCompletionEntry(string displayText, string insertionText, string description, object iconSource, string packageName, IFunctionIndex functionIndex, IEditorIntellisenseSession session) :
     base(displayText, insertionText, description, iconSource)
 {
     _packageName   = packageName;
     _functionIndex = functionIndex;
     _session       = session;
 }
Example #5
0
        public async Task <string> GetHostIdAsync(CancellationToken cancellationToken)
        {
            IStorageAccount account;

            try
            {
                account = await _storageAccountProvider.GetStorageAccountAsync(cancellationToken);
            }
            catch (InvalidOperationException exception)
            {
                throw new InvalidOperationException(
                          "A host ID is required. Either set JobHostConfiguration.HostId or provide a valid storage " +
                          "connection string.", exception);
            }

            IFunctionIndex index = await _getFunctionIndexProvider.Invoke().GetAsync(cancellationToken);

            IEnumerable <MethodInfo> indexedMethods = index.ReadAllMethods();

            string sharedHostName           = GetSharedHostName(indexedMethods, account);
            IStorageBlobDirectory directory = account.CreateBlobClient().GetContainerReference(
                HostContainerNames.Hosts).GetDirectoryReference(HostDirectoryNames.Ids);

            return(await GetOrCreateHostIdAsync(sharedHostName, directory, cancellationToken));
        }
Example #6
0
 public FunctionInfoTest(RSupportMefCatalogFixture catalog)
 {
     _exportProvider = catalog.CreateExportProvider();
     UIThreadHelper.Instance.Invoke(() => _exportProvider.GetExportedValue <IRInteractiveWorkflowProvider>().GetOrCreate());
     _packageIndex  = _exportProvider.GetExportedValue <IPackageIndex>();
     _functionIndex = _exportProvider.GetExportedValue <IFunctionIndex>();
 }
Example #7
0
 public FunctionInfoTest(IExportProvider exportProvider)
 {
     _exportProvider = exportProvider;
     _workflow       = UIThreadHelper.Instance.Invoke(() => _exportProvider.GetExportedValue <IRInteractiveWorkflowProvider>().GetOrCreate());
     _packageIndex   = _exportProvider.GetExportedValue <IPackageIndex>();
     _functionIndex  = _exportProvider.GetExportedValue <IFunctionIndex>();
 }
        public async Task <IFunctionIndex> GetAsync(CancellationToken cancellationToken)
        {
            if (_index == null)
            {
                _index = await CreateAsync(cancellationToken);
            }

            return(_index);
        }
        public async Task<IFunctionIndex> GetAsync(CancellationToken cancellationToken)
        {
            if (_index == null)
            {
                _index = await CreateAsync(cancellationToken);
            }

            return _index;
        }
Example #10
0
        public PackageIndex(IRInteractiveWorkflowProvider interactiveWorkflowProvider, ICoreShell shell, IIntellisenseRSession host, IFunctionIndex functionIndex)
        {
            _shell         = shell;
            _host          = host;
            _functionIndex = functionIndex;

            _interactiveSession = interactiveWorkflowProvider.GetOrCreate().RSession;

            _interactiveSession.PackagesInstalled += OnPackagesChanged;
            _interactiveSession.PackagesRemoved   += OnPackagesChanged;
        }
Example #11
0
        public bool Init(IFunctionIndex functions, IListenerFactory functionsListenerFactory, out IFunctionExecutor hostCallExecutor, out IListener listener, out HostOutputMessage hostOutputMessage, string hostId, CancellationToken shutdownToken)
        {
            hostCallExecutor = new ShutdownFunctionExecutor(shutdownToken, _functionExecutor);

            IListener factoryListener  = new ListenerFactoryListener(functionsListenerFactory, _sharedQueueHandler);
            IListener shutdownListener = new ShutdownListener(shutdownToken, factoryListener);

            listener          = shutdownListener;
            hostOutputMessage = new DataOnlyHostOutputMessage();
            return(false);
        }
Example #12
0
        public PackageFunctionCompletionProvider(IServiceContainer serviceContainer)
        {
            _session = serviceContainer.GetService <IIntellisenseRSession>();
            _snippetInformationSource = serviceContainer.GetService <ISnippetInformationSourceProvider>();
            _packageIndex             = serviceContainer.GetService <IPackageIndex>();
            _functionIndex            = serviceContainer.GetService <IFunctionIndex>();

            var imageService = serviceContainer.GetService <IImageService>();

            _functionGlyph = imageService.GetImage(ImageType.Method);
            _constantGlyph = imageService.GetImage(ImageType.Constant);
        }
Example #13
0
        public static void GetFunctionInfoAsync(this IFunctionIndex functionIndex, string functionName, string packageName, Action <IFunctionInfo, object> callback, object parameter = null)
        {
            var fi = functionIndex.GetFunctionInfo(functionName, packageName);

            if (fi != null)
            {
                callback(fi, parameter);
            }
            else
            {
                GetFunctionInfoFromPackageAsync(functionIndex, functionName, packageName, callback, parameter).DoNotWait();
            }
        }
Example #14
0
 public PackageFunctionCompletionProvider(
     ILoadedPackagesProvider loadedPackagesProvider,
     [Import(AllowDefault = true)] ISnippetInformationSourceProvider snippetInformationSource,
     IPackageIndex packageIndex,
     IFunctionIndex functionIndex,
     ICoreShell shell)
 {
     _loadedPackagesProvider   = loadedPackagesProvider;
     _snippetInformationSource = snippetInformationSource;
     _shell         = shell;
     _packageIndex  = packageIndex;
     _functionIndex = functionIndex;
 }
Example #15
0
        public static Task<IFunctionInfo> GetFunctionInfoAsync(IFunctionIndex functionIndex, string functionName) {
            var tcs = new TaskCompletionSource<IFunctionInfo>();
            var result = functionIndex.GetFunctionInfo(functionName, null, (o, p) => {
                var r = functionIndex.GetFunctionInfo(functionName, p);
                tcs.TrySetResult(r);
            });

            if (result != null) {
                tcs.TrySetResult(result);
            }

            return tcs.Task;
        }
Example #16
0
        private static async Task GetFunctionInfoFromPackageAsync(IFunctionIndex functionIndex, string functionName, string packageName, Action <IFunctionInfo, object> callback, object parameter)
        {
            IFunctionInfo fi = null;

            packageName = packageName ?? await functionIndex.GetPackageNameAsync(functionName);

            if (!string.IsNullOrEmpty(packageName))
            {
                fi = functionIndex.GetFunctionInfo(functionName, packageName);
            }
            await functionIndex.Services.MainThread().SwitchToAsync();

            callback(fi, parameter);
        }
        public PackageFunctionCompletionProvider(
            ILoadedPackagesProvider loadedPackagesProvider,
            [Import(AllowDefault = true)] ISnippetInformationSourceProvider snippetInformationSource,
            IPackageIndex packageIndex,
            IFunctionIndex functionIndex,
            IGlyphService glyphService) {
            _loadedPackagesProvider = loadedPackagesProvider;
            _snippetInformationSource = snippetInformationSource;
            _packageIndex = packageIndex;
            _functionIndex = functionIndex;

            _functionGlyph = glyphService.GetGlyphThreadSafe(StandardGlyphGroup.GlyphGroupMethod, StandardGlyphItem.GlyphItemPublic);
            _constantGlyph = glyphService.GetGlyphThreadSafe(StandardGlyphGroup.GlyphGroupConstant, StandardGlyphItem.GlyphItemPublic);
        }
Example #18
0
        public PackageFunctionCompletionProvider(
            ILoadedPackagesProvider loadedPackagesProvider,
            [Import(AllowDefault = true)] ISnippetInformationSourceProvider snippetInformationSource,
            IPackageIndex packageIndex,
            IFunctionIndex functionIndex,
            IGlyphService glyphService)
        {
            _loadedPackagesProvider   = loadedPackagesProvider;
            _snippetInformationSource = snippetInformationSource;
            _packageIndex             = packageIndex;
            _functionIndex            = functionIndex;

            _functionGlyph = glyphService.GetGlyphThreadSafe(StandardGlyphGroup.GlyphGroupMethod, StandardGlyphItem.GlyphItemPublic);
            _constantGlyph = glyphService.GetGlyphThreadSafe(StandardGlyphGroup.GlyphGroupConstant, StandardGlyphItem.GlyphItemPublic);
        }
Example #19
0
        public static Task <IFunctionInfo> GetFunctionInfoAsync(IFunctionIndex functionIndex, string functionName)
        {
            var tcs    = new TaskCompletionSource <IFunctionInfo>();
            var result = functionIndex.GetFunctionInfo(functionName, o => {
                var r = functionIndex.GetFunctionInfo(functionName);
                tcs.TrySetResult(r);
            });

            if (result != null)
            {
                tcs.TrySetResult(result);
            }

            return(tcs.Task);
        }
        private static async Task GetFunctionInfoFromPackageAsync(IFunctionIndex functionIndex
                                                                  , string functionName
                                                                  , string packageName
                                                                  , Action <IFunctionInfo, object> callback
                                                                  , object parameter)
        {
            IFunctionInfo fi = null;

            packageName = packageName ?? await functionIndex.GetPackageNameAsync(functionName);

            if (!string.IsNullOrEmpty(packageName))
            {
                fi = await functionIndex.GetFunctionInfoAsync(functionName, packageName);
            }
            callback(fi, parameter);
        }
        public PackageFunctionCompletionProvider(
            IIntellisenseRSession session,
            [Import(AllowDefault = true)] ISnippetInformationSourceProvider snippetInformationSource,
            IPackageIndex packageIndex,
            IFunctionIndex functionIndex,
            ICoreShell coreShell)
        {
            _session = session;
            _snippetInformationSource = snippetInformationSource;
            _packageIndex             = packageIndex;
            _functionIndex            = functionIndex;

            var imageService = coreShell.GetService <IImageService>();

            _functionGlyph = imageService.GetImage(ImageType.Method) as ImageSource;
            _constantGlyph = imageService.GetImage(ImageType.Constant) as ImageSource;;
        }
Example #22
0
        public PackageIndex(IRInteractiveWorkflowProvider interactiveWorkflowProvider, ICoreShell shell, IIntellisenseRSession host, IFunctionIndex functionIndex) {
            _shell = shell;
            _host = host;
            _functionIndex = functionIndex;

            _workflow = interactiveWorkflowProvider.GetOrCreate();

            _interactiveSession = _workflow.RSession;
            _interactiveSession.Connected += OnSessionConnected;
            _interactiveSession.PackagesInstalled += OnPackagesChanged;
            _interactiveSession.PackagesRemoved += OnPackagesChanged;

            _workflow.RSessions.BrokerStateChanged += OnBrokerStateChanged;

            if (_workflow.RSession.IsHostRunning) {
                BuildIndexAsync().DoNotWait();
            }
        }
Example #23
0
        public async Task<IEditorScript> StartScript(IExportProvider exportProvider, string text, string filename, string contentType, IRSessionProvider sessionProvider) {
            var shell = exportProvider.GetExportedValue<ICoreShell>();
            var coreEditor = await InUI(() => new CoreEditor(shell, text, filename, contentType));
            var containerDisposable = await AddToHost(coreEditor.Control);

            if (sessionProvider != null) {
                IntelliSenseRSession.HostStartTimeout = 10000;
                HostScript = new RHostScript(sessionProvider);

                PackageIndex = exportProvider.GetExportedValue<IPackageIndex>();
                await PackageIndex.BuildIndexAsync();

                FunctionIndex = exportProvider.GetExportedValue<IFunctionIndex>();
                await FunctionIndex.BuildIndexAsync();
            }

            return new EditorScript(exportProvider, coreEditor, containerDisposable);
        }
        public static async Task <IFunctionInfo> GetFunctionInfoAsync(this IFunctionIndex functionIndex, string functionName, string packageName = null)
        {
            var fi = functionIndex.GetFunctionInfo(functionName, packageName);

            if (fi != null)
            {
                return(fi);
            }

            packageName = await functionIndex.GetPackageNameAsync(functionName);

            if (!string.IsNullOrEmpty(packageName))
            {
                return(functionIndex.GetFunctionInfo(functionName, packageName));
            }

            return(null);
        }
        private static Task LogHostStartedAsync(IFunctionIndex functionIndex, HostOutputMessage hostOutputMessage,
                                                IHostInstanceLogger logger, CancellationToken cancellationToken)
        {
            IEnumerable <FunctionDescriptor> functions = functionIndex.ReadAllDescriptors();

            HostStartedMessage message = new HostStartedMessage
            {
                HostInstanceId      = hostOutputMessage.HostInstanceId,
                HostDisplayName     = hostOutputMessage.HostDisplayName,
                SharedQueueName     = hostOutputMessage.SharedQueueName,
                InstanceQueueName   = hostOutputMessage.InstanceQueueName,
                Heartbeat           = hostOutputMessage.Heartbeat,
                WebJobRunIdentifier = hostOutputMessage.WebJobRunIdentifier,
                Functions           = functions
            };

            return(logger.LogHostStartedAsync(message, cancellationToken));
        }
Example #26
0
        public PackageIndex(
            IRInteractiveWorkflowProvider interactiveWorkflowProvider, ICoreShell shell, IIntellisenseRSession host, IFunctionIndex functionIndex)
        {
            _shell         = shell;
            _host          = host;
            _functionIndex = functionIndex;

            _workflow = interactiveWorkflowProvider.GetOrCreate();

            _interactiveSession                  = _workflow.RSession;
            _interactiveSession.Connected       += OnSessionConnected;
            _interactiveSession.PackagesRemoved += OnPackagesRemoved;

            _workflow.RSessions.BrokerStateChanged += OnBrokerStateChanged;

            if (_workflow.RSession.IsHostRunning)
            {
                BuildIndexAsync().DoNotWait();
            }
        }
        public async Task <IEditorScript> StartScript(ICoreShell coreShell, string text, string filename, string contentType, IRSessionProvider sessionProvider)
        {
            var coreEditor = await InUI(() => new CoreEditor(coreShell, text, filename, contentType));

            var containerDisposable = await AddToHost(coreEditor.Control);

            if (sessionProvider != null)
            {
                IntelliSenseRSession.HostStartTimeout = 10000;
                HostScript = new RHostScript(sessionProvider);

                PackageIndex = coreShell.GetService <IPackageIndex>();
                await PackageIndex.BuildIndexAsync();

                FunctionIndex = coreShell.GetService <IFunctionIndex>();
                await FunctionIndex.BuildIndexAsync();
            }

            return(new EditorScript(coreShell, coreEditor, containerDisposable));
        }
        private static Task LogHostStartedAsync(IFunctionIndex functionIndex, HostOutputMessage hostOutputMessage,
            IHostInstanceLogger logger, CancellationToken cancellationToken)
        {
            IEnumerable<FunctionDescriptor> functions = functionIndex.ReadAllDescriptors();

            HostStartedMessage message = new HostStartedMessage
            {
                HostInstanceId = hostOutputMessage.HostInstanceId,
                HostDisplayName = hostOutputMessage.HostDisplayName,
                SharedQueueName = hostOutputMessage.SharedQueueName,
                InstanceQueueName = hostOutputMessage.InstanceQueueName,
                Heartbeat = hostOutputMessage.Heartbeat,
                WebJobRunIdentifier = hostOutputMessage.WebJobRunIdentifier,
                Functions = functions
            };

            return logger.LogHostStartedAsync(message, cancellationToken);
        }
 public ParameterNameCompletionProvider(IFunctionIndex functionIndex, IImageService imageService)
 {
     _functionIndex = functionIndex;
     _imageService  = imageService;
 }
 public ParameterNameCompletionProvider(IFunctionIndex functionIndex, IImageService imageService, IREditorSettings settings)
 {
     _functionIndex = functionIndex;
     _imageService  = imageService;
     _settings      = settings;
 }
        public async Task <JobHostContext> Create(CancellationToken shutdownToken, CancellationToken cancellationToken)
        {
            using (CancellationTokenSource combinedCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, shutdownToken))
            {
                CancellationToken combinedCancellationToken = combinedCancellationSource.Token;

                await WriteSiteExtensionManifestAsync(combinedCancellationToken);

                // TODO: FACAVAL: Chat with Brettsam, this should probably be moved out of here.
                _loggerFactory.AddProvider(new FunctionOutputLoggerProvider());

                IFunctionIndex functions = await _functionIndexProvider.GetAsync(combinedCancellationToken);

                IListenerFactory functionsListenerFactory = new HostListenerFactory(functions.ReadAll(), _singletonManager, _activator, _nameResolver, _loggerFactory, _jobHostOptions.Value.AllowPartialHostStartup);

                string hostId = await _hostIdProvider.GetHostIdAsync(cancellationToken);

                bool dashboardLoggingEnabled = _dashboardLoggingSetup.Setup(functions, functionsListenerFactory, out IFunctionExecutor hostCallExecutor,
                                                                            out IListener listener, out HostOutputMessage hostOutputMessage, hostId, shutdownToken);
                if (dashboardLoggingEnabled)
                {
                    // Publish this to Azure logging account so that a web dashboard can see it.
                    await LogHostStartedAsync(functions, hostOutputMessage, _hostInstanceLogger, combinedCancellationToken);
                }

                if (_functionExecutor is FunctionExecutor executor)
                {
                    executor.HostOutputMessage = hostOutputMessage;
                }

                IEnumerable <FunctionDescriptor> descriptors = functions.ReadAllDescriptors();
                int descriptorsCount = descriptors.Count();

                ILogger startupLogger = _loggerFactory?.CreateLogger(LogCategories.Startup);

                if (_jobHostOptions.Value.UsingDevelopmentSettings)
                {
                    startupLogger?.LogDebug("Development settings applied");
                }

                if (descriptorsCount == 0)
                {
                    startupLogger?.LogWarning($"No job functions found. Try making your job classes and methods public. {Resource.ExtensionInitializationMessage}");
                }
                else
                {
                    StringBuilder functionsTrace = new StringBuilder();
                    functionsTrace.AppendLine("Found the following functions:");

                    foreach (FunctionDescriptor descriptor in descriptors)
                    {
                        functionsTrace.AppendLine(descriptor.FullName);
                    }
                    string msg = functionsTrace.ToString();
                    startupLogger?.LogInformation(msg);
                }

                return(new JobHostContext(
                           functions,
                           hostCallExecutor,
                           listener,
                           _eventCollector,
                           _loggerFactory));
            }
        }
 public ParameterNameCompletionProvider(IFunctionIndex functionIndex, IGlyphService glyphService)
 {
     _functionIndex = functionIndex;
     _glyphService  = glyphService;
 }
Example #33
0
 public FunctionInfoTest(RSupportMefCatalogFixture catalog) {
     _exportProvider = catalog.CreateExportProvider();
     _workflow = UIThreadHelper.Instance.Invoke(() => _exportProvider.GetExportedValue<IRInteractiveWorkflowProvider>().GetOrCreate());
     _packageIndex = _exportProvider.GetExportedValue<IPackageIndex>();
     _functionIndex = _exportProvider.GetExportedValue<IFunctionIndex>();
 }
Example #34
0
 public static Task InitializeAsync(this IPackageIndex packageIndex, IFunctionIndex functionIndex)
 {
     RToolsSettings.Current = new TestRToolsSettings();
     return(packageIndex.BuildIndexAsync());
 }
 public ParameterNameCompletionProvider(IFunctionIndex functionIndex, IGlyphService glyphService) {
     _functionIndex = functionIndex;
     _glyphService = glyphService;
 }
        public static async Task <JobHostContext> CreateAndLogHostStartedAsync(
            JobHost host,
            IStorageAccountProvider storageAccountProvider,
            IQueueConfiguration queueConfiguration,
            ITypeLocator typeLocator,
            IJobActivator activator,
            INameResolver nameResolver,
            IConsoleProvider consoleProvider,
            JobHostConfiguration config,
            CancellationToken shutdownToken,
            CancellationToken cancellationToken,
            IHostIdProvider hostIdProvider                                 = null,
            FunctionExecutor functionExecutor                              = null,
            IFunctionIndexProvider functionIndexProvider                   = null,
            IBindingProvider bindingProvider                               = null,
            IHostInstanceLoggerProvider hostInstanceLogerProvider          = null,
            IFunctionInstanceLoggerProvider functionInstanceLoggerProvider = null,
            IFunctionOutputLoggerProvider functionOutputLoggerProvider     = null,
            IBackgroundExceptionDispatcher backgroundExceptionDispatcher   = null,
            SingletonManager singletonManager                              = null)
        {
            if (hostIdProvider == null)
            {
                hostIdProvider = new DynamicHostIdProvider(storageAccountProvider, () => functionIndexProvider);
            }

            IExtensionTypeLocator extensionTypeLocator = new ExtensionTypeLocator(typeLocator);

            if (backgroundExceptionDispatcher == null)
            {
                backgroundExceptionDispatcher = BackgroundExceptionDispatcher.Instance;
            }
            ContextAccessor <IMessageEnqueuedWatcher> messageEnqueuedWatcherAccessor = new ContextAccessor <IMessageEnqueuedWatcher>();
            ContextAccessor <IBlobWrittenWatcher>     blobWrittenWatcherAccessor     = new ContextAccessor <IBlobWrittenWatcher>();
            ISharedContextProvider sharedContextProvider = new SharedContextProvider();

            // Create a wrapper TraceWriter that delegates to both the user
            // TraceWriter specified on Config (if present), as well as to Console
            TraceWriter trace = new ConsoleTraceWriter(config.Tracing, consoleProvider.Out);

            // Register system services with the service container
            config.AddService <INameResolver>(nameResolver);

            ExtensionConfigContext context = new ExtensionConfigContext
            {
                Config = config,
                Trace  = trace,
                Host   = host
            };

            InvokeExtensionConfigProviders(context);

            IExtensionRegistry      extensions             = config.GetExtensions();
            ITriggerBindingProvider triggerBindingProvider = DefaultTriggerBindingProvider.Create(nameResolver,
                                                                                                  storageAccountProvider, extensionTypeLocator, hostIdProvider, queueConfiguration, backgroundExceptionDispatcher,
                                                                                                  messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, sharedContextProvider, extensions, trace);

            if (bindingProvider == null)
            {
                bindingProvider = DefaultBindingProvider.Create(nameResolver, storageAccountProvider, extensionTypeLocator, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, extensions);
            }

            DefaultLoggerProvider loggerProvider = new DefaultLoggerProvider(storageAccountProvider, trace);

            if (singletonManager == null)
            {
                singletonManager = new SingletonManager(storageAccountProvider, backgroundExceptionDispatcher, config.Singleton, trace, config.NameResolver);
            }

            using (CancellationTokenSource combinedCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, shutdownToken))
            {
                CancellationToken combinedCancellationToken = combinedCancellationSource.Token;

                await WriteSiteExtensionManifestAsync(combinedCancellationToken);

                IStorageAccount dashboardAccount = await storageAccountProvider.GetDashboardAccountAsync(combinedCancellationToken);

                IHostInstanceLogger hostInstanceLogger = null;
                if (hostInstanceLogerProvider != null)
                {
                    hostInstanceLogger = await hostInstanceLogerProvider.GetAsync(combinedCancellationToken);
                }
                else
                {
                    hostInstanceLogger = await((IHostInstanceLoggerProvider)loggerProvider).GetAsync(combinedCancellationToken);
                }

                IFunctionInstanceLogger functionInstanceLogger = null;
                if (functionInstanceLoggerProvider != null)
                {
                    functionInstanceLogger = await functionInstanceLoggerProvider.GetAsync(combinedCancellationToken);
                }
                else
                {
                    functionInstanceLogger = (IFunctionInstanceLogger)(await((IFunctionInstanceLoggerProvider)loggerProvider).GetAsync(combinedCancellationToken));
                }

                IFunctionOutputLogger functionOutputLogger = null;
                if (functionOutputLoggerProvider != null)
                {
                    functionOutputLogger = await functionOutputLoggerProvider.GetAsync(combinedCancellationToken);
                }
                else
                {
                    functionOutputLogger = (IFunctionOutputLogger)(await((IFunctionOutputLoggerProvider)loggerProvider).GetAsync(combinedCancellationToken));
                }

                if (functionExecutor == null)
                {
                    functionExecutor = new FunctionExecutor(functionInstanceLogger, functionOutputLogger, backgroundExceptionDispatcher, trace, config.FunctionTimeout);
                }

                if (functionIndexProvider == null)
                {
                    functionIndexProvider = new FunctionIndexProvider(typeLocator, triggerBindingProvider, bindingProvider, activator, functionExecutor, extensions, singletonManager);
                }

                IFunctionIndex functions = await functionIndexProvider.GetAsync(combinedCancellationToken);

                IListenerFactory functionsListenerFactory = new HostListenerFactory(functions.ReadAll(), singletonManager, activator, nameResolver, trace);

                IFunctionExecutor hostCallExecutor;
                IListener         listener;
                HostOutputMessage hostOutputMessage;

                if (dashboardAccount != null)
                {
                    string hostId = await hostIdProvider.GetHostIdAsync(cancellationToken);

                    string sharedQueueName = HostQueueNames.GetHostQueueName(hostId);
                    IStorageQueueClient dashboardQueueClient       = dashboardAccount.CreateQueueClient();
                    IStorageQueue       sharedQueue                = dashboardQueueClient.GetQueueReference(sharedQueueName);
                    IListenerFactory    sharedQueueListenerFactory = new HostMessageListenerFactory(sharedQueue,
                                                                                                    queueConfiguration, backgroundExceptionDispatcher, trace, functions,
                                                                                                    functionInstanceLogger, functionExecutor);

                    Guid             hostInstanceId               = Guid.NewGuid();
                    string           instanceQueueName            = HostQueueNames.GetHostQueueName(hostInstanceId.ToString("N"));
                    IStorageQueue    instanceQueue                = dashboardQueueClient.GetQueueReference(instanceQueueName);
                    IListenerFactory instanceQueueListenerFactory = new HostMessageListenerFactory(instanceQueue,
                                                                                                   queueConfiguration, backgroundExceptionDispatcher, trace, functions,
                                                                                                   functionInstanceLogger, functionExecutor);

                    HeartbeatDescriptor heartbeatDescriptor = new HeartbeatDescriptor
                    {
                        SharedContainerName = HostContainerNames.Hosts,
                        SharedDirectoryName = HostDirectoryNames.Heartbeats + "/" + hostId,
                        InstanceBlobName    = hostInstanceId.ToString("N"),
                        ExpirationInSeconds = (int)HeartbeatIntervals.ExpirationInterval.TotalSeconds
                    };

                    IStorageBlockBlob blob = dashboardAccount.CreateBlobClient()
                                             .GetContainerReference(heartbeatDescriptor.SharedContainerName)
                                             .GetBlockBlobReference(heartbeatDescriptor.SharedDirectoryName + "/" + heartbeatDescriptor.InstanceBlobName);
                    IRecurrentCommand heartbeatCommand = new UpdateHostHeartbeatCommand(new HeartbeatCommand(blob));

                    IEnumerable <MethodInfo> indexedMethods = functions.ReadAllMethods();
                    Assembly hostAssembly = GetHostAssembly(indexedMethods);
                    string   displayName  = hostAssembly != null?hostAssembly.GetName().Name : "Unknown";

                    hostOutputMessage = new DataOnlyHostOutputMessage
                    {
                        HostInstanceId      = hostInstanceId,
                        HostDisplayName     = displayName,
                        SharedQueueName     = sharedQueueName,
                        InstanceQueueName   = instanceQueueName,
                        Heartbeat           = heartbeatDescriptor,
                        WebJobRunIdentifier = WebJobRunIdentifier.Current
                    };

                    hostCallExecutor = CreateHostCallExecutor(instanceQueueListenerFactory, heartbeatCommand,
                                                              backgroundExceptionDispatcher, shutdownToken, functionExecutor);
                    IListenerFactory hostListenerFactory = new CompositeListenerFactory(functionsListenerFactory,
                                                                                        sharedQueueListenerFactory, instanceQueueListenerFactory);
                    listener = CreateHostListener(hostListenerFactory, heartbeatCommand, backgroundExceptionDispatcher, shutdownToken);

                    // Publish this to Azure logging account so that a web dashboard can see it.
                    await LogHostStartedAsync(functions, hostOutputMessage, hostInstanceLogger, combinedCancellationToken);
                }
                else
                {
                    hostCallExecutor = new ShutdownFunctionExecutor(shutdownToken, functionExecutor);

                    IListener factoryListener  = new ListenerFactoryListener(functionsListenerFactory);
                    IListener shutdownListener = new ShutdownListener(shutdownToken, factoryListener);
                    listener = shutdownListener;

                    hostOutputMessage = new DataOnlyHostOutputMessage();
                }

                functionExecutor.HostOutputMessage = hostOutputMessage;

                IEnumerable <FunctionDescriptor> descriptors = functions.ReadAllDescriptors();
                int descriptorsCount = descriptors.Count();

                if (descriptorsCount == 0)
                {
                    trace.Warning(string.Format("No job functions found. Try making your job classes and methods public. {0}",
                                                Constants.ExtensionInitializationMessage), TraceSource.Indexing);
                }
                else
                {
                    StringBuilder functionsTrace = new StringBuilder();
                    functionsTrace.AppendLine("Found the following functions:");

                    foreach (FunctionDescriptor descriptor in descriptors)
                    {
                        functionsTrace.AppendLine(descriptor.FullName);
                    }

                    trace.Info(functionsTrace.ToString(), TraceSource.Indexing);
                }

                return(new JobHostContext(functions, hostCallExecutor, listener, trace));
            }
        }
Example #37
0
 public RFunctionCompletion(string displayText, string insertionText, string description, ImageSource iconSource, IFunctionIndex functionIndex, ICompletionSession session) :
     base(displayText, insertionText, description, iconSource) {
     _functionIndex = functionIndex;
     _session = session;
 }
Example #38
0
 public RFunctionCompletion(string displayText, string insertionText, string description, ImageSource iconSource, IFunctionIndex functionIndex, ICompletionSession session) :
     base(displayText, insertionText, description, iconSource)
 {
     _functionIndex = functionIndex;
     _session       = session;
 }
        // Do the full runtime intitialization. This includes static initialization.
        // This mainly means:
        // - indexing the functions
        // - spinning up the listeners (so connecting to the services)
        public static async Task <JobHostContext> CreateJobHostContextAsync(
            this JobHostConfiguration config,
            ServiceProviderWrapper services, // Results from first phase
            JobHost host,
            CancellationToken shutdownToken,
            CancellationToken cancellationToken)
        {
            FunctionExecutor                           functionExecutor       = services.GetService <FunctionExecutor>();
            IFunctionIndexProvider                     functionIndexProvider  = services.GetService <IFunctionIndexProvider>();
            ITriggerBindingProvider                    triggerBindingProvider = services.GetService <ITriggerBindingProvider>();
            IBindingProvider                           bindingProvider        = services.GetService <IBindingProvider>();
            SingletonManager                           singletonManager       = services.GetService <SingletonManager>();
            IJobActivator                              activator              = services.GetService <IJobActivator>();
            IHostIdProvider                            hostIdProvider         = services.GetService <IHostIdProvider>();
            INameResolver                              nameResolver           = services.GetService <INameResolver>();
            IExtensionRegistry                         extensions             = services.GetExtensions();
            IStorageAccountProvider                    storageAccountProvider = services.GetService <IStorageAccountProvider>();
            ILoggerFactory                             loggerFactory          = services.GetService <ILoggerFactory>();
            IFunctionResultAggregatorFactory           aggregatorFactory      = services.GetService <IFunctionResultAggregatorFactory>();
            IAsyncCollector <FunctionInstanceLogEntry> functionEventCollector = null;

            // Create the aggregator if all the pieces are configured
            IAsyncCollector <FunctionInstanceLogEntry> aggregator = null;

            if (loggerFactory != null && aggregatorFactory != null && config.Aggregator.IsEnabled)
            {
                aggregator = aggregatorFactory.Create(config.Aggregator.BatchSize, config.Aggregator.FlushTimeout, loggerFactory);
            }

            IQueueConfiguration queueConfiguration = services.GetService <IQueueConfiguration>();
            var blobsConfiguration = config.Blobs;

            TraceWriter trace = services.GetService <TraceWriter>();
            IAsyncCollector <FunctionInstanceLogEntry> registeredFunctionEventCollector = services.GetService <IAsyncCollector <FunctionInstanceLogEntry> >();

            if (registeredFunctionEventCollector != null && aggregator != null)
            {
                // If there are both an aggregator and a registered FunctionEventCollector, wrap them in a composite
                functionEventCollector = new CompositeFunctionEventCollector(new[] { registeredFunctionEventCollector, aggregator });
            }
            else
            {
                // Otherwise, take whichever one is null (or use null if both are)
                functionEventCollector = aggregator ?? registeredFunctionEventCollector;
            }

            IWebJobsExceptionHandler exceptionHandler = services.GetService <IWebJobsExceptionHandler>();

            if (exceptionHandler != null)
            {
                exceptionHandler.Initialize(host);
            }

            bool hasFastTableHook   = services.GetService <IAsyncCollector <FunctionInstanceLogEntry> >() != null;
            bool noDashboardStorage = config.DashboardConnectionString == null;

            // Only testing will override these interfaces.
            IHostInstanceLoggerProvider     hostInstanceLoggerProvider     = services.GetService <IHostInstanceLoggerProvider>();
            IFunctionInstanceLoggerProvider functionInstanceLoggerProvider = services.GetService <IFunctionInstanceLoggerProvider>();
            IFunctionOutputLoggerProvider   functionOutputLoggerProvider   = services.GetService <IFunctionOutputLoggerProvider>();

            if (hostInstanceLoggerProvider == null && functionInstanceLoggerProvider == null && functionOutputLoggerProvider == null)
            {
                if (hasFastTableHook && noDashboardStorage)
                {
                    var loggerProvider = new FastTableLoggerProvider(trace);
                    hostInstanceLoggerProvider     = loggerProvider;
                    functionInstanceLoggerProvider = loggerProvider;
                    functionOutputLoggerProvider   = loggerProvider;
                }
                else
                {
                    var loggerProvider = new DefaultLoggerProvider(storageAccountProvider, trace);
                    hostInstanceLoggerProvider     = loggerProvider;
                    functionInstanceLoggerProvider = loggerProvider;
                    functionOutputLoggerProvider   = loggerProvider;
                }
            }

            using (CancellationTokenSource combinedCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, shutdownToken))
            {
                CancellationToken combinedCancellationToken = combinedCancellationSource.Token;

                await WriteSiteExtensionManifestAsync(combinedCancellationToken);

                IStorageAccount dashboardAccount = await storageAccountProvider.GetDashboardAccountAsync(combinedCancellationToken);

                IHostInstanceLogger hostInstanceLogger = await hostInstanceLoggerProvider.GetAsync(combinedCancellationToken);

                IFunctionInstanceLogger functionInstanceLogger = await functionInstanceLoggerProvider.GetAsync(combinedCancellationToken);

                IFunctionOutputLogger functionOutputLogger = await functionOutputLoggerProvider.GetAsync(combinedCancellationToken);

                if (functionExecutor == null)
                {
                    var extensionRegistry     = config.GetService <IExtensionRegistry>();
                    var globalFunctionFilters = extensionRegistry.GetFunctionFilters();

                    functionExecutor = new FunctionExecutor(functionInstanceLogger, functionOutputLogger, exceptionHandler, trace, functionEventCollector, loggerFactory, globalFunctionFilters);
                    services.AddService(functionExecutor);
                }

                if (functionIndexProvider == null)
                {
                    functionIndexProvider = new FunctionIndexProvider(
                        services.GetService <ITypeLocator>(),
                        triggerBindingProvider,
                        bindingProvider,
                        activator,
                        functionExecutor,
                        extensions,
                        singletonManager,
                        trace,
                        loggerFactory);

                    // Important to set this so that the func we passed to DynamicHostIdProvider can pick it up.
                    services.AddService <IFunctionIndexProvider>(functionIndexProvider);
                }

                IFunctionIndex functions = await functionIndexProvider.GetAsync(combinedCancellationToken);

                IListenerFactory functionsListenerFactory = new HostListenerFactory(functions.ReadAll(), singletonManager, activator, nameResolver, trace, loggerFactory);

                IFunctionExecutor hostCallExecutor;
                IListener         listener;
                HostOutputMessage hostOutputMessage;

                string hostId = await hostIdProvider.GetHostIdAsync(cancellationToken);

                if (string.Compare(config.HostId, hostId, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    // if this isn't a static host ID, provide the HostId on the config
                    // so it is accessible
                    config.HostId = hostId;
                }

                if (dashboardAccount == null)
                {
                    hostCallExecutor = new ShutdownFunctionExecutor(shutdownToken, functionExecutor);

                    IListener factoryListener  = new ListenerFactoryListener(functionsListenerFactory);
                    IListener shutdownListener = new ShutdownListener(shutdownToken, factoryListener);
                    listener = shutdownListener;

                    hostOutputMessage = new DataOnlyHostOutputMessage();
                }
                else
                {
                    string sharedQueueName = HostQueueNames.GetHostQueueName(hostId);
                    IStorageQueueClient dashboardQueueClient       = dashboardAccount.CreateQueueClient();
                    IStorageQueue       sharedQueue                = dashboardQueueClient.GetQueueReference(sharedQueueName);
                    IListenerFactory    sharedQueueListenerFactory = new HostMessageListenerFactory(sharedQueue,
                                                                                                    queueConfiguration, exceptionHandler, trace, loggerFactory, functions,
                                                                                                    functionInstanceLogger, functionExecutor);

                    Guid             hostInstanceId               = Guid.NewGuid();
                    string           instanceQueueName            = HostQueueNames.GetHostQueueName(hostInstanceId.ToString("N"));
                    IStorageQueue    instanceQueue                = dashboardQueueClient.GetQueueReference(instanceQueueName);
                    IListenerFactory instanceQueueListenerFactory = new HostMessageListenerFactory(instanceQueue,
                                                                                                   queueConfiguration, exceptionHandler, trace, loggerFactory, functions,
                                                                                                   functionInstanceLogger, functionExecutor);

                    HeartbeatDescriptor heartbeatDescriptor = new HeartbeatDescriptor
                    {
                        SharedContainerName = HostContainerNames.Hosts,
                        SharedDirectoryName = HostDirectoryNames.Heartbeats + "/" + hostId,
                        InstanceBlobName    = hostInstanceId.ToString("N"),
                        ExpirationInSeconds = (int)HeartbeatIntervals.ExpirationInterval.TotalSeconds
                    };

                    IStorageBlockBlob blob = dashboardAccount.CreateBlobClient()
                                             .GetContainerReference(heartbeatDescriptor.SharedContainerName)
                                             .GetBlockBlobReference(heartbeatDescriptor.SharedDirectoryName + "/" + heartbeatDescriptor.InstanceBlobName);
                    IRecurrentCommand heartbeatCommand = new UpdateHostHeartbeatCommand(new HeartbeatCommand(blob));

                    IEnumerable <MethodInfo> indexedMethods = functions.ReadAllMethods();
                    Assembly hostAssembly = GetHostAssembly(indexedMethods);
                    string   displayName  = hostAssembly != null?AssemblyNameCache.GetName(hostAssembly).Name : "Unknown";

                    hostOutputMessage = new DataOnlyHostOutputMessage
                    {
                        HostInstanceId      = hostInstanceId,
                        HostDisplayName     = displayName,
                        SharedQueueName     = sharedQueueName,
                        InstanceQueueName   = instanceQueueName,
                        Heartbeat           = heartbeatDescriptor,
                        WebJobRunIdentifier = WebJobRunIdentifier.Current
                    };

                    hostCallExecutor = CreateHostCallExecutor(instanceQueueListenerFactory, heartbeatCommand,
                                                              exceptionHandler, shutdownToken, functionExecutor);
                    IListenerFactory hostListenerFactory = new CompositeListenerFactory(functionsListenerFactory,
                                                                                        sharedQueueListenerFactory, instanceQueueListenerFactory);
                    listener = CreateHostListener(hostListenerFactory, heartbeatCommand, exceptionHandler, shutdownToken);

                    // Publish this to Azure logging account so that a web dashboard can see it.
                    await LogHostStartedAsync(functions, hostOutputMessage, hostInstanceLogger, combinedCancellationToken);
                }

                functionExecutor.HostOutputMessage = hostOutputMessage;

                IEnumerable <FunctionDescriptor> descriptors = functions.ReadAllDescriptors();
                int descriptorsCount = descriptors.Count();

                ILogger startupLogger = loggerFactory?.CreateLogger(LogCategories.Startup);

                if (config.UsingDevelopmentSettings)
                {
                    string msg = "Development settings applied";
                    trace.Verbose(msg);
                    startupLogger?.LogDebug(msg);
                }

                if (descriptorsCount == 0)
                {
                    string msg = string.Format("No job functions found. Try making your job classes and methods public. {0}",
                                               Constants.ExtensionInitializationMessage);

                    trace.Warning(msg, Host.TraceSource.Indexing);
                    startupLogger?.LogWarning(msg);
                }
                else
                {
                    StringBuilder functionsTrace = new StringBuilder();
                    functionsTrace.AppendLine("Found the following functions:");

                    foreach (FunctionDescriptor descriptor in descriptors)
                    {
                        functionsTrace.AppendLine(descriptor.FullName);
                    }
                    string msg = functionsTrace.ToString();
                    trace.Info(msg, Host.TraceSource.Indexing);
                    startupLogger?.LogInformation(msg);
                }

                return(new JobHostContext(
                           functions,
                           hostCallExecutor,
                           listener,
                           trace,
                           functionEventCollector,
                           loggerFactory));
            }
        }