Esempio n. 1
0
        protected async Task <InvokeResult> ExecWithRetry(TableOperation operation, int numberRetries = 5)
        {
            var retryCount = 0;
            var completed  = false;

            while (retryCount++ < numberRetries && !completed)
            {
                try
                {
                    var execResult = await _cloudTable.ExecuteAsync(operation);

                    completed = (execResult.HttpStatusCode == 200 || execResult.HttpStatusCode == 204);
                    if (!completed)
                    {
                        _logger.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Warning, "AzureTableStorageConnector_ExecWithRetry", "HTTP Error Adding PEM", execResult.HttpStatusCode.ToString().ToKVP("httpStatusCode"), retryCount.ToString().ToKVP("retryCount"));
                    }
                }
                catch (Exception ex)
                {
                    if (retryCount == numberRetries)
                    {
                        _logger.AddException("AzureTableStorageConnector_ExecWithRetry", ex);
                        return(InvokeResult.FromException("AzureTableStorageConnector_ExecWithRetyr", ex));
                    }
                    else
                    {
                        _logger.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Warning, "AzureTableStorageConnector_ExecWithRetry", "Exception writing PEM, will retry", ex.Message.ToKVP("exceptionMessage"), ex.GetType().Name.ToKVP("exceptionType"), retryCount.ToString().ToKVP("retryCount"));
                    }
                    await Task.Delay(retryCount * 250);
                }
            }

            return(InvokeResult.Success);
        }
Esempio n. 2
0
        private async Task <InvokeResult <CloudBlobContainer> > GetStorageContainerAsync(IConnectionSettings settings, string containerName)
        {
            var client    = CreateBlobClient(settings);
            var container = client.GetContainerReference(containerName);

            try
            {
                var options = new BlobRequestOptions()
                {
                    MaximumExecutionTime = TimeSpan.FromSeconds(15)
                };

                var opContext = new OperationContext();
                await container.CreateIfNotExistsAsync(options, opContext);

                return(InvokeResult <CloudBlobContainer> .Create(container));
            }
            catch (ArgumentException ex)
            {
                _logger.AddException("DeviceMediaRepo_GetStorageContainerAsync", ex);
                return(InvokeResult <CloudBlobContainer> .FromException("DeviceMediaRepo_GetStorageContainerAsync_InitAsync", ex));
            }
            catch (StorageException ex)
            {
                _logger.AddException("DeviceMediaRepo_GetStorageContainerAsync", ex);
                return(InvokeResult <CloudBlobContainer> .FromException("DeviceMediaRepo_GetStorageContainerAsync", ex));
            }
        }
        private async Task <InvokeResult <CloudBlobContainer> > GetStorageContainerAsync(string orgId)
        {
            var client        = CreateBlobClient(_blobConnectionSettings);
            var containerName = $"dtresource-{orgId}".ToLower();

            Console.WriteLine(containerName);
            var container = client.GetContainerReference(containerName);

            try
            {
                var options = new BlobRequestOptions()
                {
                    MaximumExecutionTime = TimeSpan.FromSeconds(15)
                };

                var opContext = new OperationContext();
                await container.CreateIfNotExistsAsync(options, opContext);

                return(InvokeResult <CloudBlobContainer> .Create(container));
            }
            catch (ArgumentException ex)
            {
                _logger.AddException("MediaServicesRepo_GetStorageContainerAsync", ex);
                return(InvokeResult <CloudBlobContainer> .FromException("MediaServicesRepo_GetStorageContainerAsync_InitAsync", ex));
            }
            catch (StorageException ex)
            {
                _logger.AddException("MediaServicesRepo_GetStorageContainerAsync", ex);
                return(InvokeResult <CloudBlobContainer> .FromException("MediaServicesRepo_GetStorageContainerAsync", ex));
            }
        }
Esempio n. 4
0
        public void ShowColor(string color)
        {
            _logger.AddCustomEvent(LogLevel.Message, "LedController_ShowColor", color);

            try
            {
                var buffer = new byte[]
                {
                    (byte)'c',
                    (byte)'o',
                    (byte)'l',
                    (byte)'o',
                    (byte)'r',
                    (byte)' ',
                    (byte)byte.Parse(color.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
                    (byte)' ',
                    (byte)byte.Parse(color.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
                    (byte)' ',
                    (byte)byte.Parse(color.Substring(4, 2), System.Globalization.NumberStyles.HexNumber),
                    (byte)';',
                };

                _ledController.Write(buffer);
            }
            catch (Exception ex)
            {
                _logger.AddException("LedController", ex);
            }
        }
Esempio n. 5
0
        public async Task <InvokeResult> InitAsync(DataStream stream)
        {
            _stream = stream;

            _cloudBlobClient = CreateBlobClient(_stream);
            _container       = _cloudBlobClient.GetContainerReference(_stream.AzureBlobStorageContainerName);
            try
            {
                Microsoft.WindowsAzure.Storage.NameValidator.ValidateContainerName(_stream.AzureBlobStorageContainerName);

                var options = new BlobRequestOptions()
                {
                    MaximumExecutionTime = TimeSpan.FromSeconds(15)
                };

                var opContext = new OperationContext();
                await _container.CreateIfNotExistsAsync(options, opContext);

                return(InvokeResult.Success);
            }
            catch (ArgumentException ex)
            {
                _logger.AddException("AzureBlobConnector_InitAsync", ex);
                var result = InvokeResult.FromException("AzureBlobConnector_InitAsync", ex);
                return(result);
            }
            catch (StorageException ex)
            {
                _logger.AddException("AzureBlobConnector_InitAsync", ex);
                var result = InvokeResult.FromException("AzureBlobConnector_InitAsync", ex);
                return(result);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Инициализация потоков исторических данных
        /// </summary>
        /// <param name="insID">Инструмент</param>
        public void InitInsStores(int insID)
        {
            var isNewTran = _storage.BeginTransaction();

            try
            {
                foreach (var tf in _tfs)
                {
                    var insStore = _insStoreDA.GetInsStore(insID, tf);
                    if (insStore == null)
                    {
                        int insStoreID = _insStoreDA.CreateInsStore(insID, tf, true);
                        insStore = _insStoreDA.GetInsStoreByID(insStoreID);
                    }
                    BarRow bars = new BarRow(tf, insStore.InsID);
                    _insStore_barRow.Add(insStore, bars);

                    if (!_insID_barRows.ContainsKey(insID))
                    {
                        _insID_barRows.Add(insStore.InsID, new List <BarRow>());
                    }
                    _insID_barRows[insID].Add(bars);
                }
                _storage.Commit(isNewTran);
            }
            catch (Exception ex)
            {
                _storage.Rollback(isNewTran);
                _logger.AddException("InsStoreBL:CreateInsStore", ex);
            }
        }
        public async Task <InvokeResult> InitAsync(DataStream stream)
        {
            _stream = stream;
            var options = new CredentialProfileOptions
            {
                AccessKey = stream.AwsAccessKey,
                SecretKey = stream.AwsSecretKey
            };

            var profile    = new Amazon.Runtime.CredentialManagement.CredentialProfile($"awsprofile_{stream.Id}", options);
            var netSDKFile = new NetSDKCredentialsFile();

            netSDKFile.RegisterProfile(profile);

            var creds = AWSCredentialsFactory.GetAWSCredentials(profile, netSDKFile);

            try
            {
                _s3Client = new AmazonS3Client(creds, AWSRegionMappings.MapRegion(stream.AwsRegion));
                await _s3Client.EnsureBucketExistsAsync(stream.S3BucketName);
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                _logger.AddException("AWSS3Connector_InitAsync", amazonS3Exception);
                return(InvokeResult.FromException("AWSS3Connector_InitAsync", amazonS3Exception));
            }

            return(InvokeResult.Success);
        }
Esempio n. 8
0
        private async Task runAndExportSimulationsInSnapshotFile(FileInfo projectFile, string outputFolder, SimulationExportMode simulationExportMode)
        {
            _logger.AddInfo($"Starting batch simulation export for file '{projectFile}'");
            try
            {
                var project = await _snapshotTask.LoadProjectFromSnapshot(projectFile.FullName);

                var simulations         = project.All <Simulation>();
                var numberOfSimulations = simulations.Count;
                _logger.AddInfo($"{numberOfSimulations} {"simulation".PluralizeIf(numberOfSimulations)} found in file '{projectFile}'");

                foreach (var simulation in simulations)
                {
                    _logger.AddDebug($"Starting batch simulation export for simulation '{simulation.Name}'");

                    await _simulationExporter.RunAndExport(simulation, outputFolder, _simulationRunOptions, simulationExportMode, FileHelper.FileNameFromFileFullPath(projectFile.FullName));

                    _allSimulationNames.Add(simulation.Name);
                }
            }
            catch (Exception e)
            {
                _logger.AddException(e);
            }
        }
Esempio n. 9
0
        private async Task <IEnumerable <Model.Simulation> > allSmulationsFrom(Simulation[] snapshots, ModelProject project)
        {
            var simulations = new List <Model.Simulation>();

            if (snapshots == null)
            {
                return(simulations);
            }

            //do not run tasks in parallel as the same mapper instance may be used concurrently to load two different snapshots
            foreach (var snapshot in snapshots)
            {
                try
                {
                    var simulation = await _simulationMapper.MapToModel(snapshot, project);

                    simulations.Add(simulation);
                }
                catch (Exception e)
                {
                    _logger.AddException(e);
                    _logger.AddError(PKSimConstants.Error.CannotLoadSimulation(snapshot.Name));
                }
            }

            return(simulations);
        }
Esempio n. 10
0
        /// <summary>
        /// Сохранение данных по всем сделкам для всех инструментов
        /// </summary>
        /// <param name="tickDispatcher">Диспетчер тиковых данных</param>
        /// <param name="sessionDbPath">Каталог данных текущей сессии (определяется датой)</param>
        public void SaveData(ITickDispatcher tickDispatcher, string sessionDbPath)
        {
            if (tickDispatcher == null)
            {
                throw new ArgumentNullException("tickDispatcher");
            }
            if (string.IsNullOrWhiteSpace(sessionDbPath))
            {
                throw new ArgumentException("SessionDbPath is empty, session not opened.");
            }

            _logger.AddInfo("AllTradesData", "Save data ...");
            try
            {
                var allTradesDir = Path.Combine(sessionDbPath, "AllTrades");

                if (!Directory.Exists(allTradesDir))
                {
                    Directory.CreateDirectory(allTradesDir);
                }

                var insIDs = tickDispatcher.GetInstrumIDs();
                foreach (var insID in insIDs)
                {
                    Instrum ins = _instrumTable.GetInstrum(insID);
                    if (ins == null)
                    {
                        continue;
                    }
                    var ticks = tickDispatcher.GetTicks(insID);
                    if (ticks == null || !ticks.Any())
                    {
                        continue;
                    }

                    var encoder = new AllTradesEncoder(ins.Decimals);
                    var persist = new AllTradesPersist();
                    persist.Initialize(allTradesDir, ins.Ticker);
                    _insStoreData.InitInsStores(insID);

                    foreach (Tick tick in ticks)
                    {
                        uint   seconds = (uint)(tick.Time.Hour * 60 * 60 + tick.Time.Minute * 60 + tick.Time.Second);
                        byte[] buf     = encoder.AddTick(seconds, tick.Price, tick.Lots);
                        persist.Write(buf);
                        _insStoreData.AddTick(insID, tick.Time, tick.Price, tick.Lots);
                    }
                    persist.Close();
                }
                _insStoreData.SaveData();
            }
            catch (Exception ex)
            {
                _logger.AddException("AllTradesData", ex);
            }
            _logger.AddInfo("AllTradesData", "Data saved");
        }
Esempio n. 11
0
 protected override void OnStart(string[] args)
 {
     _logger.AddInfo("Service", "OnStart");
     try
     {
         _app.Initialize();
     }
     catch (Exception ex)
     {
         _logger.AddException("Service", ex);
     }
 }
Esempio n. 12
0
        public void Initialize()
        {
            _logger.AddInfo("BotManager", "Initialize ...");

            _key_bots.Clear();
            _bot_platform.Clear();
            var keys = _botsConfig.GetBotKeys();

            foreach (var key in keys)
            {
                var conf = _botsConfig.GetBotConfig(key);
                if (conf == null)
                {
                    continue;
                }

                try
                {
                    var asm = Assembly.LoadFrom(conf.Assembly);
                    if (asm == null)
                    {
                        continue;
                    }

                    var type = asm.GetType(conf.Class);
                    if (type == null)
                    {
                        continue;
                    }

                    var platform = IoC.Resolve <ILeechPlatform>();
                    var bot      = Activator.CreateInstance(type, platform) as IBot;
                    if (bot == null)
                    {
                        continue;
                    }

                    bot.Initialize(conf.InitData);

                    // после успешной инициализации бота
                    _bot_platform.Add(bot, platform);
                    _key_bots.Add(key, bot);
                    _logger.AddInfo("BotManager", "Bot '" + key + "' initialized");
                }
                catch (Exception ex)
                {
                    _logger.AddException("BotManager", ex);
                }
            }
            _logger.AddInfo("BotManager", "Initialized");
        }
        public async Task <InvokeResult> ExecuteNonQuery(String sql)
        {
            using (var cn = OpenConnection(_stream.DbName))
            {
                using (var cmd = new NpgsqlCommand())
                {
                    try
                    {
                        cmd.Connection  = cn;
                        cmd.CommandText = sql;
                        await cmd.ExecuteNonQueryAsync();

                        return(InvokeResult.Success);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        _logger.AddException("PostgresqlConnector_ExecuteNonQuery", ex);
                        return(InvokeResult.FromException("PostgresqlConnector_ExecuteNonQuery", ex));
                    }
                }
            }
        }
Esempio n. 14
0
        public async Task Start()
        {
            try
            {
                _logPresenter.ClearLog();
                _view.EnableButtons(false);
                _logger.AddInfo(PKSimConstants.Information.LoadingSnapshot(_loadFromSnapshotDTO.SnapshotFile, typeToLoad));
                await Task.Run(() => PerformLoad(_loadFromSnapshotDTO.SnapshotFile));

                _logger.AddInfo(PKSimConstants.Information.SnapshotLoaded(typeToLoad));
            }
            catch (Exception e)
            {
                _logger.AddException(e);
            }
            finally
            {
                _view.EnableButtons(cancelEnabled: true, okEnabled: ModelIsDefined, startEnabled: CanClose);
            }
        }
        public async Task <InvokeResult> AddItemAsync(DataStreamRecord item)
        {
            var recordId = DateTime.UtcNow.ToInverseTicksRowKey();

            item.Data.Add(_stream.TimestampFieldName, item.GetTimeStampValue(_stream));
            item.Data.Add("sortOrder", item.GetTicks());
            item.Data.Add("deviceId", item.DeviceId);
            item.Data.Add("id", recordId);
            item.Data.Add("dataStreamId", _stream.Id);

            var json      = JsonConvert.SerializeObject(item.Data);
            var buffer    = Encoding.UTF8.GetBytes(json);
            var eventData = new EventData(buffer);

            var numberRetries = 5;
            var retryCount    = 0;
            var completed     = false;

            while (retryCount++ < numberRetries && !completed)
            {
                try
                {
                    await _eventHubClient.SendAsync(eventData);
                }
                catch (Exception ex)
                {
                    if (retryCount == numberRetries)
                    {
                        _logger.AddException("AzureTableStorageConnector_GetItemsAsync", ex);
                        return(InvokeResult.FromException("AzureBlobConnector_AddItemAsync", ex));
                    }
                    else
                    {
                        _logger.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Warning, "AzureTableStorageConnector_GetItemsAsync", "", ex.Message.ToKVP("exceptionMessage"), ex.GetType().Name.ToKVP("exceptionType"), retryCount.ToString().ToKVP("retryCount"));
                    }
                    await Task.Delay(retryCount * 250);
                }
            }

            return(InvokeResult.Success);
        }
Esempio n. 16
0
        private void DoWork()
        {
            int lasttime = -1;

            _logger.AddInfo("Scheduler", "Working");
            while (_isWorking)
            {
                var now     = DateTime.Now;
                int curtime = now.Hour * 10000 + now.Minute * 100 + now.Second;
                if (lasttime == curtime)
                {
                    Thread.Sleep(200);
                    continue;
                }
                lasttime = curtime;

                lock (_time_action)
                {
                    foreach (var time in _time_action.Keys)
                    {
                        if (time != curtime)
                        {
                            continue;
                        }
                        Task.Factory.StartNew(() =>
                        {
                            try
                            {
                                _time_action[time].Invoke();
                            }
                            catch (Exception ex)
                            {
                                _logger.AddException("Scheduler", ex);
                            }
                        });
                    }
                }
            }
            _logger.AddInfo("Scheduler", "Stop work");
        }
Esempio n. 17
0
        public virtual async Task RunBatch()
        {
            if (_isRunning)
            {
                return;
            }
            _isRunning = true;
            _logPresenter.ClearLog();
            _view.CalculateEnabled = false;

            try
            {
                await StartBatch();
            }
            catch (Exception e)
            {
                _batchLogger.AddException(e);
            }

            _isRunning             = false;
            _view.CalculateEnabled = true;
        }
Esempio n. 18
0
        public void Load()
        {
            _logger.AddInfo("BotsConfiguration", "Loading bots configuration ...");
            _key_botconf.Clear();

            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(_config.GetBotsConfigPath());

                foreach (XmlNode xn in xDoc.DocumentElement.ChildNodes)
                {
                    if (xn.Name != "Bot")
                    {
                        _logger.AddInfo("BotsConfiguration", "Skip node: " + xn.Name);
                        continue;
                    }

                    var xa_key = xn.Attributes["Key"];
                    if (xa_key == null)
                    {
                        _logger.AddError("BotsConfiguration", "Key attribute not found.");
                        continue;
                    }
                    string key = xa_key.Value;

                    var xa_assembly = xn.Attributes["Assembly"];
                    if (xa_assembly == null)
                    {
                        _logger.AddError("BotsConfiguration", "Assembly attribute not found.");
                        continue;
                    }
                    string assembly = xa_assembly.Value;
                    if (!Path.IsPathRooted(assembly))
                    {
                        assembly = Path.Combine(_config.GetBotsPath(), assembly);
                    }

                    var xa_class = xn.Attributes["Class"];
                    if (xa_class == null)
                    {
                        _logger.AddError("BotsConfiguration", "Class attribute not found.");
                        continue;
                    }
                    string cls = xa_class.Value;

                    var    xa_initdata = xn.Attributes["InitData"];
                    string initdata    = "";
                    if (xa_initdata != null)
                    {
                        initdata = xa_initdata.Value;
                    }
                    else
                    {
                        _logger.AddInfo("BotsConfiguration", "InitData attribute not found. Empty string used.");
                    }

                    if (_key_botconf.ContainsKey(key))
                    {
                        _logger.AddError("BotsConfiguration", "Duplicate key: " + key);
                        continue;
                    }

                    _key_botconf.Add(key, new BotConfig()
                    {
                        Assembly = assembly,
                        Class    = cls,
                        InitData = initdata,
                        Key      = key
                    });
                    _logger.AddInfo("BotsConfiguration",
                                    string.Format("Bot config load: Key={0}, Assembly={1}, Class={2}", key, assembly, cls));
                }
            }
            catch (FileNotFoundException ex)
            {
                _logger.AddError("BotsConfiguration", "Bots config file not found");
            }
            catch (Exception ex)
            {
                _logger.AddException("BotsConfiguration", ex);
            }
        }
Esempio n. 19
0
 public static void AddException <T>(this ILogger logger, Exception exception) => logger.AddException(exception, typeof(T).Name);
Esempio n. 20
0
        public async Task <RawResponse> PerformCall(Func <Task <HttpResponseMessage> > call, CancellationTokenSource cancellationTokenSource)
        {
            await _callSemaphore.WaitAsync();

            var retry = true;

            var rawResponse = RawResponse.FromNotCompleted();

            while (retry)
            {
                _httpClient.DefaultRequestHeaders.Clear();
                if (_authManager.IsAuthenticated)
                {
                    _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _authManager.AccessToken);
                }

                retry = false;
                try
                {
                    var start    = DateTime.Now;
                    var response = await call();

                    var delta = DateTime.Now - start;


                    if (response.IsSuccessStatusCode)
                    {
                        rawResponse = RawResponse.FromSuccess(await response.Content.ReadAsStringAsync());
                    }
                    else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        _logger.AddCustomEvent(LogLevel.Error, "RawRestClient_PerformCall", "401 From Server");
                        retry = ((await RenewRefreshToken()).Successful);
                        if (!retry)
                        {
                            rawResponse = RawResponse.FromNotAuthorized();
                        }
                    }
                    else
                    {
                        _logger.AddCustomEvent(LogLevel.Message, "RawRestClient_PerformCall", $"Http Error {(int)response.StatusCode}");
                        /* Check for 401 (I think, if so then attempt to get a new access token,  */
                        rawResponse = RawResponse.FromHttpFault((int)response.StatusCode, $"{ClientResources.Err_GeneralErrorCallingServer} : HTTP{(int)response.StatusCode} - {response.ReasonPhrase}");
                    }
                }
                catch (Exceptions.CouldNotRenewTokenException)
                {
                    _callSemaphore.Release();
                    throw;
                }
                catch (TaskCanceledException tce)
                {
                    _logger.AddException("RawRestClient_PerformCall_TaskCancelled", tce);
                    rawResponse = RawResponse.FromException(tce, tce.CancellationToken.IsCancellationRequested);
                }
                catch (Exception ex)
                {
                    _logger.AddException("RawRestClient_PerformCall", ex);
                    rawResponse = RawResponse.FromException(ex);
                }
            }

            _callSemaphore.Release();

            return(rawResponse);
        }