コード例 #1
0
        private void CompletePendingTransactions(SmugglerResult result)
        {
            // If we export documents from a database,
            // we should wait for all the pending transactions to be completed first.
            var shouldExecute = _options.ExecutePendingClusterTransactions ||
                                (_options.OperateOnTypes.HasFlag(DatabaseItemType.Documents) && _source is DatabaseSource);

            if (shouldExecute == false)
            {
                return;
            }

            _database.ExectueClusterTransactionOnDatabase(WaitForClusterTransactionCompletion);

            void WaitForClusterTransactionCompletion(IReadOnlyList <Task> transactionTasks)
            {
                result.AddInfo($"Has to processing {transactionTasks.Count} cluster transactions before the export can take place.");
                _onProgress.Invoke(result.Progress);

                for (var index = 0; index < transactionTasks.Count; index++)
                {
                    var task = transactionTasks[index];

                    _token.ThrowIfCancellationRequested();
                    while (task.IsCompleted == false)
                    {
                        _token.ThrowIfCancellationRequested();
                        if (task.Wait((int)TimeSpan.FromSeconds(10).TotalMilliseconds, _token) == false)
                        {
                            _token.ThrowIfCancellationRequested();
                            result.AddInfo($"Processing cluster transaction {index}.");
                            _onProgress.Invoke(result.Progress);
                        }
                    }

                    if (task.IsCompletedSuccessfully)
                    {
                        result.AddInfo($"Cluster transaction {index} out of {transactionTasks.Count} is completed.");
                        _onProgress.Invoke(result.Progress);
                    }

                    if (task.IsCanceled)
                    {
                        result.AddInfo($"Cluster transaction {index} was canceled.");
                        _onProgress.Invoke(result.Progress);
                    }

                    if (task.IsFaulted)
                    {
                        _result.AddError($"Cluster transaction {index} is faulted: {task.Exception}.");
                        _onProgress.Invoke(result.Progress);
                    }
                }
            }
        }