public void BinarySubmitContext_CanLogMissingOrEmptyTitle(string title)
        {
            var submitContext = new BinarySubmitContext();

            submitContext.FileName = title;

            Assert.Contains(_expectedNoTitleMessage, submitContext.LogPrefix());
        }
        public void BinarySubmitContext_CanLogMissingOrEmptyExternalId(string title)
        {
            var submitContext = new BinarySubmitContext();

            submitContext.ExternalId = title;

            Assert.Contains(_expectedNoExternalIdMessage, submitContext.LogPrefix());
        }
        public void BinarySubmitContext_CanLogTitle()
        {
            var title         = Guid.NewGuid().ToString();
            var submitContext = new BinarySubmitContext();

            submitContext.FileName = title;

            Assert.Contains(title, submitContext.LogPrefix());
        }
        public void BinarySubmitContext_CanLogExternalId()
        {
            var externalId    = Guid.NewGuid().ToString();
            var submitContext = new BinarySubmitContext();

            submitContext.ExternalId = externalId;

            Assert.Contains(externalId, submitContext.LogPrefix());
        }
Esempio n. 5
0
        public async Task RetryPolicy_Logs(Exception exception)
        {
            var           externalId    = Guid.NewGuid().ToString();
            SubmitContext submitContext = new BinarySubmitContext
            {
                ExternalId = externalId
            };

            var policy = ApiClientRetryPolicy.GetPolicy(
                _mockLog.Object,
                typeof(HttpSubmitBinaryPipelineElement),
                nameof(HttpSubmitBinaryPipelineElement.Submit),
                2,
                CancellationToken.None,
                submitContext.LogPrefix()
                );

            Type expectedExceptionType = exception.GetType();

            await Assert.ThrowsAsync(expectedExceptionType, async() =>
            {
                await policy.ExecuteAsync(
                    async(ct) =>
                {
                    throw exception;
                },
                    CancellationToken.None
                    );
            });

            _mockLog.Verify(x => x.LogMessage(
                                It.Is <Type>(t => t == typeof(HttpSubmitBinaryPipelineElement)),
                                It.Is <string>(m => m == "Submit_Retry"),
                                It.Is <string>(l => l.Contains(externalId)),
                                It.IsAny <long?>()
                                ), Times.Exactly(2));
        }
Esempio n. 6
0
        private async Task SubmitBinary(string itemExternalId, CancellationToken cancellationToken)
        {
            BinarySubmitContext binarySubmitContext = null;

            // Submit the binary!
            // Note the retry loop here - the binary submission endpoint may reject the submission
            // of a binary if the item it refers to has not been processed by the platform yet.
            int tryCount = 0;

            do
            {
                tryCount++;

                if (binarySubmitContext == null)
                {
                    binarySubmitContext = new BinarySubmitContext
                    {
                        TenantId                     = _connectorConfigModel.TenantIdAsGuid,
                        ConnectorConfigId            = _connectorConfigModel.IdAsGuid,
                        ApiClientFactorySettings     = ConnectorApiAuthHelper.GetApiClientFactorySettings(),
                        AuthenticationHelperSettings = ConnectorApiAuthHelper.GetAuthenticationHelperSettings(_connectorConfigModel.TenantDomainName),
                        CoreMetaData                 = new List <SubmissionMetaDataModel>(),
                        SourceMetaData               = new List <SubmissionMetaDataModel>(),
                        Filters           = _connectorConfigModel.Filters,
                        CancellationToken = cancellationToken
                    };

                    // Associate the binary with the item with the ItemExternalId field.
                    binarySubmitContext.ItemExternalId = itemExternalId;

                    // Set the "ExternalId" of the binary. The ExternalId uniquely identifies the binary in the content
                    // source. Note that this is not the same thing as the ExternalId of the item. An item may have
                    // multiple binaries associated with it - if multiple binaries are submitted, end users can download them
                    // as a zipped archive.
                    binarySubmitContext.ExternalId = Guid.NewGuid().ToString();

                    // Set the "FileName" of the binary. This field is optional. When it is provided, end users will
                    // get this filename by default when they download the binary from Records365 vNext.
                    binarySubmitContext.FileName = "file.txt";
                    binarySubmitContext.FileHash = "Optional MD5 Hash here";
                }

                var fileContent = "This is a binary file!";
                var fileBytes   = Encoding.Default.GetBytes(fileContent);
                using (var stream = new MemoryStream(fileBytes))
                {
                    // Set the Stream of the binary. This stream represents the binary content.
                    // Note that the MemoryStream.Position will be moved to the end on each API call
                    // So if the submission needs to be retried, a new stream should be assigned here
                    binarySubmitContext.Stream = stream;

                    try
                    {
                        await _binarySubmitPipeline.Submit(binarySubmitContext).ConfigureAwait(false);

                        HandleSubmitPipelineResult(binarySubmitContext);
                    }
                    catch (Exception)
                    {
                        // Something went wrong trying to submit the item.
                        // Dead-letter the item to a durable data store where it can be retried later. (e.g., a message broker).
                    }

                    // If the submit was deferred by the platform, wait a few seconds and try again.
                    if (binarySubmitContext.SubmitResult.SubmitStatus == SubmitResult.Status.Deferred)
                    {
                        await Task.Delay(TimeSpan.FromSeconds(2)).ConfigureAwait(false);
                    }
                }
            }while (binarySubmitContext.SubmitResult.SubmitStatus == SubmitResult.Status.Deferred && tryCount < 30);
        }