Exemple #1
0
        private static void Test()
        {
            var nodes = ConfigurationManager.GetMySqlClusterSettings();

            for (int i = 0; i < nodes.Nodes.Count; i++)
            {
                string configs = nodes.Nodes[i].AutoMoveDataSet;
                List <AutoMoveDataConfig> ls = AutoMoveDataConfig.Parse(configs);
                Console.WriteLine(String.Format("节点:{1},ID:{2},共{0}个配置表", ls.Count, nodes.Nodes[i].DataBasesName, nodes.Nodes[i].ID));
                for (int r = 0; r < ls.Count; r++)
                {
                    Console.WriteLine(String.Format("表{0}:{1},数据保鲜期:{2}天,时间标识列:{3},任务计划时间:{4},归档节点ID:{5}", r + 1, ls[r].TableName, ls[r].DataHoldDays, ls[r].DateField, ls[r].ScheduleTime, ls[r].ArchiveNodeID));
                }
            }
        }
Exemple #2
0
        private static void Status()
        {
            Console.WriteLine("work path:");
            Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
            Console.WriteLine(AppDomain.CurrentDomain.DynamicDirectory);
            Console.WriteLine("log path:");
            string logpath = ConfigurationManager.GetAppConfig("LogManPath");

            Console.WriteLine(String.Format("{0},existed? {1}", logpath, Directory.Exists(logpath)));
            Console.WriteLine("");
            var nodes = ConfigurationManager.GetMySqlClusterSettings();

            for (int i = 0; i < nodes.Nodes.Count; i++)
            {
                string configs = nodes.Nodes[i].AutoMoveDataSet;
                List <AutoMoveDataConfig> ls = AutoMoveDataConfig.Parse(configs);
                Console.WriteLine(String.Format("节点:{1},ID:{2},共{0}个配置表", ls.Count, nodes.Nodes[i].DataBasesName, nodes.Nodes[i].ID));
                for (int r = 0; r < ls.Count; r++)
                {
                    Console.WriteLine(String.Format("表{0}:{1},数据保鲜期:{2}天,时间标识列:{3},任务计划时间:{4},归档节点ID:{5}", r + 1, ls[r].TableName, ls[r].DataHoldDays, ls[r].DateField, ls[r].ScheduleTime, ls[r].ArchiveNodeID));
                }
            }

            Console.WriteLine("");
            Console.WriteLine("任务计划如下:");
            if (service == null)
            {
                var s = AppDomain.CurrentDomain.GetData("service");
                if (s != null)
                {
                    service = (IDataNiuKnifeService <AppSettings>)s;
                }
            }

            if (service != null)
            {
                for (int i = 0; i < service.JobList.Count; i++)
                {
                    Console.WriteLine(service.JobList[i].Description);
                }
            }
            else
            {
                Console.WriteLine("there is no job service instances.");
            }
        }
        /// <summary>
        /// 启动服务
        /// </summary>
        private async void StartServices(CancellationToken stoppingToken = default(CancellationToken))
        {
            //初始化过程……
            //按每个节点配置Job
            //1.创建Schedule
            m_Scheduler = await StdSchedulerFactory.GetDefaultScheduler();

            Loger.Info(this.GetType(), "开始初始化服务……");

            try
            {
                //遍历所有节点,解析带有数据分割配置的节点创建job队列
                if (m_MySqlClusterSettings != null && m_MySqlClusterSettings.Nodes.Count > 0)
                {
                    foreach (var node in m_MySqlClusterSettings.Nodes)
                    {
                        if (!String.IsNullOrEmpty(node.AutoMoveDataSet))
                        {
                            List <AutoMoveDataConfig> ls = AutoMoveDataConfig.Parse(node.AutoMoveDataSet);
                            if (ls != null && ls.Count > 0)
                            {
                                //m_ConfiguredDataNode.Add(node, ls);
                                for (int k = 0; k < ls.Count; k++)
                                {
                                    //2.创建job (具体的job需要单独在一个文件中执行)
                                    var job = JobBuilder.Create <MysqlDataWorker>()
                                              .UsingJobData("conn_source_str", node.ConnStr)
                                              .UsingJobData("conn_dest_str", m_MySqlClusterSettings.Nodes.FindLast(o => o.ID.Equals(ls[k].ArchiveNodeID)).ConnStr)
                                              .UsingJobData("table_name", ls[k].TableName)
                                              .UsingJobData("key_name", ls[k].KeyName)
                                              .UsingJobData("data_hold_days", ls[k].DataHoldDays)
                                              .UsingJobData("date_field", ls[k].DateField)
                                              .WithIdentity("job_" + k, "g_" + node.DataBasesName)
                                              .WithDescription(String.Format("自动分割数据表:{0},每天在{1}执行一次", ls[0].TableName, ls[k].ScheduleTime))
                                              .StoreDurably(true)
                                              .Build();

                                    m_JobList.Add(job);

                                    //3.创建触发器
                                    TimeOfDay timeOfDay = new TimeOfDay(ls[k].ScheduleTime.Hours, ls[k].ScheduleTime.Minutes, ls[k].ScheduleTime.Seconds);
                                    ITrigger  trigger   = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(x => x.OnEveryDay().StartingDailyAt(timeOfDay).EndingDailyAfterCount(1)).Build();
                                    //ITrigger trigger_test = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(x => x.OnEveryDay().WithInterval(1, IntervalUnit.Minute)).Build();
                                    await m_Scheduler.ScheduleJob(job, trigger);

                                    //await m_Scheduler.ScheduleJob(job, trigger_test);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Loger.Error(this.GetType(), "初始化服务失败," + ex.Message, ex);
            }

            StringBuilder sb = new StringBuilder();

            if (m_JobList.Count > 0)
            {
                sb.AppendLine("开始初始化服务结束");
                for (int i = 0; i < m_JobList.Count; i++)
                {
                    sb.AppendLine(m_JobList[i].Description);
                }

                Loger.Info(this.GetType(), sb.ToString());
                await m_Scheduler.Start(stoppingToken);
            }

            AppDomain.CurrentDomain.SetData("service", this);
        }
        /// <summary>
        /// 从字符串中解析配置
        /// </summary>
        /// <param name="config">例:table_name=t1,key_name=key1,date_field=created,data_hold_days=30,archive_node_id=5,schedule_time=23:00:00:00;table_name=t2,key_name=key2,date_field=created,data_hold_days=30,archive_node_id=5,schedule_time=23:30:00:00;</param>
        /// <returns></returns>
        public static List <AutoMoveDataConfig> Parse(string config)
        {
            List <AutoMoveDataConfig> list = new List <AutoMoveDataConfig>();

            if (!String.IsNullOrEmpty(config))
            {
                string[] cfigs_1 = config.Split(";", StringSplitOptions.RemoveEmptyEntries);//第1层循环 ;
                if (cfigs_1.Length > 0)
                {
                    for (int i = 0; i < cfigs_1.Length; i++)
                    {
                        AutoMoveDataConfig cfg     = new AutoMoveDataConfig();
                        string[]           cfigs_2 = cfigs_1[i].Split(",", StringSplitOptions.RemoveEmptyEntries);//第2层循环 ,
                        if (cfigs_2.Length > 0)
                        {
                            for (int m = 0; m < cfigs_2.Length; m++)
                            {
                                string[] cfigs_3 = cfigs_2[m].Split("=", StringSplitOptions.RemoveEmptyEntries);//第3层循环 =
                                if (cfigs_3.Length == 2)
                                {
                                    string name  = cfigs_3[0];
                                    string value = cfigs_3[1];
                                    switch (name.ToLower())
                                    {
                                    case "table_name":
                                        cfg.TableName = value;
                                        break;

                                    case "key_name":
                                        cfg.KeyName = value;
                                        break;

                                    case "date_field":
                                        cfg.DateField = value;
                                        break;

                                    case "data_hold_days":
                                        int dys = 30;
                                        int.TryParse(value, out dys);
                                        cfg.DataHoldDays = dys;
                                        break;

                                    case "archive_node_id":
                                        int nodeid = 1;
                                        int.TryParse(value, out nodeid);
                                        cfg.ArchiveNodeID = nodeid;
                                        break;

                                    case "schedule_time":
                                        string[] schedule_time_fields = value.Split(":", StringSplitOptions.RemoveEmptyEntries);
                                        int      hours = 23;
                                        int      mits  = 59;
                                        int      secs  = 0;
                                        if (schedule_time_fields.Length > 0)
                                        {
                                            int.TryParse(schedule_time_fields[0], out hours);
                                            int.TryParse(schedule_time_fields[1], out mits);
                                            int.TryParse(schedule_time_fields[2], out secs);
                                        }
                                        cfg.ScheduleTime = TimeSpan.FromHours(hours) + TimeSpan.FromMinutes(mits) + TimeSpan.FromSeconds(secs);
                                        break;
                                    }
                                }
                            }
                        }
                        list.Add(cfg);
                    }
                }
            }

            return(list);
        }