Пример #1
0
        public void Enqueue_Multi_Task()
        {
            var chrono = new System.Diagnostics.Stopwatch();

            chrono.Start();

            TasksHost.Enqueue <LongTask>();
            TasksHost.Enqueue <LongTask>();
            TasksHost.Enqueue <LongTask>();
            TasksHost.Enqueue <LongTask>();
            TasksHost.Enqueue <LongTask>();
            TasksHost.Enqueue <LongTask>();
            TasksHost.Enqueue <LongTask>();
            TasksHost.Enqueue <LongTask>();
            TasksHost.Enqueue <LongTask>();
            TasksHost.Enqueue <LongTask>();

            while (true)
            {
                if (!TasksHost.IsRunning())
                {
                    break;
                }
                System.Threading.Thread.Sleep(1000);
            }

            chrono.Stop();

            Check.That(chrono.ElapsedMilliseconds).IsLessThan(10 * 10 * 1000);
        }
Пример #2
0
        public void Task_Is_Running()
        {
            var id  = Guid.NewGuid();
            var mre = new ManualResetEvent(false);

            TasksHost.Enqueue <LongTask>(id, completed: (dic) => mre.Set());
            System.Threading.Thread.Sleep(1 * 1000);
            var result = TasksHost.IsRunning(id);

            mre.WaitOne();
            Check.That(result).IsTrue();
        }
Пример #3
0
        public void Scheduled_Task_Is_Running()
        {
            var id   = Guid.NewGuid();
            var task = Scheduler.CreateScheduledTask <LongTask>("runningTask")
                       .EverySecond(10);

            Scheduler.Add(task);
            System.Threading.Thread.Sleep(1 * 1000);
            var result = TasksHost.IsRunning("runningTask");

            System.Threading.Thread.Sleep(10 * 1000);
            Check.That(result).IsTrue();
        }
Пример #4
0
 public static void Remove(string taskName)
 {
     lock (Current.ScheduledTaskList.SyncRoot)
     {
         var task = Current.ScheduledTaskList.SingleOrDefault(i => i.Name.Equals(taskName, StringComparison.InvariantCultureIgnoreCase));
         if (task != null)
         {
             if (TasksHost.IsRunning(task.Name))
             {
                 var h = TasksHost.GetHistory(task.Name).LastOrDefault();
                 if (h != null && h.Context != null)
                 {
                     h.Context.IsCancelRequested = true;
                 }
             }
             Current.ScheduledTaskList.Remove(task);
         }
     }
 }
Пример #5
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);
        }