Exemplo n.º 1
0
        public Task Publish(PublishMessageCommand command)
        {
            var board = Context.User.FindFirst(ClaimTypes.GroupSid)?.Value;

            if (command.Route.StartsWith(board))
            {
                rabbitManager.Publish(command.Payload, board, ExchangeType.Topic, command.Route);
            }
            else
            {
                this.Context.Abort();
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            List <string> scheduledToDelete = new List <string>();

            while (!stoppingToken.IsCancellationRequested)
            {
                if (map.Count == 0)
                {
                    await Task.Delay(20);
                }
                // workToDo.WaitOne();

                foreach (var pair in map)
                {
                    while (pair.Value.Remove(out string symbol, out string type))
                    {
                        Task.Run(async() =>
                        {
                            var attempts = 0;
                            var delay    = 10;
                            while (true)
                            {
                                if (attempts > 6)
                                {
                                    logger.LogError("Fire and Forget API price Failed");
                                    return;
                                }

                                try
                                {
                                    var result = await pair.Value.Client.Stocks.IntraDay(symbol).SetInterval(Interval.OneMinute).GetAsync();
                                    var data   = result.Data[0].Close;
                                    var time   = result.Data[0].Timestamp;

                                    var message = new Price
                                    {
                                        Symbol    = symbol,
                                        Value     = data,
                                        Timestamp = time
                                    };

                                    // send message
                                    var lastpart = pair.Value.ApiKey == "Demo" ? "Demo" : pair.Value.ApiKey.Sha256Hash();

                                    var routeKey = $"price.{type}.{lastpart}";

                                    _rabbitMQ.Publish(message, "exchange.price", "topic", routeKey);

                                    return;
                                }
                                catch (AlphaVantageApiLimitException e)
                                {
                                    logger.LogWarning(e, $"Attempt #{attempts} to access AV api.");
                                }

                                await Task.Delay(delay);
                                attempts++;
                                delay *= 10;
                            }
                        }).Forget();
                    }
                    scheduledToDelete.Add(pair.Key);
                }

                foreach (var apikey in scheduledToDelete)
                {
                    map.TryRemove(apikey, out _);
                }
                scheduledToDelete.Clear();
            }
        }