public override void Run() { Task hostUpdaterTask = null; Trace.TraceInformation("Instance run - Entering main loop"); while (true) { try { bool isMainSequencerFreeze = bool.Parse(RoleEnvironment.GetConfigurationSettingValue("MainSequencerFreeze")); bool hasDriveMounted = false; // 1.We try to mount each disk declared in azure table while (!hasDriveMounted) { shard = PoolForDisk(); hasDriveMounted = shard != null; if (hasDriveMounted) break; Trace.TraceInformation("Instance run - Waiting for disk to mount"); Thread.Sleep(2000); } // 2. Update Entry in Azure Table Task updateIp = new Task(new Action(() => { bool ipHasBeenUpdated = false; // We have mounted a drive, we must register this IP address Trace.TraceInformation("Instance start - Registering IP"); while (!ipHasBeenUpdated) { // ipHasBeenUpdated = MongoHelper.UpdateShardIpWithName(shard.RowKey); if (ipHasBeenUpdated) { Trace.TraceInformation("Instance start - Registering successful"); } else { Trace.TraceError("Instance start - Registering IP failed! will retry in 5s"); } Thread.Sleep(5000); } })); updateIp.Start(); hostUpdaterTask = new Task(new Action(() => { HostUpdater.Run(true); })); while (true) { try { if (hostUpdaterTask.Status != TaskStatus.Running) { hostUpdaterTask.Start(); } } catch (Exception ex) { Trace.TraceError(ex.ToString()); } try { //Serialize operations to avoid conflicting instructions with MongoD process lock (MainSequencerLock) { if (isMainSequencerFreeze) { Trace.TraceWarning("Instance run Main Loop - MainSequencerFreeze = True : Doing nothing"); } else { // Time for action, depending of our status //========================================= ReplicaSetRoleManager.ReplicaSetRoleState CurrentStatus = ReplicaSetRoleManager.GetState(); if (ReplicaSetRoleManager.ReplicaSetRoleState.InstanceRunning != CurrentStatus) { bool autorestart = false; bool.TryParse(RoleEnvironment.GetConfigurationSettingValue("AutoRestart"), out autorestart); if (autorestart) { Trace.TraceInformation("MongoD is not running, will launch a process"); LaunchMongoDProcess(); } } } } } catch (Exception ex) { Trace.TraceError(string.Format("Instance run - Exception in Main loop : {0} ({1}) at {2}", ex.Message, ex.InnerException == null ? "" : ex.InnerException.Message, ex.StackTrace)); } catch { Trace.TraceError("Uncaught exception in Main Sequencer"); } } } catch (Exception ex) { Trace.TraceError("Error in main loop : " + ex.ToString()); } catch { Trace.TraceError("Uncaught exception in Main Loop"); } } }
public static bool CreateShardInfo(string name, string vhd, string ip) { InitializeTableHelper(); ShardInfo info = new ShardInfo(name, vhd, RoleEnvironment.CurrentRoleInstance.Id, ip); return _tableHelperShardInfo.AddItem(info); }