public static void WaitForBucket(IAmazonS3 client, string bucketName, int maxSeconds) { var sleeper = UtilityMethods.ListSleeper.Create(); UtilityMethods.WaitUntilSuccess(() => { //Check if a bucket exists by trying to put an object in it var key = Guid.NewGuid().ToString() + "_existskey"; var res = client.PutObject(new PutObjectRequest { BucketName = bucketName, Key = key, ContentBody = "exists..." }); try { client.Delete(bucketName, key, null); } catch { Console.WriteLine($"Eventual consistency error: failed to delete key {key} from bucket {bucketName}"); } return(true); }); //Double check the bucket still exists using the DoesBucketExistV2 method var exists = S3TestUtils.WaitForConsistency(() => { return(AmazonS3Util.DoesS3BucketExistV2(client, bucketName) ? (bool?)true : null); }); }
public static T WaitForConsistency <T>(Func <T> loadFunction) { //First try waiting up to 60 seconds. var firstWaitSeconds = 60; try { return(UtilityMethods.WaitUntilSuccess(loadFunction, 10, firstWaitSeconds)); } catch { Console.WriteLine($"Eventual consistency wait: could not resolve eventual consistency after {firstWaitSeconds} seconds. Attempting to resolve..."); } //Spin through request to try to get the expected result. As soon as we get a non null result use it. for (var spinCounter = 0; spinCounter < MAX_SPIN_LOOPS; spinCounter++) { try { T result = loadFunction(); if (result != null) { if (spinCounter != 0) { //Only log that a wait happened if it didn't do it on the first time. Console.WriteLine($"Eventual consistency wait successful on attempt {spinCounter + 1}."); } return(result); } } catch { } Thread.Sleep(0); } //If we don't have an ok result then spend the normal wait period to wait for eventual consistency. Console.WriteLine($"Eventual consistency wait: could not resolve eventual consistency after {MAX_SPIN_LOOPS}. Waiting normally..."); var lastWaitSeconds = 240; //4 minute wait. return(UtilityMethods.WaitUntilSuccess(loadFunction, 5, lastWaitSeconds)); }