Created
October 12, 2015 14:09
-
-
Save Aaronontheweb/29c0f65a2721ba22d44c to your computer and use it in GitHub Desktop.
Akka.NET Cluster Bootstrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WebCrawler.AzureBootstrap | |
{ | |
public static class AzureRuntimeBootLoader | |
{ | |
public static async Task<Config> CreateRuntimeConfig(string seedRoleName, string akkaEndpoint) | |
{ | |
// Need to wait until at least 1 instance of our seed role is up and available | |
while(!IsSeedRoleAvailable(seedRoleName)) | |
{ | |
await Task.Delay(TimeSpan.FromSeconds(1.5)); | |
} | |
return CreateConfig(seedRoleName, akkaEndpoint); | |
} | |
public static bool IsSeedRoleAvailable(string seedRoleName) | |
{ | |
if (!RoleEnvironment.Roles.ContainsKey(seedRoleName)) | |
throw new ArgumentException("Undefined role " + seedRoleName + " in Azure service definition"); | |
return RoleEnvironment.Roles[seedRoleName].Instances.Any(); | |
} | |
private static Config CreateConfig(string seedRoleName, string akkaEndpoint) | |
{ | |
var section = (AkkaConfigurationSection)ConfigurationManager.GetSection("akka"); | |
var clusterConfig = section.AkkaConfig; | |
var selfAzureAddress = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[akkaEndpoint]; | |
var selfAkkaAddress = selfAzureAddress.IPEndpoint.Address; | |
var selfPort = selfAzureAddress.IPEndpoint.Port; | |
var seedNodes = new List<string>(); | |
var trackerRoles = RoleEnvironment.Roles[seedRoleName].Instances; | |
foreach (var trackerEndpoint in trackerRoles.Select(x => x.InstanceEndpoints[akkaEndpoint])) | |
{ | |
seedNodes.Add(string.Format(@"""akka.tcp://{0}@{1}:{2}""", "webcrawler", trackerEndpoint.IPEndpoint.Address, trackerEndpoint.IPEndpoint.Port)); | |
} | |
var remoteConfig = ConfigurationFactory.ParseString(string.Format(@"akka.remote.helios.tcp.public-hostname = ""{0}"" | |
akka.remote.helios.tcp.port = {1}", selfAkkaAddress, selfPort)); | |
var clusterSeedConfig = ConfigurationFactory.ParseString(string.Format(@"akka.cluster.seed-nodes = [{0}]", string.Join(",", seedNodes))); | |
var finalConfig = clusterSeedConfig.WithFallback(remoteConfig).WithFallback(clusterConfig); | |
return finalConfig; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment