private void Handler(DeliveryReport <Null, string> result) { if (result?.Error?.IsError == false) { return; } StaticLog.Error($"Delivery Error: {result.Error.Code} - {result.Error.Reason}"); }
public KafkaQueueService( IConfiguration configuration, ISerializer serializer, IOptions <QueueOptions> options ) : base(serializer, options) { _producer = new Lazy <IProducer <Null, string> >(() => { var config = new ProducerConfig { BootstrapServers = configuration.GetSection(Constants.OptionNameBootstrapServer).Value, BatchNumMessages = 1, MessageTimeoutMs = 1, SocketNagleDisable = true, Acks = Acks.Leader }; return(new ProducerBuilder <Null, string>(config).Build()); }); _consumer = new Lazy <IConsumer <Ignore, string> >(() => { var groupIdSection = configuration.GetSection(Constants.OptionNameConsumerGroupId); var groupId = groupIdSection.Exists() && !string.IsNullOrEmpty(groupIdSection.Value) ? groupIdSection.Value : Constants.DefaultGroupId; var config = new ConsumerConfig { BootstrapServers = configuration.GetSection(Constants.OptionNameBootstrapServer).Value, GroupId = groupId, AutoOffsetReset = AutoOffsetReset.Earliest, StatisticsIntervalMs = 5000, SessionTimeoutMs = 6000 }; var consumer = new ConsumerBuilder <Ignore, string>(config) .SetErrorHandler((_, e) => StaticLog.Error($"Receiving Error: {e.Code} - {e.Reason}")) .Build(); return(consumer); }); }
public static void ScanStocks() { //Log.Error(typeof(CollectDataManager), "Test 1"); //Log.Error(typeof(CollectDataManager),"Test tets"); //Log.Error(typeof(CollectDataManager), "Test tets 123", new Exception("Failed to test")); var engine = new YahooStockEngine(); var quoteList = new ObservableCollection <Quote>(); var boList = new List <CompanyList>(); using (var db = new TheFishEntities()) { boList = db.CompanyLists.Where(x => x.Sector.Equals("Health care", StringComparison.OrdinalIgnoreCase) && x.Symbol.Length < 5).ToList(); } int i = 1; var quoteSingleCollectionChunk = new List <Quote>(); foreach (var item in boList) { //if (i > 2) // break; try { var quote = new Quote(item.Symbol.Trim()); quoteSingleCollectionChunk.Add(quote); if (i == StockFetchTrunk) { YahooStockEngine.Fetch(quoteSingleCollectionChunk); //YahooStockDownoader.GetQuote(quoteSingleCollectionChunk); foreach (var stockInfo in quoteSingleCollectionChunk) { quoteList.Add(stockInfo); } quoteSingleCollectionChunk = new List <Quote>(); i = 1; } i++; }catch (Exception ex) { var message = ex.Message; } //i++; } try { if (quoteSingleCollectionChunk.Count > 0) { YahooStockEngine.Fetch(quoteSingleCollectionChunk); //YahooStockDownoader.GetQuote(quoteSingleCollectionChunk); foreach (var stockInfo in quoteSingleCollectionChunk) { quoteList.Add(stockInfo); } } }catch (Exception ex) { var message = ex.Message; } DateTime today = DateTime.Today; // earliest time today DateTime tomorrow = DateTime.Today.AddDays(1); // earliest time tomorrow using (var db = new TheFishEntities()) { foreach (var item in quoteList.ToList()) { try { var result = StockAnalyzer.AnalyzeStock(item); var isPriceChangeFish = result.IsPriceChangedDramatically; var isVolumeChangeFish = result.IsVolumeAbnormal; var isPrice52WeeksLow = result.IsPrice52WeeksLow; if (!(isPriceChangeFish || isVolumeChangeFish || isPrice52WeeksLow)) { continue; } if ( db.CaughtFish.Where( x => x.Symbol.Equals(item.Symbol) && x.WhenCreated > today && x.WhenCreated < tomorrow) .Any()) { continue; } var caughtFish = new CaughtFish(); caughtFish.Symbol = item.Symbol; caughtFish.WhenCreated = DateTime.Now; caughtFish.Price = item.LastTradePrice; caughtFish.PriceChangePercentage = item.ChangeInPercent; caughtFish.Volume = item.Volume; if (item.AverageDailyVolume > 0 && item.Volume > 0) { caughtFish.VolumeChangePercentage = (int)(0.5M + 100M * (item.Volume - item.AverageDailyVolume) / item.AverageDailyVolume); } var message = ""; var subject = ""; if (isPriceChangeFish) { caughtFish.FishType = 0; message = string.Format(MessageText, "Price Change Alert -- ", caughtFish.Symbol, caughtFish.Price.ToString(), caughtFish.PriceChangePercentage.ToString(), caughtFish.Volume.ToString(), caughtFish.VolumeChangePercentage); subject = " Price Drop Alert -- " + caughtFish.Symbol; } else if (isVolumeChangeFish) { caughtFish.FishType = 1; message = string.Format(MessageText, "Volume Change Alert -- ", caughtFish.Symbol, caughtFish.Price.ToString(), caughtFish.PriceChangePercentage.ToString(), caughtFish.Volume.ToString(), caughtFish.VolumeChangePercentage.ToString()); subject = " Volumne Change Alert -- " + caughtFish.Symbol; } else if (isPrice52WeeksLow) { caughtFish.FishType = 1; message = string.Format(MessageText, "52 Weeks low price Alert -- ", caughtFish.Symbol, caughtFish.Price.ToString(), caughtFish.PriceChangePercentage.ToString(), caughtFish.Volume.ToString(), caughtFish.VolumeChangePercentage.ToString()); subject = " 52 Weeks Low Alert -- " + caughtFish.Symbol; } db.CaughtFish.Add(caughtFish); db.SaveChanges(); Messaging.SendEmailGmail(subject, MessageDetail.GetMessageDetail(item)); } catch (Exception ex) { StaticLog.Error(ex); } } } }
public IObservable <T> GetObservableTopic <T>(string topic, QueueHandler handler = null) where T : class { return(Observable.Create <T>(observer => { var tokenSource = handler != null ? handler.TokenSource : new CancellationTokenSource(); var stoppingToken = tokenSource.Token; ThreadPool.QueueUserWorkItem(s => { var token = (CancellationToken)s; var consumer = CreateConsumer(topic); while (!token.IsCancellationRequested) { while (handler != null && handler.Wait) { handler.LastActivityTime = DateTimeOffset.Now; Wait(); } var pollingInterval = 500; SafeExecute.Sync(() => pollingInterval = _options.Value.QueuePollingInterval); if (pollingInterval < 500) { pollingInterval = 500; } var messageText = string.Empty; try { messageText = PollMessage(consumer, pollingInterval).RunSync(); if (handler != null) { handler.LastActivityTime = DateTimeOffset.Now; } if (string.IsNullOrEmpty(messageText)) { Wait(); continue; } } catch (OperationCanceledException) { // do nothing } catch (Exception e) { StaticLog.Error(e, "Queue consume error: {0}"); } try { var messageWrapper = _serializer.DeserializeObject <QueueMessage>(messageText); var item = _serializer.DeserializeObject(messageWrapper.Data, messageWrapper.Type) as T; observer.OnNext(item); } catch (Exception e) { StaticLog.Error($"Deserialise message error: {messageText} - {e.Message}"); } } }, stoppingToken); return () => { tokenSource.Cancel(); }; })); }
public static void GetQuote(List <Quote> quotes) { // Set the return string to null. var quoteList = new List <Quote>(); try { // Use Yahoo finance service to download stock data from Yahoo var symbols = ""; foreach (var symbol in quotes) { symbols += symbol.Symbol + ","; } string yahooURL = @"http://download.finance.yahoo.com/d/quotes.csv?s=" + symbols + "&f=,l1c6k2oghjkva2j1"; // Initialize a new WebRequest. HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(yahooURL); // Get the response from the Internet resource. HttpWebResponse webresp = (HttpWebResponse)webreq.GetResponse(); // Read the body of the response from the server. var quoteDict = new Dictionary <string, Quote>(); using (var strm = new StreamReader(webresp.GetResponseStream(), Encoding.ASCII)) { // Construct a XML in string format. foreach (var item in quotes) { try { if (item.Symbol.Trim() == "") { continue; } var content = strm.ReadLine().Replace("\"", ""); string[] contents = content.ToString().Split(','); // If contents[2] = "N/A". the stock symbol is invalid. if (contents[2] == "N/A") { continue; } else { var quoteFromDownloader = new Quote(contents[0]); quoteFromDownloader.Symbol = contents[0]; quoteFromDownloader.YearlyLow = Convert.ToDecimal(contents[6]); quoteFromDownloader.YearlyHigh = Convert.ToDecimal(contents[7]); quoteDict.Add(quoteFromDownloader.Symbol, quoteFromDownloader); } } catch (Exception ex) { StaticLog.Error(ex); } } } foreach (var item in quotes) { if (quoteDict.ContainsKey(item.Symbol)) { item.YearlyLow = quoteDict[item.Symbol].YearlyLow; item.YearlyHigh = quoteDict[item.Symbol].YearlyHigh; } } } catch (Exception ex) { StaticLog.Error(ex); } // Return the stock quote data in XML format. }