public LoadBalancerModule(IAsimovConfig config, ITaskExecutor taskExecutor) { _config = config; _taskExecutor = taskExecutor; Get["/loadbalancer/listHosts"] = _ => { if (_config.LoadBalancer.Password == null) return Response.AsJson(new object[] { }); using (var loadBalancer = new AlteonLoadBalancer(_config.LoadBalancer)) { loadBalancer.Login(); return Response.AsJson(loadBalancer.GetHostList()); } }; Post["/loadbalancer/change"] = _ => { var command = this.Bind<ChangeLoadBalancerStateCommand>(); _taskExecutor.AddTask(new ChangeLoadBalancerStates(command)); return "OK"; }; Post["/loadbalancer/settings"] = _ => { var command = this.Bind<UpdateLoadBalancerSettingsCommand>(); _config.LoadBalancer.Password = command.password; if (!string.IsNullOrEmpty(command.host)) _config.LoadBalancer.Host = command.host; return "OK"; }; }
protected override void Execute() { using (var loadBalancer = new AlteonLoadBalancer(Config.LoadBalancer)) { loadBalancer.Login(); foreach (var host in _command.hosts) { switch (host.action) { case "enable": loadBalancer.EnableHostById(host.Id); NodeFront.Notify(new HostLoadBalancerStateChanged(host.Id, true)); Log.InfoFormat("Performing loadbalancer action {0} : {1}", host.action, host.Id); break; case "disable": loadBalancer.DisableHostById(host.Id); NodeFront.Notify(new HostLoadBalancerStateChanged(host.Id, false)); Log.InfoFormat("Performing loadbalancer action {0} : {1}", host.action, host.Id); break; } } } }