Exemplo n.º 1
0
 /// <summary>
 ///  读取配置文件,支持动态配置
 /// </summary>
 private void WorkerScan()
 {
     if (Directory.Exists(confDir))
     {
         try
         {
             var files = Directory.GetFiles(confDir);
             foreach (var file in files)
             {
                 var conf = new WorkerConf();
                 if (conf.LoadFromFile(file))
                 {
                     if (workers.ContainsKey(conf.Name))
                     {
                         Log.Debug($"{conf.Name} already exist ,skip !");
                         continue;
                     }
                     var worker = new TimerWorker(conf);
                     workers.Add(conf.Name, worker);
                     worker.Run();
                 }
             }
         } catch (Exception ex)
         {
             Log.Warn($"Load conf error : {ex.Message} , skip");
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 定期读取配置文件,支持动态配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WorkerScan(object sender, ElapsedEventArgs e)
        {
            workerScannerTimer.Interval = 1000000;
            string confDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SysConst.CONF_DIR);

            if (Directory.Exists(confDir))
            {
                try
                {
                    var files = Directory.GetFiles(confDir);
                    foreach (var file in files)
                    {
                        if (file.TryReadAllText(out string content) &&
                            content.TryDeserializeJsonStr(out WorkerConf conf) &&
                            conf.Validate())
                        {
                            if (workers.ContainsKey(conf.Name))
                            {
                                Log.Debug($"{conf.Name} already exist ,skip !");
                                continue;
                            }
                            var worker = new TimerWorker(conf);
                            workers.Add(conf.Name, worker);
                            worker.Run();
                        }
                    }
                } catch (Exception ex)
                {
                    Log.Warn($"Load conf error : {ex.Message} , skip");
                }
            }
            workerScannerTimer.Interval = workers.Count > 0 ? WORKER_SCANNER_INTERVAL : WORKER_SCANNER_INIT_INTERVAL;
        }
Exemplo n.º 3
0
        private void OnConfCreated(object sender, FileSystemEventArgs e)
        {
            string file = e.FullPath ?? "";

            if (string.IsNullOrWhiteSpace(file))
            {
                return;
            }
            int count = 0;

            while (!file.IsFileReady())
            {
                if (!File.Exists(file))
                {
                    return;
                }
                Thread.Sleep(100);
                if (++count >= 50)
                {
                    return;
                }
            }
            WorkerConf conf = new WorkerConf();

            if (!conf.LoadFromFile(file))
            {
                return;
            }
            if (workers.ContainsKey(conf.Name))
            {
                Log.Debug($"{conf.Name} already exist ,skip !");
                return;
            }
            var worker = new TimerWorker(conf);

            workers.Add(conf.Name, worker);
            worker.Run();
        }