|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using Akka.Actor; |
| 5 | +using Akka.Configuration; |
| 6 | +using ConfigurationException = Akka.Configuration.ConfigurationException; |
| 7 | + |
| 8 | +namespace Lighthouse |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Launcher for the Lighthouse <see cref="ActorSystem"/> |
| 12 | + /// </summary> |
| 13 | + public static class LighthouseHostFactory |
| 14 | + { |
| 15 | + public static ActorSystem LaunchLighthouse(string ipAddress = null, int? specifiedPort = null, string systemName = null) |
| 16 | + { |
| 17 | + systemName = systemName ?? Environment.GetEnvironmentVariable("ACTORSYSTEM")?.Trim(); |
| 18 | + ipAddress = ipAddress ?? Environment.GetEnvironmentVariable("CLUSTER_IP")?.Trim(); |
| 19 | + if (specifiedPort == null) |
| 20 | + { |
| 21 | + var envPort = Environment.GetEnvironmentVariable("CLUSTER_PORT")?.Trim(); |
| 22 | + if (!string.IsNullOrEmpty(envPort) && int.TryParse(envPort, out var actualPort)) |
| 23 | + { |
| 24 | + specifiedPort = actualPort; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + var clusterConfig = ConfigurationFactory.ParseString(File.ReadAllText("akka.hocon")); |
| 29 | + |
| 30 | + var lighthouseConfig = clusterConfig.GetConfig("lighthouse"); |
| 31 | + if (lighthouseConfig != null && string.IsNullOrEmpty(systemName)) |
| 32 | + { |
| 33 | + systemName = lighthouseConfig.GetString("actorsystem", systemName); |
| 34 | + } |
| 35 | + |
| 36 | + var remoteConfig = clusterConfig.GetConfig("akka.remote"); |
| 37 | + |
| 38 | + if (string.IsNullOrEmpty(ipAddress)) |
| 39 | + { |
| 40 | + ipAddress = remoteConfig.GetString("dot-netty.tcp.public-hostname") ?? |
| 41 | + "127.0.0.1"; //localhost as a final default |
| 42 | + } |
| 43 | + |
| 44 | + int port = specifiedPort ?? remoteConfig.GetInt("dot-netty.tcp.port"); |
| 45 | + |
| 46 | + if (port == 0) throw new ConfigurationException("Need to specify an explicit port for Lighthouse. Found an undefined port or a port value of 0 in App.config."); |
| 47 | + |
| 48 | + var selfAddress = $"akka.tcp://{systemName}@{ipAddress}:{port}"; |
| 49 | + |
| 50 | + /* |
| 51 | + * Sanity check |
| 52 | + */ |
| 53 | + Console.WriteLine($"[Lighthouse] ActorSystem: {systemName}; IP: {ipAddress}; PORT: {port}"); |
| 54 | + Console.WriteLine("[Lighthouse] Performing pre-boot sanity check. Should be able to parse address [{0}]", selfAddress); |
| 55 | + selfAddress = new Address("akka.tcp", systemName, ipAddress.Trim(), port).ToString(); |
| 56 | + Console.WriteLine("[Lighthouse] Parse successful."); |
| 57 | + |
| 58 | + var clusterSeeds = Environment.GetEnvironmentVariable("CLUSTER_SEEDS")?.Trim(); |
| 59 | + |
| 60 | + var seeds = clusterConfig.GetStringList("akka.cluster.seed-nodes"); |
| 61 | + if (!string.IsNullOrEmpty(clusterSeeds)) |
| 62 | + { |
| 63 | + var tempSeeds = clusterSeeds.Trim('[', ']').Split(','); |
| 64 | + if (tempSeeds.Any()) |
| 65 | + { |
| 66 | + seeds = tempSeeds; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + if (!seeds.Contains(selfAddress)) |
| 72 | + { |
| 73 | + seeds.Add(selfAddress); |
| 74 | + } |
| 75 | + |
| 76 | + var injectedClusterConfigString = seeds.Aggregate("akka.cluster.seed-nodes = [", (current, seed) => current + (@"""" + seed + @""", ")); |
| 77 | + injectedClusterConfigString += "]"; |
| 78 | + |
| 79 | + var finalConfig = ConfigurationFactory.ParseString( |
| 80 | + string.Format(@"akka.remote.dot-netty.tcp.public-hostname = {0} |
| 81 | +akka.remote.dot-netty.tcp.port = {1}", ipAddress, port)) |
| 82 | + .WithFallback(ConfigurationFactory.ParseString(injectedClusterConfigString)) |
| 83 | + .WithFallback(clusterConfig); |
| 84 | + |
| 85 | + return ActorSystem.Create(systemName, finalConfig); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments