Esempio n. 1
0
        public async Task Execute(IJobExecutionContext context)
        {
            JobDataMap data = context.JobDetail.JobDataMap;

            TaskBuss taskBuss = new TaskBuss();
            var      list     = taskBuss.GetJobsIds(
                data["taskDb"].ToString(),
                data["taskTable"].ToString(),
                data["taskKey"].ToString(),
                data["taskState"].ToString(),
                data["taskStateFrom"].ToString(),
                data["taskStateTo"].ToString()
                );

            var redis = RedisManager.getRedisConn();
            var db    = redis.GetDatabase(Global.REDIS_DB);

            foreach (string id in list)
            {
                await db.ListLeftPushAsync(Global.TASK_PREFIX + "." + data["taskCode"], id);
            }

            if (list.Count > 0)
            {
                await Global.Topic(data["taskCode"].ToString());

                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "> " + "任务(" + data["taskCode"] + ")部署了" + list.Count + "个Job");
            }
        }
Esempio n. 2
0
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                IScheduler sched = await Global.GetScheduler();

                if (!sched.IsStarted)
                {
                    await sched.Start();
                }

                await sched.Clear();

                TaskBuss        taskBuss = new TaskBuss();
                List <TaskItem> list     = taskBuss.InitTask();

                foreach (TaskItem item in list)
                {
                    JobDataMap keyValues = new JobDataMap();
                    Type       t         = item.GetType();
                    foreach (FieldInfo fieldInfo in t.GetFields())
                    {
                        keyValues.Add(fieldInfo.Name, fieldInfo.GetValue(item));
                    }
                    IJobDetail job;
                    if (item.taskType == "0")
                    {
                        job = JobBuilder.Create <TaskDBJob>()
                              .WithIdentity(item.taskCode, "BussDB")
                              .SetJobData(keyValues)
                              .Build();
                    }
                    else
                    {
                        job = JobBuilder.Create <TaskLoopJob>()
                              .WithIdentity(item.taskCode, "BussLoop")
                              .SetJobData(keyValues)
                              .Build();
                    }
                    if (item.triggerType == "0")
                    {
                        ITrigger trigger = TriggerBuilder.Create()
                                           .WithIdentity(item.taskCode + "Trigger", "BussTrig")
                                           .StartNow()
                                           .WithSimpleSchedule(x => x
                                                               .WithIntervalInSeconds(item.interval)
                                                               .RepeatForever())
                                           .Build();
                        await sched.ScheduleJob(job, trigger);
                    }
                    else
                    {
                        ICronTrigger cronTrigger = (ICronTrigger)TriggerBuilder.Create()
                                                   .WithCronSchedule(item.cronExpression)
                                                   .Build();
                        await sched.ScheduleJob(job, cronTrigger);
                    }
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "> " + "已启动任务:" + item.taskCode);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "> " + ex.StackTrace);
            }
        }