예제 #1
0
        public static void Main(string[] args)
        {
            AlphaVantageAgent agent = new AlphaVantageAgent();

            agent.Start();

            Console.WriteLine("bye!");
        }
예제 #2
0
        internal static List <AlphaVantageAgent.KeyAgent> LoadKeys(AlphaVantageAgent parent)
        {
            List <AlphaVantageAgent.KeyAgent> keyAgents;

            string keyQuery = @"select ApiKey,
       coalesce(DateLastUsed, '1900-01-01') as DateLastUsed,
       coalesce(DailyCallCount, 0) as DailyCallCount,
	   DailyCallMax as DailyCallMax,
	   coalesce(CallsPerMinute, 0) as CallsPerMinute
from AlphaVantageKeys;";

            List <KeyDto> dtos = SQLUtilities.FromQuery <KeyDto>(keyQuery);

            keyAgents = dtos.Select(d => new AlphaVantageAgent.KeyAgent(parent, d.DailyCallMax, d.CallsPerMinute, d.ApiKey, d.DateLastUsed, d.DailyCallCount))
                        .ToList();

            return(keyAgents);
        }
            public KeyAgent(AlphaVantageAgent parent, int?callsPerDay, int callsPerMinute, string key, DateTime dateLastUsed, int callCount)
            {
                this.CallsPerDay    = callsPerDay;
                this.CallsPerMinute = callsPerMinute;
                this.Key            = key;
                this.dailyCallsLeft = this.CallsPerDay ?? int.MaxValue;
                this.maxDailyCalls  = callsPerDay;

                if (this.maxDailyCalls != null)
                {
                    if (dateLastUsed.Date == DateTime.Today)
                    {
                        dailyCallsLeft   = (callsPerDay ?? int.MaxValue) - callCount;
                        this.currentDate = dateLastUsed;
                    }
                    else
                    {
                        dailyCallsLeft   = callsPerDay ?? int.MaxValue;
                        this.currentDate = DateTime.Today;
                    }
                }
            }
            public void Start(AlphaVantageAgent parent)
            {
                this.parent = parent;
                int       sleepTime = 1;
                Stopwatch timer     = Stopwatch.StartNew();

                this.shutdown = false;

                if (this.maxDailyCalls != null && this.maxDailyCalls != 0 && this.dailyCallsLeft == 0)
                {
                    this.shutdown = true;
                }

                while (!shutdown)
                {
                    Console.WriteLine("Key Agent {0} getting next task", this.Key);
                    ILoadTask task = parent.GetNextTask();

                    if (task != null)
                    {
                        Console.WriteLine("Key Agent {0} processing {1}", this.Key, task.Descriptor);
                        this.dailyCallsLeft -= 1;
                        timer.Reset();
                        timer.Start();

                        try
                        {
                            task.Run(this.Key);
                            parent.CompleteTask(task);
                            this.SaveUsage();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            Console.WriteLine(ex.InnerException.Message);
                            parent.FailTask(task);
                        }

                        timer.Stop();
                        double maxSleepTime = 60.0 * 1000.0 / this.CallsPerMinute;

                        sleepTime = (int)Math.Ceiling(maxSleepTime - timer.ElapsedMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("Key Agent {0} no task found, sleeping", this.Key);
                        sleepTime = 1000;
                    }
                    NLog.LogManager.GetCurrentClassLogger().Debug("sleepTime = {0}", sleepTime);
                    Console.WriteLine("Key Agent {0} sleeping for {1} seconds", this.Key, sleepTime / 1000.0);

                    if (sleepTime < 0)
                    {
                        sleepTime = (int)Math.Ceiling(60.0 * 1000.0 / this.CallsPerMinute);
                    }
                    Thread.Sleep(sleepTime);
                }

                this.shutdown = true;

                Console.WriteLine("No daily calls left, terminating.");
            }