Exemple #1
0
        public static async Task <ISleetFileSystemLock> VerifyInitAndLock(LocalSettings settings, ISleetFileSystem fileSystem, string lockMessage, ILogger log, CancellationToken token)
        {
            ISleetFileSystemLock feedLock = null;

            ValidateFileSystem(fileSystem);

            try
            {
                // Validate source
                var exists = await fileSystem.Validate(log, token);

                if (!exists)
                {
                    throw new InvalidOperationException($"Unable to use feed.");
                }

                var timer = Stopwatch.StartNew();
                feedLock = fileSystem.CreateLock(log);

                // Use the message from settings as an override if it exists.
                var lockInfoMessage = string.IsNullOrEmpty(settings.FeedLockMessage) ? lockMessage : settings.FeedLockMessage;

                // Get lock
                var isLocked = await feedLock.GetLock(settings.FeedLockTimeout, lockInfoMessage, token);

                if (!isLocked)
                {
                    throw new InvalidOperationException("Unable to obtain a lock on the feed.");
                }

                // Log perf
                timer.Stop();
                fileSystem.LocalCache.PerfTracker.Add(new PerfSummaryEntry(timer.Elapsed, "Obtained feed lock in {0}", TimeSpan.FromSeconds(30)));

                var indexPath = fileSystem.Get("index.json");

                if (!await indexPath.ExistsWithFetch(log, token))
                {
                    throw new InvalidOperationException($"{fileSystem.BaseURI} is missing sleet files. Use 'sleet.exe init' to create a new feed.");
                }
            }
            catch
            {
                if (feedLock != null)
                {
                    feedLock.Release();
                }

                throw;
            }

            return(feedLock);
        }
Exemple #2
0
        /// <summary>
        /// Ensure a feed is initialized. If the feed is not initialized it can be automatically created and initialized.
        /// Feeds that are not in a vaild state will fail, these must be manually fixed to avoid losing data.
        /// </summary>
        /// <param name="lockMessage">Optional message to display when the feed is locked.</param>
        /// <param name="autoCreateBucket">Automatically create the folder/container/bucket without files.</param>
        /// <param name="autoInit">Automatically initialize the files in the feed.</param>
        public static async Task <ISleetFileSystemLock> InitAndLock(LocalSettings settings, ISleetFileSystem fileSystem, string lockMessage, bool autoCreateBucket, bool autoInit, ILogger log, CancellationToken token)
        {
            ISleetFileSystemLock feedLock = null;

            // Validate URI path
            ValidateFileSystem(fileSystem);

            // Create the bucket if allowed or throw.
            await EnsureBucketOrThrow(fileSystem, autoCreateBucket, log, token);

            try
            {
                var timer = Stopwatch.StartNew();
                feedLock = fileSystem.CreateLock(log);

                // Use the message from settings as an override if it exists.
                var lockInfoMessage = string.IsNullOrEmpty(settings.FeedLockMessage) ? lockMessage : settings.FeedLockMessage;

                // Get lock
                var isLocked = await feedLock.GetLock(settings.FeedLockTimeout, lockInfoMessage, token);

                if (!isLocked)
                {
                    throw new InvalidOperationException("Unable to obtain a lock on the feed.");
                }

                // Log perf
                timer.Stop();
                fileSystem.LocalCache.PerfTracker.Add(new PerfSummaryEntry(timer.Elapsed, "Obtained feed lock in {0}", TimeSpan.FromSeconds(30)));

                // Reset the file system to avoid using files retrieved before the lock, this would be unsafe
                fileSystem.Reset();

                await EnsureFeedIndexOrThrow(settings, fileSystem, autoInit, log, token);
            }
            catch
            {
                if (feedLock != null)
                {
                    feedLock.Release();
                }

                throw;
            }

            return(feedLock);
        }
Exemple #3
0
        public static async Task <ISleetFileSystemLock> VerifyInitAndLock(LocalSettings settings, ISleetFileSystem fileSystem, ILogger log, CancellationToken token)
        {
            ISleetFileSystemLock feedLock = null;

            ValidateFileSystem(fileSystem);

            try
            {
                // Validate source
                var exists = await fileSystem.Validate(log, token);

                if (!exists)
                {
                    throw new InvalidOperationException($"Unable to use feed.");
                }

                feedLock = fileSystem.CreateLock(log);
                var isLocked = await feedLock.GetLock(settings.FeedLockTimeout, token);

                if (!isLocked)
                {
                    throw new InvalidOperationException("Unable to obtain a lock on the feed.");
                }

                var indexPath = fileSystem.Get("index.json");

                if (!await indexPath.ExistsWithFetch(log, token))
                {
                    throw new InvalidOperationException($"{fileSystem.BaseURI} is missing sleet files. Use 'sleet.exe init' to create a new feed.");
                }
            }
            catch
            {
                if (feedLock != null)
                {
                    feedLock.Release();
                }

                throw;
            }

            return(feedLock);
        }