Пример #1
0
        public static void ForceTask(string taskName)
        {
            GlobalConfiguration.Logger.Info("Try to force task {0}", taskName);
            ScheduledTask task = null;

            lock (Current.ScheduledTaskList.SyncRoot)
            {
                task = Current.ScheduledTaskList.FirstOrDefault(i => i.Name == taskName);
                if (task == null)
                {
                    GlobalConfiguration.Logger.Warn("force unknown task {0}", taskName);
                    return;
                }
                task.DelayedStartInMillisecond = 0;
                task.NextRunningDate           = DateTime.MinValue;
                task.IsForced = true;
            }
            if (Current.EventForceTask != null)
            {
                Current.EventForceTask.Set();
                GlobalConfiguration.Logger.Info("Task {0} was forced", taskName);
            }
        }
Пример #2
0
        public static ScheduledTask CreateScheduledTask <T>(string name, Dictionary <string, object> parameters = null)
            where T : class
        {
            if (!typeof(T).GetInterfaces().Contains(typeof(ITask)))
            {
                throw new Exception("task must implements ITask");
            }
            var task = new ScheduledTask();

            task.Name                  = name;
            task.Enabled               = !GlobalConfiguration.Settings.ScheduledTaskDisabledByDefault;
            task.TaskType              = typeof(T);
            task.NextRunningDate       = DateTime.MinValue;
            task.CreationDate          = DateTime.Now;
            task.StartedCount          = 0;
            task.Enabled               = true;
            task.AllowMultipleInstance = true;
            if (parameters != null)
            {
                task.Parameters = parameters;
            }

            return(task);
        }
Пример #3
0
        internal virtual void ProcessTask(ScheduledTask scheduledTask)
        {
            if (scheduledTask.IsQueued &&
                !scheduledTask.AllowMultipleInstance)
            {
                GlobalConfiguration.Logger.Warn("scheduled task {0} is in queue and not allow multiple instance", scheduledTask.Name);
                return;
            }

            if (!scheduledTask.AllowMultipleInstance)
            {
                if (TasksHost.IsRunning(scheduledTask.Name))
                {
                    GlobalConfiguration.Logger.Warn("scheduled task {0} is already running and not allow multiple instance", scheduledTask.Name);
                    return;
                }
            }

            var id = Guid.NewGuid();

            scheduledTask.IsQueued = true;

            TasksHost.Enqueue(
                id
                , scheduledTask.Name
                , scheduledTask.TaskType
                , scheduledTask.Parameters
                , (dic) =>
            {
                GlobalConfiguration.Logger.Info("scheduled task {0} completed", scheduledTask.Name);
                scheduledTask.IsQueued = false;
                try
                {
                    if (scheduledTask.Completed != null)
                    {
                        scheduledTask.Completed(dic);
                    }
                }
                catch (Exception ex)
                {
                    GlobalConfiguration.Logger.Error(ex);
                }
                finally
                {
                    scheduledTask.IsForced = false;
                    if (scheduledTask.NextRunningDateFactory != null)
                    {
                        try
                        {
                            scheduledTask.NextRunningDate = scheduledTask.NextRunningDateFactory.Invoke();
                        }
                        catch (Exception ex)
                        {
                            GlobalConfiguration.Logger.Error(ex);
                        }
                    }
                }
            }
                , (ex) =>
            {
                scheduledTask.Exception = ex;
                GlobalConfiguration.Logger.Error(ex);
                if (TaskFailed != null)
                {
                    try
                    {
                        TaskFailed.Invoke(scheduledTask.Name, ex);
                    }
                    catch { }
                }
            },
                null,
                () =>
            {
                GlobalConfiguration.Logger.Info("scheduled task {0} started", scheduledTask.Name);
                scheduledTask.StartedCount += 1;
                if (TaskStarted != null)
                {
                    try
                    {
                        TaskStarted.Invoke(scheduledTask.Name);
                    }
                    catch { }
                }
            },
                true,
                scheduledTask.IsForced);
        }
Пример #4
0
 public static ScheduledTask AllowMultipleInstance(this ScheduledTask task, bool allow)
 {
     task.AllowMultipleInstance = allow;
     return(task);
 }
Пример #5
0
 public static ScheduledTask StartWithDelay(this ScheduledTask task, int delayInSeconds = 1)
 {
     task.DelayedStartInMillisecond = delayInSeconds;
     return(task);
 }
Пример #6
0
 public static ScheduledTask EverySecond(this ScheduledTask task, int second = 1)
 {
     task.Period   = ScheduledTaskTimePeriod.Second;
     task.Interval = second;
     return(task);
 }
Пример #7
0
 public static ScheduledTask EveryMinute(this ScheduledTask task, int minutes = 1)
 {
     task.Period   = ScheduledTaskTimePeriod.Minute;
     task.Interval = minutes;
     return(task);
 }
Пример #8
0
 public static ScheduledTask EveryHour(this ScheduledTask task, int hours = 1)
 {
     task.Period   = ScheduledTaskTimePeriod.Hour;
     task.Interval = hours;
     return(task);
 }
Пример #9
0
 public static ScheduledTask EveryWorkingDay(this ScheduledTask task)
 {
     task.Period   = ScheduledTaskTimePeriod.WorkingDay;
     task.Interval = 1;
     return(task);
 }
Пример #10
0
 public static ScheduledTask EveryDay(this ScheduledTask task, int days = 1)
 {
     task.Period   = ScheduledTaskTimePeriod.Day;
     task.Interval = days;
     return(task);
 }