public ScheduleJobResult ScheduleJob <T>(string jobId, string jobGroup, string cronExpression, Dictionary <string, string> jobData = null, string timeZone = null) where T : IJob { var ret = new ScheduleJobResult(); // a. build job JobBuilder jobBuilder = JobBuilder.Create <T>() .WithIdentity(jobId, jobGroup); if (jobData != null) { foreach (KeyValuePair <string, string> pair in jobData) { jobBuilder.UsingJobData(pair.Key, pair.Value); } } IJobDetail job = jobBuilder.Build(); ret.Job = job; // // b. build trigger TimeZoneInfo timeZoneInfo = string.IsNullOrWhiteSpace(timeZone) ? TimeZoneInfo.Local : TimeZoneInfo.FindSystemTimeZoneById(timeZone); TriggerBuilder triggerBuilder = TriggerBuilder.Create() .WithIdentity(jobId, jobGroup) .WithCronSchedule(cronExpression, x => x.InTimeZone(timeZoneInfo)); if (jobData != null) { foreach (KeyValuePair <string, string> pair in jobData) { jobBuilder.UsingJobData(pair.Key, pair.Value); } } ITrigger trigger = triggerBuilder.Build(); ret.Trigger = trigger; // // c. schedule job if (trigger.GetNextFiringTimes(DateTimeOffset.Now).FirstOrDefault() == default(DateTime)) { ret.Success = false; _log.Info($"Job WILL NEVER START for \"{jobId}\""); } else { _scheduler.ScheduleJob(job, trigger).Wait(); ret.Success = true; _log.Debug($"Job scheduled OK for \"{jobId}\""); } return(ret); }
public static void CreateJob <T>(JobInfo jInfo, ref IScheduler scheduler) where T : IJob { JobBuilder jbuilder = JobBuilder.Create <T>(); jbuilder.WithIdentity(jInfo.JobName, jInfo.GroupName); if (jInfo.DataParamters != null && jInfo.DataParamters.Any()) { foreach (var item in jInfo.DataParamters) { jbuilder.UsingJobData(GetDataMap(item)); } } IJobDetail jobDetail = jbuilder.Build(); TriggerBuilder tBuilder = TriggerBuilder.Create(); tBuilder.WithIdentity(jInfo.TriggerName, jInfo.GroupName) .StartNow() .WithCronSchedule(jInfo.CronExpression); //.WithSimpleSchedule(x => x // .WithIntervalInSeconds(10) // .RepeatForever()); ITrigger trigger = tBuilder.Build(); scheduler.ScheduleJob(jobDetail, trigger); }
public static JobBuilder UsingStaticJobData(this JobBuilder source, Action <JobData> jobDataAction) { var map = new JobDataMap(); map.SetStaticJobData(jobDataAction); source.UsingJobData(map); return(source); }
/// <summary> /// Extension method for <see cref="JobBuilder"/> to ensure that the current tenant is being set in the job data. /// </summary> /// <param name="builder">The job builder.</param> /// <returns>The job builder.</returns> public static JobBuilder UsingCurrentTenant(this JobBuilder builder) { var context = RequestContext.GetContext(); if (context == null || context.Tenant == null || context.Tenant.Id < 0) { return(builder); } return(builder.UsingJobData(TenantIdContextKey, context.Tenant.Id.ToString(CultureInfo.InvariantCulture))); }
public void RunNow <T>(string jobId, Dictionary <string, string> jobData = null) where T : IJob { string uid = Guid.NewGuid().ToString(); // a. build job JobBuilder jobBuilder = JobBuilder.Create <T>() .WithIdentity($"{jobId}_{uid}", "OneTimeJobs"); if (jobData != null) { foreach (KeyValuePair <string, string> pair in jobData) { jobBuilder.UsingJobData(pair.Key, pair.Value); } } IJobDetail job = jobBuilder.Build(); // // b. build trigger TriggerBuilder triggerBuilder = TriggerBuilder.Create() .WithIdentity($"{jobId}_{uid}", "OneTimeTriggers") .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).WithRepeatCount(1)); if (jobData != null) { foreach (KeyValuePair <string, string> pair in jobData) { jobBuilder.UsingJobData(pair.Key, pair.Value); } } ITrigger trigger = triggerBuilder.Build(); // // c. schedule job _scheduler.ScheduleJob(job, trigger).Wait(); }
/// <summary> /// 添加定时任务 /// </summary> /// <param name="intervalTs">间隔时间</param> /// <param name="startAt">第一次执行时间</param> /// <param name="repeatCount">重复次数,不填或者为0:不限次</param> public static void AddJobExt <T>(this IScheduler scheduler, TimeSpan intervalTs, DateTime?startAt = null, int?repeatCount = null, IDictionary <string, object> jobDataDic = null) where T : IJob { string jobName = typeof(T).Name; string groupName = jobName + "Group"; string triggerName = jobName + "Trigger"; JobBuilder jobBuilder = JobBuilder.Create <T>().WithIdentity(jobName, groupName); if (jobDataDic != null) { jobBuilder.UsingJobData(new JobDataMap(jobDataDic)); } IJobDetail job = jobBuilder.Build(); AddJob(scheduler, intervalTs, startAt, repeatCount, groupName, triggerName, job); }
public ScheduleJobResult ScheduleJob <T>(string jobId, string jobGroup, ITrigger trigger, Dictionary <string, string> jobData = null) where T : IJob { var ret = new ScheduleJobResult(); // a. build job JobBuilder jobBuilder = JobBuilder.Create <T>() .WithIdentity(jobId, jobGroup); if (jobData != null) { foreach (KeyValuePair <string, string> pair in jobData) { jobBuilder.UsingJobData(pair.Key, pair.Value); } } IJobDetail job = jobBuilder.Build(); ret.Job = job; // // b. use trigger ret.Trigger = trigger; // // c. schedule job if (trigger.GetNextFiringTimes(DateTimeOffset.Now).FirstOrDefault() == default(DateTime)) { ret.Success = false; _log.Info($"Job WILL NEVER START for \"{jobId}\""); } else { _scheduler.ScheduleJob(job, trigger).Wait(); ret.Success = true; _log.Debug($"Job scheduled OK for \"{jobId}\""); } return(ret); }
/// <summary> /// 添加定时任务 /// </summary> /// <param name="type">类型:必须继承至IJob</param> /// <param name="intervalTs">间隔时间</param> /// <param name="startAt">第一次执行时间</param> /// <param name="repeatCount">重复次数,不填或者为0:不限次</param> public static void AddJobExt(this IScheduler scheduler, Type type, TimeSpan intervalTs, DateTime?startAt = null, int?repeatCount = null, IDictionary <string, object> jobDataDic = null) { if (!typeof(IJob).IsAssignableFrom(type)) { throw new ArgumentException("传入的类型必须是IJob的派生类!"); } string jobName = type.Name; string groupName = jobName + "Group"; string triggerName = jobName + "Trigger"; JobBuilder jobBuilder = JobBuilder.Create(type).WithIdentity(jobName, groupName); if (jobDataDic != null) { jobBuilder.UsingJobData(new JobDataMap(jobDataDic)); } IJobDetail job = jobBuilder.Build(); AddJob(scheduler, intervalTs, startAt, repeatCount, groupName, triggerName, job); }
static async Task <IJobDetail> CreateJobDetail(ConsumeContext context, Uri destination, JobKey jobKey, Guid?tokenId = default(Guid?)) { string body; using (var ms = new MemoryStream()) { using (Stream bodyStream = context.ReceiveContext.GetBody()) { await bodyStream.CopyToAsync(ms).ConfigureAwait(false); } body = Encoding.UTF8.GetString(ms.ToArray()); } if (string.Compare(context.ReceiveContext.ContentType.MediaType, JsonMessageSerializer.JsonContentType.MediaType, StringComparison.OrdinalIgnoreCase) == 0) { body = TranslateJsonBody(body, destination.ToString()); } else if (string.Compare(context.ReceiveContext.ContentType.MediaType, XmlMessageSerializer.XmlContentType.MediaType, StringComparison.OrdinalIgnoreCase) == 0) { body = TranslateXmlBody(body, destination.ToString()); } else { throw new InvalidOperationException("Only JSON and XML messages can be scheduled"); } JobBuilder builder = JobBuilder.Create <ScheduledMessageJob>() .RequestRecovery(true) .WithIdentity(jobKey) .UsingJobData("Destination", ToString(destination)) .UsingJobData("ResponseAddress", ToString(context.ResponseAddress)) .UsingJobData("FaultAddress", ToString(context.FaultAddress)) .UsingJobData("Body", body) .UsingJobData("ContentType", context.ReceiveContext.ContentType.MediaType); if (context.MessageId.HasValue) { builder = builder.UsingJobData("MessageId", context.MessageId.Value.ToString()); } if (context.CorrelationId.HasValue) { builder = builder.UsingJobData("CorrelationId", context.CorrelationId.Value.ToString()); } if (context.ConversationId.HasValue) { builder = builder.UsingJobData("ConversationId", context.ConversationId.Value.ToString()); } if (context.InitiatorId.HasValue) { builder = builder.UsingJobData("InitiatorId", context.InitiatorId.Value.ToString()); } if (context.RequestId.HasValue) { builder = builder.UsingJobData("RequestId", context.RequestId.Value.ToString()); } if (context.ExpirationTime.HasValue) { builder = builder.UsingJobData("ExpirationTime", context.ExpirationTime.Value.ToString()); } if (tokenId.HasValue) { builder = builder.UsingJobData("TokenId", tokenId.Value.ToString("N")); } IJobDetail jobDetail = builder .UsingJobData("HeadersAsJson", JsonConvert.SerializeObject(context.Headers.GetAll())) .Build(); return(jobDetail); }
public static JobBuilder WithRunUntil(this JobBuilder jobBuilder, DateTime endDateTime) { jobBuilder.UsingJobData(RunUntil, endDateTime.ToString("yyyy-MM-ddTHH:mm:ss.fff"));; return(jobBuilder); }
public static JobBuilder UsingScheduledItemJobData(this JobBuilder jobBuilder, ScheduledItem scheduledItem) { return (jobBuilder.UsingJobData(ScheduledItemHelper.ScheduledItemContextKey, scheduledItem.Id.ToString())); }