public bool AddHttpJob(JobParamHttpModel job)
        {
            try
            {
                JobKey jobKey = CreateJobKey(job.JobName, job.JobGroupName);
                var    bl     = _scheduler.CheckExists(jobKey).GetAwaiter().GetResult();
                if (!bl)
                {
                    var jobbuilder = JobBuilder.Create <HttpJob>()
                                     .WithIdentity(jobKey)
                                     .UsingJobData(JobConfig.CallbackUrl, job.CallbackUrl);

                    var param = JobUtils.GetDictToString(job.CallbackParams);
                    if (!string.IsNullOrEmpty(param))
                    {
                        jobbuilder.UsingJobData(JobConfig.CallbackParams, JobUtils.GetDictToString(job.CallbackParams));
                    }

                    var jobDetail = jobbuilder.Build();

                    ITrigger trigger = CreateTrigger(job, jobKey);

                    _scheduler.ScheduleJob(jobDetail, trigger).GetAwaiter().GetResult();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return(false);
        }
        public bool UpdateHttpJob(JobParamHttpModel job)
        {
            try
            {
                JobKey       jobKey  = CreateJobKey(job.JobName, job.JobGroupName);
                var          jobData = _scheduler.GetJobDetail(jobKey).GetAwaiter().GetResult();
                JobDataModel model   = new JobDataModel();
                model.CallbackUrl    = jobData.JobDataMap[JobConfig.CallbackUrl].ToString();
                model.CallbackParams = JobUtils.GetDictFromString(jobData.JobDataMap[JobConfig.CallbackParams].ToString());

                bool isSameParams = IsSameParam(job, model);
                if (isSameParams)
                {
                    return(ModifyJobCron(job));
                }
                else
                {
                    bool bl = DeleteJob(job);
                    if (bl)
                    {
                        return(AddHttpJob(job));
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return(false);
        }
Exemple #3
0
        public bool RunAtOnce(string jobName, string jobGroupName)
        {
            try
            {
                JobKey jobKey = CreateJobKey(jobName, jobGroupName);

                JobDataModel jobData = Xml.XmlJobManage.GetJobDataByKey(jobKey);
                if (jobData == null)
                {
                    return(false);
                }

                JobKey jobKeyOnce = CreateJobKey(jobName, JobConfig.JobGroupNameOnce);

                var type = typeof(HttpJob);

                if (jobData.JobType == JobType.Assembly)
                {
                    string   path     = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, jobData.AssemblyDllName);
                    Assembly assembly = Assembly.LoadFile(path);
                    type = assembly.GetType(jobData.TypeFullName);
                }

                var bl = _scheduler.CheckExists(jobKeyOnce).GetAwaiter().GetResult();
                if (bl)
                {
                    _scheduler.DeleteJob(jobKeyOnce);
                }
                IJobDetail jobDetail = JobBuilder.Create(type)
                                       .WithIdentity(jobKeyOnce)
                                       .UsingJobData(JobConfig.CallbackUrl, jobData.CallbackUrl)
                                       .UsingJobData(JobConfig.CallbackParams, JobUtils.GetDictToString(jobData.CallbackParams))
                                       .Build();

                DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(null, 2);
                ITrigger       trigger   = TriggerBuilder.Create()
                                           .WithIdentity(JobConfig.GetTriggerNameOnce(jobName), JobConfig.TriggerGroupNameOnce)
                                           .StartAt(startTime)
                                           .Build();

                _scheduler.ScheduleJob(jobDetail, trigger).GetAwaiter().GetResult();
                return(true);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return(false);
        }
        public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (jobException != null)
            {
                SchedulerLog.Instance.LogRun(context, LogConfig.ExecuteResultFailed);
                log.Error(jobException);

                StringBuilder sb = new StringBuilder();
                JobUtils.GetErrMessage(jobException, sb);
                SchedulerLog.Instance.LogErr(context, sb.ToString());
            }
            else
            {
                SchedulerLog.Instance.LogRun(context, LogConfig.ExecuteResultSuccess);
            }
            return(JobUtils.CompletedTask);
        }
Exemple #5
0
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                JobDataMap dataMap = context.JobDetail.JobDataMap;

                string callbackUrl = dataMap.GetString(JobConfig.CallbackUrl);

                Dictionary <string, string> param = new Dictionary <string, string>();

                if (dataMap.ContainsKey(JobConfig.CallbackParams))
                {
                    string callbackParams = dataMap.GetString(JobConfig.CallbackParams);
                    param = JobUtils.GetDictFromString(callbackParams);
                }

                var result = JobUtils.GetHttpPost(callbackUrl, param);

                log.Info(result);

                string res = LogConfig.ExecuteResultSuccess;
                if (!string.IsNullOrEmpty(result))
                {
                    res = $"{res} {result}";
                }

                SchedulerLog.Instance.LogRun(context, res);

                if (context.JobDetail.Key.Group == JobConfig.JobGroupNameOnce)
                {
                    context.Scheduler.DeleteJob(context.JobDetail.Key).GetAwaiter().GetResult();
                }
            }
            catch (Exception ex)
            {
                SchedulerLog.Instance.LogRun(context, LogConfig.ExecuteResultFailed);

                StringBuilder sb = new StringBuilder();
                JobUtils.GetErrMessage(ex, sb);
                SchedulerLog.Instance.LogErr(context, sb.ToString());

                log.Error(ex);
            }

            return(Task.FromResult(true));
        }
        /// <summary>
        /// 参数是否相同
        /// </summary>
        /// <returns></returns>
        protected bool IsSameParam(JobParamHttpModel job, JobDataModel data)
        {
            string map = JobUtils.GetDictToString(data.CallbackParams);

            string newParam = JobUtils.GetDictToString(job.CallbackParams);

            if (data.CallbackUrl != job.CallbackUrl)
            {
                return(false);
            }

            if (map.ToString() != newParam)
            {
                return(false);
            }

            return(true);
        }
        public List <JobViewModel> GetJobList()
        {
            List <JobViewModel> list = new List <JobViewModel>();

            try
            {
                var jobs = _scheduler.GetJobKeys(GroupMatcher <JobKey> .AnyGroup()).GetAwaiter().GetResult().ToList();
                foreach (var item in jobs)
                {
                    var jobDetail = _scheduler.GetJobDetail(item).GetAwaiter().GetResult();

                    JobViewModel model       = new JobViewModel();
                    var          jobTriggers = _scheduler.GetTriggersOfJob(item).GetAwaiter().GetResult().ToList();
                    if (jobTriggers.Count > 0)
                    {
                        var tr = jobTriggers.First();
                        if (tr is CronTriggerImpl)
                        {
                            var cron = tr as CronTriggerImpl;
                            model.CronExpression = cron.CronExpressionString;
                        }

                        var state = JobUtils.GetTriggerState(tr.Key.Name, tr.Key.Group);
                        model.JobState = JobUtils.GetTriggerStateValue(state);
                    }

                    model.JobType      = jobDetail.JobType.ToString();
                    model.JobData      = JobUtils.GetJobDataString(jobDetail.JobDataMap);
                    model.JobGroupName = jobDetail.Key.Group;
                    model.JobName      = jobDetail.Key.Name;


                    list.Add(model);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return(list);
        }