public override async Task process(WatchedEvent @event) { if (@event.getPath() == "/nscrapy/conf") { var newConfig = await ZkHelper.GetAsync(@event.getPath()); string configFromZK = $"appsetting.zk.{DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss.ms")}.json"; using (var fs = File.OpenWrite(Path.Combine(Directory.GetCurrentDirectory(), configFromZK))) { var content = Encoding.UTF8.GetBytes(newConfig); fs.Write(content, 0, content.Length); } if (NScrapyContext.CurrentContext != null) { NScrapyContext.CurrentContext.RefreshConfigFile(configFromZK); } } }
public string GetConfigFilePath() { //Copy the config from zookeeper, and return the config name in local var configContent = ZkHelper.GetAsync("/nscrapy/conf").Result; string configFromZK = $"appsetting.zk.{DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss.ms")}.json"; if (string.IsNullOrEmpty(configContent)) { //Return default if no config found in ZK return("appsetting.json"); } using (var fs = File.OpenWrite(Path.Combine(Directory.GetCurrentDirectory(), configFromZK))) { var content = Encoding.UTF8.GetBytes(configContent); fs.Write(content, 0, content.Length); } //Use ZK Get to register the watcher again ZkHelper.GetAsync("/nscrapy/conf"); return(configFromZK); }