Ejemplo n.º 1
0
        /// <summary>
        /// Execute the transfer asynchronously.
        /// </summary>
        /// <param name="scheduler">Transfer scheduler</param>
        /// <param name="cancellationToken">Token that can be used to cancel the transfer.</param>
        /// <returns>A task representing the transfer operation.</returns>
        public override async Task ExecuteAsync(TransferScheduler scheduler, CancellationToken cancellationToken)
        {
            if (this.transferJob.Status == TransferJobStatus.Finished ||
                this.transferJob.Status == TransferJobStatus.Skipped)
            {
                return;
            }

            TransferEventArgs eventArgs = new TransferEventArgs(this.Source.Instance, this.Destination.Instance);

            eventArgs.StartTime = DateTime.UtcNow;

            if (this.transferJob.Status == TransferJobStatus.Failed)
            {
                // Resuming a failed transfer job
                if (string.IsNullOrEmpty(this.transferJob.CopyId))
                {
                    this.UpdateTransferJobStatus(this.transferJob, TransferJobStatus.Transfer);
                }
                else
                {
                    this.UpdateTransferJobStatus(this.transferJob, TransferJobStatus.Monitor);
                }
            }

            try
            {
                await scheduler.ExecuteJobAsync(this.transferJob, cancellationToken);

                if (TransferJobStatus.SkippedDueToShouldNotTransfer != this.transferJob.Status)
                {
                    eventArgs.EndTime = DateTime.UtcNow;
                    this.UpdateTransferJobStatus(this.transferJob, TransferJobStatus.Finished);

                    if (this.Context != null)
                    {
                        this.Context.OnTransferSuccess(eventArgs);
                    }
                }
            }
            catch (TransferException exception)
            {
                eventArgs.EndTime   = DateTime.UtcNow;
                eventArgs.Exception = exception;

                if (exception.ErrorCode == TransferErrorCode.NotOverwriteExistingDestination)
                {
                    // transfer skipped
                    this.UpdateTransferJobStatus(this.transferJob, TransferJobStatus.Skipped);
                    if (this.Context != null)
                    {
                        this.Context.OnTransferSkipped(eventArgs);
                    }

                    throw;
                }
                else if (exception.ErrorCode == TransferErrorCode.FailedCheckingShouldTransfer)
                {
                    throw;
                }
                else
                {
                    this.OnTransferFailed(eventArgs);
                    throw;
                }
            }
            catch (Exception ex)
            {
                eventArgs.EndTime   = DateTime.UtcNow;
                eventArgs.Exception = ex;

                this.OnTransferFailed(eventArgs);

                throw;
            }

            this.Journal?.RemoveTransfer(this);
        }