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)
                    {
                        _instanceLogger.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Warning, "PEMTableStorage_UpdateMessageAsync", "HTTP Error Adding PEM", execResult.HttpStatusCode.ToString().ToKVP("httpStatusCode"), retryCount.ToString().ToKVP("retryCount"));
                    }
                }
                catch (Exception ex)
                {
                    if (retryCount == numberRetries)
                    {
                        _instanceLogger.AddException("PEMTableStorage_UpdateMessageAsync", ex);
                        return(InvokeResult.FromException("AzureTableStorageConnector_ExecWithRetyr", ex));
                    }
                    else
                    {
                        _instanceLogger.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Warning, "PEMTableStorage_UpdateMessageAsync", "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);
        }
        public async Task <InvokeResult> InitAsync(DataStream stream)
        {
            _stream = stream;

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

                await _container.CreateIfNotExistsAsync();

                return(InvokeResult.Success);
            }
            catch (ArgumentException ex)
            {
                _instanceLogger.AddException("AzureBlobConnector_InitAsync", ex);
                return(InvokeResult.FromException("AzureBlobConnector_InitAsync", ex));
            }
            catch (StorageException ex)
            {
                _instanceLogger.AddException("AzureBlobConnector_InitAsync", ex);
                return(InvokeResult.FromException("AzureBlobConnector_InitAsync", 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)
            {
                _instanceLogger.AddException("AWSS3Connector_InitAsync", amazonS3Exception);
                return(InvokeResult.FromException("AWSS3Connector_InitAsync", amazonS3Exception));
            }

            return(InvokeResult.Success);
        }
Beispiel #4
0
        public async Task <byte[]> GetCertAsync(string domainName)
        {
            if (_settings == null || _instanceLogger == null)
            {
                throw new InvalidOperationException("[GetCertAsync] Not initialized.");
            }

            try
            {
                if (String.IsNullOrEmpty(domainName))
                {
                    throw new ArgumentNullException(nameof(domainName));
                }

                this._instanceLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Verbose, "BlobCertStorage_GetCertAsync", "Requested Cert", domainName.ToKVP("domainName"));
                using (var ms = new MemoryStream())
                {
                    var container = await GetContainerAsync();

                    var blob = container.GetBlockBlobReference(domainName);
                    if (!await blob.ExistsAsync())
                    {
                        this._instanceLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "BlobCertStorage_GetCertAsync", "Cert not found", domainName.ToKVP("domainName"));
                        return(null);
                    }

                    this._instanceLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Verbose, "BlobCertStorage_GetCertAsync", "Cert found", domainName.ToKVP("domainName"));
                    await blob.DownloadToStreamAsync(ms);

                    var buffer = ms.ToArray();
                    this._instanceLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Verbose, "BlobCertStorage_GetCertAsync", "Cert downloaded",
                                                        buffer.Length.ToString().ToKVP("bufferLen"), domainName.ToKVP("domainName"));
                    return(buffer);
                }
            }
            catch (Exception ex)
            {
                _instanceLogger.AddException("BlobCertStorage_GetCertAsync", ex, domainName.ToKVP("domainName"));
                return(null);
            }
        }
        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)
                    {
                        _instanceLogger.AddException("AzureTableStorageConnector_GetItemsAsync", ex);
                        return(InvokeResult.FromException("AzureBlobConnector_AddItemAsync", ex));
                    }
                    else
                    {
                        _instanceLogger.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);
        }