public static void DoOnce(CloudBlob blob, Action action, TimeSpan pollingFrequency) { // blob.Exists has the side effect of calling blob.FetchAttributes, which populates the metadata collection while (!blob.Exists() || blob.Metadata["progress"] != "done") { using (var arl = new AutoRenewLease(blob)) { if (arl.HasLease) { action(); blob.Metadata["progress"] = "done"; blob.SetMetadata(arl.leaseId); } else { Thread.Sleep(pollingFrequency); } } } }
static long WriteWithoutCompression(CloudBlob blob, BlobRequestOptions mapped, Action<Stream> writer) { var md5 = MD5.Create(); long position; using (var stream = blob.OpenWrite(mapped)) { using (var crypto = new CryptoStream(stream, md5, CryptoStreamMode.Write)) { writer(crypto); } position = stream.Position; } blob.Metadata[LokadHashFieldName] = Convert.ToBase64String(md5.Hash); blob.SetMetadata(); return position; }
public static void DoEvery(CloudBlob blob, TimeSpan interval, Action action) { while (true) { var lastPerformed = DateTimeOffset.MinValue; using (var arl = new AutoRenewLease(blob)) { if (arl.HasLease) { blob.FetchAttributes(); DateTimeOffset.TryParseExact(blob.Metadata["lastPerformed"], "R", CultureInfo.CurrentCulture, DateTimeStyles.AdjustToUniversal, out lastPerformed); if (DateTimeOffset.UtcNow >= lastPerformed + interval) { action(); lastPerformed = DateTimeOffset.UtcNow; blob.Metadata["lastPerformed"] = lastPerformed.ToString("R"); blob.SetMetadata(arl.leaseId); } } } var timeLeft = (lastPerformed + interval) - DateTimeOffset.UtcNow; var minimum = TimeSpan.FromSeconds(5); // so we're not polling the leased blob too fast Thread.Sleep( timeLeft > minimum ? timeLeft : minimum); } }
static long WriteWithCompression(CloudBlob blob, BlobRequestOptions mapped, Action<Stream> writer) { long position; var md5 = MD5.Create(); blob.Properties.ContentEncoding = "gzip"; using (var stream = blob.OpenWrite(mapped)) { using (var crypto = new CryptoStream(stream, md5, CryptoStreamMode.Write)) using (var compress = new GZipStream(crypto, CompressionMode.Compress, true)) { writer(compress); } position = stream.Position; } blob.Metadata[LokadHashFieldName] = Convert.ToBase64String(md5.Hash); blob.SetMetadata(); return position; }
private static void BreakLease(CloudBlob blob) { try { blob.BreakLease(TimeSpan.Zero); } catch { } try { blob.Metadata.Remove(leasedForTestTag); blob.SetMetadata(); } catch { } }