/// <summary>
        /// Schedule a news mail
        /// </summary>
        /// <param name="NewsId"></param>
        /// <param name="callbackDelay"></param>
        /// <param name="CreationDate"></param>
        /// <param name="ExpectedExecutionDate"></param>
        /// <returns></returns>
        public bool ScheduleNews(int NewsId, TimeSpan callbackDelay, DateTime?CreationDate = null, DateTime?ExpectedExecutionDate = null)
        {
            bool Result = false;

            try
            {
                if (CreationDate == null)
                {
                    CreationDate          = DateTime.UtcNow;
                    ExpectedExecutionDate = CreationDate.Value.Add(callbackDelay);
                }


                Dictionary <string, object> Parameters = new Dictionary <string, object>();
                Parameters.Add("NewsId", NewsId);
                TaskScheduleResult ResultSchedule = TaskHelper.ScheduleTask(JobBuilder.Create <SendNews>(), callbackDelay, Parameters);

                if (ResultSchedule != null && ResultSchedule.Result)
                {
                    ScheduledTask Task = new ScheduledTask();
                    Task.NewsId                = NewsId;
                    Task.CreationDate          = CreationDate.Value;
                    Task.ExpectedExecutionDate = ExpectedExecutionDate.Value;
                    Task.CallbackId            = ResultSchedule.Id;
                    Task.GroupName             = ResultSchedule.GroupName;
                    Result = InsertScheduledTask(Task);
                }
            }
            catch (Exception e)
            {
                Result = false;
                Commons.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "NewsId = " + NewsId);
            }
            return(Result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Schedule a recurring Task
        /// </summary>
        /// <param name="Task"></param>
        /// <param name="CallBackDelay"></param>
        /// <param name="Parameters"></param>
        /// <returns></returns>
        public static TaskScheduleResult ScheduleRecurringTask(JobBuilder Task, string CronSchedule, Dictionary <string, object> Parameters = null)
        {
            TaskScheduleResult Result = new TaskScheduleResult();
            Guid Id = Guid.NewGuid();

            IJobDetail job     = Task.WithIdentity(Id.ToString(), Task.Build().JobType.Name).Build();
            bool       Success = false;

            try
            {
                IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
                scheduler.Start();

                string Group = job.JobType.Name;

                JobKey Key = new JobKey(Id.ToString(), Group);


                TriggerBuilder triggerBuilder = TriggerBuilder.Create().WithIdentity(Id.ToString(), Group).WithCronSchedule(
                    CronSchedule,
                    x => x.InTimeZone(TimeZoneInfo.Utc)
                    );

                if (Parameters != null)
                {
                    foreach (var Param in Parameters)
                    {
                        triggerBuilder = triggerBuilder.UsingJobData(Param.Key, Param.Value.ToString());
                    }
                }
                ITrigger trigger = triggerBuilder.ForJob(job).Build();


                var DatetimeOffset = scheduler.ScheduleJob(job, trigger);
                Success          = DatetimeOffset != null ? true : false;
                Result.GroupName = Group;
                Result.Id        = Id.ToString();
            }
            catch (Exception e)
            {
                Success = false;
                Commons.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "job = " + job.JobType.Name);
            }
            Result.Result = Success;
            return(Result);
        }