private object LaunchScheduled() { var schedule = _schedule; var utcNow = DateTime.UtcNow; foreach (var brokerSchedule in schedule) { foreach (var brokerTask in brokerSchedule.RepeatSchedule) { if ((int) brokerTask.Value.TotalSeconds <= 9) { continue; //idiot-safety mode } DateTime lastRunDateTime; if (!brokerSchedule.LastTaskRun.TryGetValue(brokerTask.Key, out lastRunDateTime)) { var brokerLastRequest = SystemBrokerRequest.DataSource .WhereEquals(SystemBrokerRequest.Fields.BrokerID, (short)brokerSchedule.BrokerID) .WhereEquals(SystemBrokerRequest.Fields.Languagetype, (short)brokerSchedule.Languagetype) .WhereEquals(SystemBrokerRequest.Fields.Taskmodetype, (short)brokerTask.Key) .Sort(SystemBrokerRequest.Fields.Datelaunchedutc, SortDirection.Desc) .First(SystemBrokerRequest.Fields.Datelaunchedutc); lastRunDateTime = brokerLastRequest?.Datelaunchedutc ?? DateTime.MinValue; brokerSchedule.LastTaskRun[brokerTask.Key] = lastRunDateTime; } if (utcNow - lastRunDateTime > brokerTask.Value) { SystemBrokerRequest systemBrokerRequestToLaunch; switch (brokerTask.Key) { case RunTaskMode.RunLiveOddsTask: case RunTaskMode.RunRegularOddsTask: case RunTaskMode.RunTodayHistoryTask: systemBrokerRequestToLaunch = new SystemBrokerRequest { BrokerID = brokerSchedule.BrokerID, Languagetype = brokerSchedule.Languagetype, AlgoDateutc = utcNow, Datecreatedutc = utcNow, Taskmodetype = brokerTask.Key }; RunTask(brokerSchedule, systemBrokerRequestToLaunch, utcNow); break; case RunTaskMode.RunPastDateHistoryTask: for (var i = brokerSchedule.Pasthistorydays; i > 0; i--) { var algoDate = utcNow.Date.AddDays(-i); systemBrokerRequestToLaunch = new SystemBrokerRequest { BrokerID = brokerSchedule.BrokerID, Languagetype = brokerSchedule.Languagetype, AlgoDateutc = algoDate, Datecreatedutc = utcNow, Taskmodetype = brokerTask.Key }; RunTask(brokerSchedule, systemBrokerRequestToLaunch, utcNow); } break; } } } } return null; }
private void RunTask(BrokerRequestScheduleTransport brokerSchedule, SystemBrokerRequest launchedRequest, DateTime utcNow) { launchedRequest.Datelaunchedutc = utcNow; launchedRequest.Save(); brokerSchedule.LastTaskRun[launchedRequest.Taskmodetype] = utcNow; TaskRunner.Instance.AddAction(() => { Func<BrokerData> dataGetter = null; var additionalStat = ProcessStatType.Default; switch (launchedRequest.Taskmodetype) { case RunTaskMode.RunTodayHistoryTask: case RunTaskMode.RunPastDateHistoryTask: dataGetter = () => GetBroker(brokerSchedule.BrokerID).LoadResult(launchedRequest.AlgoDateutc.Date, brokerSchedule.SportType, brokerSchedule.Languagetype); additionalStat = ProcessStatType.Result; break; case RunTaskMode.RunLiveOddsTask: dataGetter = () => GetBroker(brokerSchedule.BrokerID).LoadLive(brokerSchedule.SportType, brokerSchedule.Languagetype); additionalStat = ProcessStatType.Bet; break; case RunTaskMode.RunRegularOddsTask: dataGetter = () => GetBroker(brokerSchedule.BrokerID).LoadRegular(brokerSchedule.SportType, brokerSchedule.Languagetype); additionalStat = ProcessStatType.Bet; break; } if (dataGetter != null) { var sw = Stopwatch.StartNew(); var data = dataGetter(); var stat = ProjectProvider.Instance.BetProvider.SaveBrokerState(data, brokerSchedule.Behaviormode, launchedRequest.Taskmodetype); sw.Stop(); launchedRequest.Duratation = (int) sw.ElapsedMilliseconds; launchedRequest.Counttotalrawci = stat[ProcessStatType.CompetitionItemFromRaw].TotalCount; launchedRequest.Countnewrawci = stat[ProcessStatType.CompetitionItemFromRaw].CreateRawCount; launchedRequest.Countnewci = stat[ProcessStatType.CompetitionItemFromRaw].CreateOriginalCount; launchedRequest.Counttotalentity = stat[additionalStat].TotalCount; launchedRequest.Countnewentity = stat[additionalStat].CreateOriginalCount; launchedRequest.Save(); } }); }