示例#1
0
        /// <inheritdoc />
        public async Task MoveInstance(Models.Instance instance, string oldPath, CancellationToken cancellationToken)
        {
            if (oldPath == null)
            {
                throw new ArgumentNullException(nameof(oldPath));
            }
            using var instanceReferenceCheck = GetInstanceReference(instance);
            if (instanceReferenceCheck != null)
            {
                throw new InvalidOperationException("Cannot move an online instance!");
            }
            var newPath = instance.Path;

            try
            {
                await ioManager.MoveDirectory(oldPath, newPath, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                logger.LogError(
                    ex,
                    "Error moving instance {0}!",
                    instance.Id);
                try
                {
                    logger.LogDebug("Reverting instance {0}'s path to {1} in the DB...", instance.Id, oldPath);

                    // DCT: Operation must always run
                    await databaseContextFactory.UseContext(db =>
                    {
                        var targetInstance = new Models.Instance
                        {
                            Id = instance.Id,
                        };
                        db.Instances.Attach(targetInstance);
                        targetInstance.Path = oldPath;
                        return(db.Save(default));