コード例 #1
0
 public static void DoEvery(CloudBlockBlob 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(AccessCondition.GenerateLeaseCondition(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);
     }
 }
コード例 #2
0
        public void Run()
        {
            _isRecycling = false;
            TenantStatus = TenantStatus.Starting;
            Trace.TraceInformation("tenant {0} starting", TenantNumber);
            var container = _blobClient.GetContainerReference("locks");
            var blob      = container.GetBlockBlobReference("tenant" + TenantNumber);

            var amITheLeader = AzureLeaderElectionProvider.AmITheLeader;
            var tenantStatus = GetTenantStatus();

            if (amITheLeader == false && tenantStatus != TenantStatus.Started)
            {
                Trace.TraceInformation("Tenant {0} not started on the leader \t\t Recycling tenant", TenantNumber);
                return; //recycle tenant
            }

            using (var arl = new AutoRenewLease(blob))
            {
                if (arl.HasLease)
                {
                    Trace.TraceInformation("tenant {0} setup...", TenantNumber);
                    Thread.Sleep(10000);
                    Trace.TraceInformation("tenant {0} setup done", TenantNumber);
                }
                else
                {
                    Trace.TraceInformation("Lock exception on tenant {0} \t Recycle tenant", TenantNumber);
                    return; // recycle tenant
                }
            } // lease is released here
            Trace.TraceInformation("tenant {0} started", TenantNumber);
            TenantStatus = TenantStatus.Started;
            while (true)
            {
                GenerateEvent();
                Tick();
                if (_isRecycling)
                {
                    return;
                }
            }
        }
コード例 #3
0
 public static void DoOnce(CloudBlockBlob 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(AccessCondition.GenerateLeaseCondition(arl.leaseId));
             }
             else
             {
                 Thread.Sleep(pollingFrequency);
             }
         }
     }
 }