Example #1
0
        public static void TimerTick(object sender, ElapsedEventArgs e)
        {
            Logger.Write("Entering TimerTick.", Logger.LogType.ServiceLog);

            System.Threading.Tasks.Task.Run(() =>
            {
                try
                {
                    SchedulerId = (SchedulerId + 1) % 4;
                    Logger.Write($"SchedulerId: {SchedulerId}", Logger.LogType.ServiceLog);

                    var tls = new TaskLoopScheduler();
                    var eas = new EventActionScheduler();
                    switch (SchedulerId)
                    {
                    case 0:
                        tls.PollForTasksToStart();
                        tls.StartTasks();
                        break;

                    case 1:
                        eas.PollForEventActionsToStop();
                        eas.StopEventActions();
                        break;

                    case 2:
                        eas.PollForEventActionsToStart();
                        eas.StartEventActions();
                        break;

                    case 3:
                        tls.PollForTasksToStop();
                        tls.StopTasks();
                        break;
                    }

                    Logger.Write($"{new GlobalStateManager().GetCount()} TaskLoop(s) running: {new GlobalStateManager().ToString()}", Logger.LogType.ServiceLog);
                }
                catch (Exception exception)
                {
                    Logger.WriteException(exception);
                }
            });

            Logger.Write("Exiting TimerTick.", Logger.LogType.ServiceLog);

            TimerHelper.Timer.Interval = TimerUtility.GetAdjustedInterval(TimerHelper.TimerTickInterval);
            TimerHelper.Timer.Start();
        }
Example #2
0
        public static void GatherTaskElapsed(object sender, string currencyPair, int interval, Timer t, List <EventAction> eventActions)
        {
            t.Interval = TimerUtility.GetAdjustedInterval(interval);
            t.Start();

            System.Threading.Tasks.Task.Run(() =>
            {
                try
                {
                    DateTime dateTimeNow  = DateTime.UtcNow;
                    DateTime dateTimePast = dateTimeNow.AddSeconds(-(60 * 4));

                    var result = PoloniexExchangeService.Instance.ReturnTradeHistory(currencyPair, dateTimePast, dateTimeNow);
                    result     = result.OrderBy(x => x.date).ToList();

                    var dataPoint = new CurrencyDataPoint
                    {
                        CurrencyPair    = currencyPair,
                        ClosingDateTime = dateTimeNow,
                    };

                    if (result.Any())
                    {
                        var curRate            = result.Last().rate;
                        dataPoint.ClosingValue = curRate;
                    }
                    else
                    {
                        using (var db = new PoloniexContext())
                        {
                            dataPoint.ClosingValue = db.CurrencyDataPoints
                                                     .Where(x => x.CurrencyPair == dataPoint.CurrencyPair)
                                                     .OrderByDescending(x => x.ClosingDateTime)
                                                     .First().ClosingValue;
                        }
                    }

                    using (var db = new PoloniexContext())
                    {
                        dataPoint.CreatedDateTime = DateTime.UtcNow;
                        db.CurrencyDataPoints.Add(dataPoint);
                        db.SaveChanges();
                    }

                    if (eventActions != null)
                    {
                        var sortedActions = eventActions.OrderBy(x => x.Priority).ToList();
                        for (int i = 0; i < sortedActions.Count(); i++)
                        {
                            Logger.Write($"{sortedActions[i].EventActionId}: Executing action {i + 1} of {sortedActions.Count()} - {sortedActions[i].EventActionType}", Logger.LogType.ServiceLog);
                            var threadEventAction = sortedActions[i];
                            threadEventAction.Action(threadEventAction.EventActionId);
                        }
                    }
                }
                catch (Exception exception)
                {
                    Logger.WriteException(exception);
                }
            });
        }