// On Timer, grab items from RSS Feed
        public void RSSFeedProcessor(
            [TimerTrigger("0 */5 * * * *", RunOnStartup=true)] TimerInfo timer, 
            [Queue("SaveQuestions")] ICollector<Question> outQ, 
            TraceWriter log)
        {
            log.Verbose("RSS Feed Processor started");
            Console.WriteLine("RSS Feed Processor started");

            foreach (var feeds in mFeedSources)
            {
                switch(feeds.Key){
                    case "StackOverflow":
                        foreach (var feed in feeds.Value) {
                            try {
                                using (XmlReader reader = XmlReader.Create(feed.Value)) {
                                    SyndicationFeed rss = SyndicationFeed.Load(reader);

                                    foreach (SyndicationItem item in rss.Items) {
                                        outQ.Add(ProcessStackOverflowEntry(item, feed.Key));
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                log.Error(String.Format("Couldn't read from feed - {0}", feeds.Value), e);
                            }
                        }
                        break;
                    default:
                        log.Warning(String.Format("Couldn't recognize feed format: {0}", feeds.Key),"RSSFeedProcessor");
                        break;
                }
            }
        }
Ejemplo n.º 2
0
        private async Task<bool> SendJSONtoURL(JObject json, string url, TraceWriter trace)
        {
            var client = new HttpClient();
            var response = await client.PostAsJsonAsync(url, json);

            if (response.StatusCode != HttpStatusCode.OK) {
                trace.Error(String.Format("Status Code: {0} - Content: {1}", response.StatusCode, response.Content));
                return false;
            }

            trace.Info("Finished sending message to Slack");
            return true;
        }
 /// <summary>
 /// Event handler for the error stream.
 /// </summary>
 private void ErrorDataAdded(object sender, DataAddedEventArgs e, TraceWriter traceWriter)
 {
     var source = (PSDataCollection<ErrorRecord>)sender;
     var msg = GetErrorMessage(_functionName, _scriptFilePath, source[e.Index]);
     traceWriter.Error(msg);
 }
 public void Error(string message)
 {
     _logger.Error(message);
 }
        public void RunAndBlock(CancellationToken cancellationToken = default(CancellationToken))
        {
            // Start the host and restart it if requested. Host Restarts will happen when
            // host level configuration files change
            do
            {
                ScriptHost newInstance = null;
                try
                {
                    // if we were in an error state retain that,
                    // otherwise move to default
                    if (State != ScriptHostState.Error)
                    {
                        State = ScriptHostState.Default;
                    }

                    // Create a new host config, but keep the host id from existing one
                    _config.HostConfig = new JobHostConfiguration
                    {
                        HostId = _config.HostConfig.HostId
                    };
                    OnInitializeConfig(_config);
                    newInstance = _scriptHostFactory.Create(_settingsManager, _config);
                    _traceWriter = newInstance.TraceWriter;

                    _currentInstance = newInstance;
                    lock (_liveInstances)
                    {
                        _liveInstances.Add(newInstance);
                    }

                    OnHostCreated();

                    if (_traceWriter != null)
                    {
                        string message = string.Format("Starting Host (HostId={0}, Version={1}, ProcessId={2}, Debug={3})",
                            newInstance.ScriptConfig.HostConfig.HostId, ScriptHost.Version, Process.GetCurrentProcess().Id, newInstance.InDebugMode.ToString());
                        _traceWriter.Info(message);
                    }
                    newInstance.StartAsync(cancellationToken).GetAwaiter().GetResult();

                    // log any function initialization errors
                    LogErrors(newInstance);

                    OnHostStarted();

                    // only after ALL initialization is complete do we set the
                    // state to Running
                    State = ScriptHostState.Running;
                    LastError = null;

                    // Wait for a restart signal. This event will automatically reset.
                    // While we're restarting, it is possible for another restart to be
                    // signaled. That is fine - the restart will be processed immediately
                    // once we get to this line again. The important thing is that these
                    // restarts are only happening on a single thread.
                    WaitHandle.WaitAny(new WaitHandle[]
                    {
                        cancellationToken.WaitHandle,
                        newInstance.RestartEvent,
                        _stopEvent
                    });

                    // Orphan the current host instance. We're stopping it, so it won't listen for any new functions
                    // it will finish any currently executing functions and then clean itself up.
                    // Spin around and create a new host instance.
                    Task taskIgnore = Orphan(newInstance);
                }
                catch (Exception ex)
                {
                    State = ScriptHostState.Error;
                    LastError = ex;

                    // We need to keep the host running, so we catch and log any errors
                    // then restart the host
                    if (_traceWriter != null)
                    {
                        _traceWriter.Error("A ScriptHost error has occurred", ex);
                    }

                    // If a ScriptHost instance was created before the exception was thrown
                    // Orphan and cleanup that instance.
                    if (newInstance != null)
                    {
                        Orphan(newInstance, forceStop: true)
                            .ContinueWith(t =>
                            {
                                if (t.IsFaulted)
                                {
                                    t.Exception.Handle(e => true);
                                }
                            }, TaskContinuationOptions.ExecuteSynchronously);
                    }

                    // Wait for a short period of time before restarting to
                    // avoid cases where a host level config error might cause
                    // a rapid restart cycle
                    Task.Delay(_config.RestartInterval).GetAwaiter().GetResult();
                }
            }
            while (!_stopped && !cancellationToken.IsCancellationRequested);
        }
        public void RunAndBlock(CancellationToken cancellationToken = default(CancellationToken))
        {
            // Start the host and restart it if requested. Host Restarts will happen when
            // host level configuration files change
            do
            {
                ScriptHost newInstance = null;
                try
                {
                    IsRunning = false;

                    // Create a new host config, but keep the host id from existing one
                    _config.HostConfig = new JobHostConfiguration
                    {
                        HostId = _config.HostConfig.HostId
                    };
                    OnInitializeConfig(_config.HostConfig);
                    newInstance = _scriptHostFactory.Create(_config);
                    _traceWriter = newInstance.TraceWriter;

                    newInstance.StartAsync(cancellationToken).Wait();

                    // write any function initialization errors to the log file
                    LogErrors(newInstance);

                    lock (_liveInstances)
                    {
                        _liveInstances.Add(newInstance);
                    }
                    _currentInstance = newInstance;
                    OnHostStarted();

                    // only after ALL initialization is complete do we set this flag
                    IsRunning = true;
                    LastError = null;

                    // Wait for a restart signal. This event will automatically reset.
                    // While we're restarting, it is possible for another restart to be
                    // signaled. That is fine - the restart will be processed immediately
                    // once we get to this line again. The important thing is that these
                    // restarts are only happening on a single thread.
                    WaitHandle.WaitAny(new WaitHandle[] 
                    {
                        cancellationToken.WaitHandle,
                        newInstance.RestartEvent,
                        _stopEvent
                    });

                    // Orphan the current host instance. We're stopping it, so it won't listen for any new functions
                    // it will finish any currently executing functions and then clean itself up.
                    // Spin around and create a new host instance.
                    Task taskIgnore = Orphan(newInstance);
                }
                catch (Exception ex)
                {
                    IsRunning = false;
                    LastError = ex;

                    // We need to keep the host running, so we catch and log any errors
                    // then restart the host
                    if (_traceWriter != null)
                    {
                        _traceWriter.Error("A ScriptHost error occurred", ex);
                    }

                    // If a ScriptHost instance was created before the exception was thrown
                    // Orphan and cleanup that instance.
                    if (newInstance != null)
                    {
                        Orphan(newInstance, forceStop: true)
                            .ContinueWith(t =>
                            {
                                if (t.IsFaulted)
                                {
                                    t.Exception.Handle(e => true);
                                }
                            });
                    }

                    // Wait for a short period of time before restarting to
                    // avoid cases where a host level config error might cause
                    // a rapid restart cycle
                    Task.Delay(5000).Wait();
                }
            }
            while (!_stopped && !cancellationToken.IsCancellationRequested);
        }
Ejemplo n.º 7
0
        private static Collection<ScriptBindingProvider> LoadBindingProviders(ScriptHostConfiguration config, JObject hostMetadata, TraceWriter traceWriter)
        {
            JobHostConfiguration hostConfig = config.HostConfig;

            // Register our built in extensions
            var bindingProviderTypes = new Collection<Type>()
            {
                // binding providers defined in this assembly
                typeof(WebJobsCoreScriptBindingProvider),
                typeof(ServiceBusScriptBindingProvider),

                // binding providers defined in known extension assemblies
                typeof(CoreExtensionsScriptBindingProvider),
                typeof(ApiHubScriptBindingProvider),
                typeof(DocumentDBScriptBindingProvider),
                typeof(MobileAppsScriptBindingProvider),
                typeof(NotificationHubScriptBindingProvider),
                typeof(SendGridScriptBindingProvider),
                typeof(TwilioScriptBindingProvider),
                typeof(BotFrameworkScriptBindingProvider)
            };

            // Create the binding providers
            var bindingProviders = new Collection<ScriptBindingProvider>();
            foreach (var bindingProviderType in bindingProviderTypes)
            {
                try
                {
                    var provider = (ScriptBindingProvider)Activator.CreateInstance(bindingProviderType, new object[] { hostConfig, hostMetadata, traceWriter });
                    bindingProviders.Add(provider);
                }
                catch (Exception ex)
                {
                    // If we're unable to load create a binding provider for any reason, log
                    // the error and continue
                    traceWriter.Error(string.Format("Unable to create binding provider '{0}'", bindingProviderType.FullName), ex);
                }
            }

            return bindingProviders;
        }