Ejemplo n.º 1
0
        protected override async Task <Tuple <bool, JObject> > TryObtainLockAsync(string lockMessage, CancellationToken token)
        {
            var result = false;
            var json   = new JObject();

            try
            {
                if (await FileExistsAsync(client, bucketName, LockFile, token).ConfigureAwait(false))
                {
                    // Read the existing message
                    json = await GetExistingMessage(json, token);
                }
                else
                {
                    // Create a new lock
                    json = GetMessageJson(lockMessage);

                    await CreateFileAsync(client, bucketName, LockFile, json.ToString(), serverSideEncryptionMethod, token).ConfigureAwait(false);

                    result = true;
                }
            }
            catch (AmazonS3Exception ex) when
                (ex.StatusCode != HttpStatusCode.Forbidden &&
                ex.StatusCode != HttpStatusCode.Unauthorized &&
                ex.StatusCode != HttpStatusCode.BadRequest)
            {
                // Ignore and retry, there may be a race case writing the lock file.
                ExceptionUtilsSleetLib.LogException(ex, Log, LogLevel.Verbose);
            }

            return(Tuple.Create(result, json));
        }
Ejemplo n.º 2
0
        private async Task <JObject> GetMessageJson()
        {
            try
            {
                var text = await _messageBlob.DownloadTextAsync();

                return(JObject.Parse(text));
            }
            catch (Exception ex)
            {
                // ignore
                ExceptionUtilsSleetLib.LogException(ex, Log, LogLevel.Debug);
            }

            return(new JObject());
        }
Ejemplo n.º 3
0
        private async Task KeepLock()
        {
            var renewTime = TimeSpan.FromSeconds(15);

            try
            {
                while (!_cts.IsCancellationRequested)
                {
                    try
                    {
                        await _lease.Renew();

                        await Task.Delay(renewTime, _cts.Token);
                    }
                    catch (TaskCanceledException)
                    {
                        // Ignore
                    }
                    catch (Exception ex)
                    {
                        // Ignore
                        Log.LogWarning("Failed to renew lock on feed. If another client takes the lock conflicts could occur.");
                        ExceptionUtilsSleetLib.LogException(ex, Log, LogLevel.Warning);
                    }
                }

                // Exit lock
                // Make sure writing to the lock finished
                await _updateLockMessage;

                // Delete the message file
                await _messageBlob.DeleteIfExistsAsync();
            }
            catch (Exception ex)
            {
                // Ignore
                Log.LogWarning("Unable to clear lock messsage");
                ExceptionUtilsSleetLib.LogException(ex, Log, LogLevel.Warning);
            }
        }
Ejemplo n.º 4
0
        protected override async Task <Tuple <bool, JObject> > TryObtainLockAsync(string lockMessage, CancellationToken token)
        {
            var result = false;
            var json   = new JObject();

            try
            {
                if (File.Exists(LockFile))
                {
                    // Read message from existing lock file
                    json = await JsonUtility.LoadJsonAsync(LockPath);
                }
                else
                {
                    // Obtain the lock
                    Directory.CreateDirectory(Path.GetDirectoryName(LockPath));

                    json = GetMessageJson(lockMessage);
                    json.Add(new JProperty("pid", Process.GetCurrentProcess().Id));

                    using (var stream = new FileStream(LockPath, FileMode.CreateNew))
                    {
                        await JsonUtility.WriteJsonAsync(json, stream);
                    }

                    result = true;
                }
            }
            catch (Exception ex)
            {
                // Ignore and retry
                ExceptionUtilsSleetLib.LogException(ex, Log, LogLevel.Debug);
            }

            return(Tuple.Create(result, json));
        }