Beispiel #1
0
        /// <summary>
        /// 重置某一定时服务
        /// </summary>
        /// <param name="taskId"></param>
        public void ResetDbTask(string taskId)
        {
            if (DbTimers.ContainsKey(taskId))
            {
                DbTimers[taskId].Dispose();
                DbTimers.Remove(taskId);
            }
            TaskStatus[taskId] = false;

            if (timedTaskProvider != null)
            {
                var task = timedTaskProvider.GetTaskById(taskId);
                if (task != null && task.IsEnabled)
                {
                    if (string.IsNullOrEmpty(task.Identifier))
                    {
                        return;
                    }
                    var clazzName    = task.Identifier.Substring(0, task.Identifier.LastIndexOf('.'));
                    var functionName = task.Identifier.Substring(task.Identifier.LastIndexOf('.') + 1);
                    var clazz        = JobTypeCollection.SingleOrDefault(x => x.FullName == clazzName);
                    if (clazz == null)
                    {
                        return;
                    }
                    var method = clazz.GetMethod(functionName);
                    if (method == null)
                    {
                        return;
                    }

                    //需要延时的时间
                    int delta = 0;
                    if (task.BeginTime == default(DateTime))
                    {
                        task.BeginTime = DateTime.Now;
                    }
                    else
                    {
                        delta = Convert.ToInt32((task.BeginTime - DateTime.Now).TotalMilliseconds);
                    }
                    if (delta < 0)
                    {
                        delta = delta % task.Interval;
                        if (delta < 0)
                        {
                            delta += task.Interval;
                        }
                    }
                    Task.Factory.StartNew(() =>
                    {
                        var timer = new Timer(t =>
                        {
                            Execute(task, clazz, method);
                        }, null, delta, task.AutoReset ? task.Interval : 0);
                        DbTimers.Add(task.Id, timer);
                    });
                }
            }
        }
Beispiel #2
0
        private void ExecuteDbTask()
        {
            //清除所有DB事务
            if (DbTimers.Count > 0)
            {
                foreach (var dbTimer in DbTimers)
                {
                    dbTimer.Value.Dispose();
                    TaskStatus[dbTimer.Key] = false;
                }
                DbTimers.Clear();
            }
            //取得数据库配置的事务
            if (timedTaskProvider != null)
            {
                var tasks = timedTaskProvider.GetTasks();
                foreach (var task in tasks)
                {
                    if (string.IsNullOrEmpty(task.Identifier))
                    {
                        continue;
                    }
                    var clazzName    = task.Identifier.Substring(0, task.Identifier.LastIndexOf('.'));
                    var functionName = task.Identifier.Substring(task.Identifier.LastIndexOf('.') + 1);
                    var clazz        = JobTypeCollection.SingleOrDefault(x => x.FullName == clazzName);
                    var method       = clazz?.GetMethod(functionName);
                    if (method == null)
                    {
                        continue;
                    }

                    //需要延时的时间
                    long delta = 0;
                    if (task.BeginTime == default(DateTime))
                    {
                        task.BeginTime = DateTime.Now;
                    }
                    else
                    {
                        delta = Convert.ToInt64((task.BeginTime - DateTime.Now).TotalMilliseconds);
                    }
                    if (delta < 0)
                    {
                        delta = delta % task.Interval;
                        if (delta < 0)
                        {
                            delta += task.Interval;
                        }
                    }
                    Task.Factory.StartNew(() =>
                    {
                        var deltaSpan    = new TimeSpan(delta * 10000);
                        var IntervalSpan = new TimeSpan((task.AutoReset ? task.Interval : 0) * 10000);
                        var timer        = new Timer(t =>
                        {
                            Execute(task, clazz, method);
                        }, null, deltaSpan, IntervalSpan);
                        DbTimers.Add(task.Id, timer);
                    });
                }
            }
        }