/// <summary>
        /// Set task as completed
        /// </summary>
        public BaseResponse LogEndTaskExecution(SchedulerTaskType type)
        {
            SchedulerQueue schedulerQueue = Uow.SchedulerQueues.GetLastByType(type, SchedulerDestination);

            if (schedulerQueue != null)
            {
                Uow.SchedulerQueues.FinishAndChangeStatus(schedulerQueue, SchedulerExecutionStatus.Success);

                if (schedulerQueue.Dependencies != null)
                {
                    foreach (SchedulerTaskType schedulerTaskType in schedulerQueue.Dependencies)
                    {
                        SchedulerQueue item = Uow.SchedulerQueues.GetLastByType(schedulerTaskType, SchedulerDestination);
                        if (item != null)
                        {
                            long changedLockerMask = item.LockerMask & (~schedulerQueue.TaskType.GetLockerMask());
                            Uow.SchedulerQueues.ChangeLockerMask(item, changedLockerMask);
                        }
                    }
                }

                return(BaseResponse.Success());
            }

            return(BaseResponse.Error(ErrorCode.SchedulerQueueItemNotFound));
        }
            public IDisposable Schedule(TimeSpan dueTime, Action action)
            {
                if (action == null)
                {
                    throw new ArgumentNullException("action");
                }
                TimeSpan       dueTime2       = Scheduler.CurrentThreadScheduler.Time + Scheduler.Normalize(dueTime);
                ScheduledItem  scheduledItem  = new ScheduledItem(action, dueTime2);
                SchedulerQueue schedulerQueue = Scheduler.CurrentThreadScheduler.GetQueue();

                if (schedulerQueue == null)
                {
                    schedulerQueue = new SchedulerQueue(4);
                    schedulerQueue.Enqueue(scheduledItem);
                    Scheduler.CurrentThreadScheduler.SetQueue(schedulerQueue);
                    try
                    {
                        Scheduler.CurrentThreadScheduler.Trampoline.Run(schedulerQueue);
                    }
                    finally
                    {
                        Scheduler.CurrentThreadScheduler.SetQueue(null);
                    }
                }
                else
                {
                    schedulerQueue.Enqueue(scheduledItem);
                }
                return(scheduledItem.Cancellation);
            }
Example #3
0
            public IDisposable Schedule(TimeSpan dueTime, Action action)
            {
                if (action == null)
                {
                    throw new ArgumentNullException("action");
                }
                TimeSpan       dueTime2      = Time + Normalize(dueTime);
                ScheduledItem  scheduledItem = new ScheduledItem(action, dueTime2);
                SchedulerQueue queue         = GetQueue();

                if (queue == null)
                {
                    queue = new SchedulerQueue(4);
                    queue.Enqueue(scheduledItem);
                    SetQueue(queue);
                    try
                    {
                        Trampoline.Run(queue);
                    }
                    finally
                    {
                        SetQueue(null);
                    }
                }
                else
                {
                    queue.Enqueue(scheduledItem);
                }
                return(scheduledItem.Cancellation);
            }
            public IDisposable Schedule(TimeSpan dueTime, Action action)
            {
                if (action == null)
                    throw new ArgumentNullException("action");

                var dt = Time + Scheduler.Normalize(dueTime);

                var si = new ScheduledItem(action, dt);

                var queue = GetQueue();

                if (queue == null)
                {
                    queue = new SchedulerQueue(4);
                    queue.Enqueue(si);

                    CurrentThreadScheduler.SetQueue(queue);
                    try
                    {
                        Trampoline.Run(queue);
                    }
                    finally
                    {
                        CurrentThreadScheduler.SetQueue(null);
                    }
                }
                else
                {
                    queue.Enqueue(si);
                }

                return si.Cancellation;
            }
            public IDisposable Schedule(TimeSpan dueTime, Action action)
            {
                if (action == null)
                {
                    throw new ArgumentNullException("action");
                }

                var dt = Time + Scheduler.Normalize(dueTime);

                var si = new ScheduledItem(action, dt);

                var queue = GetQueue();

                if (queue == null)
                {
                    queue = new SchedulerQueue(4);
                    queue.Enqueue(si);

                    CurrentThreadScheduler.SetQueue(queue);

                    try {
                        Trampoline.Run(queue);
                    } finally {
                        CurrentThreadScheduler.SetQueue(null);
                    }
                }
                else
                {
                    queue.Enqueue(si);
                }

                return(si.Cancellation);
            }
Example #6
0
 public SingleActionScheduler(TaskScheduler exclusiveScheduler = null)
 {
     if (exclusiveScheduler == null)
     {
         exclusiveScheduler = new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler;
     }
     ExclusiveScheduler = exclusiveScheduler;
     _sQueue            = new SchedulerQueue <TimeSpan>();
     DeQueueLoop        = DequeueLoopRunner();
     DeQueueLoop.Start(ExclusiveScheduler);
 }
        internal SchedulerQueue CreateSchedulerQueue()
        {
            SchedulerQueue result = new SchedulerQueue
            {
                PlannedDate  = GetCronTime(),
                TaskType     = SchedulerTask.Type,
                Dependencies = SchedulerTask.Dependencies,
                LockerMask   = SchedulerTask.LockerMask
            };

            return(result);
        }
        /// <summary>
        /// Set task as executing
        /// </summary>
        public BaseResponse LogStartTaskExecution(SchedulerTaskType type)
        {
            SchedulerQueue schedulerQueue = Uow.SchedulerQueues.GetLastByType(type, SchedulerDestination);

            if (schedulerQueue != null)
            {
                Uow.SchedulerQueues.StartAndChangeStatus(schedulerQueue, SchedulerExecutionStatus.Executing);

                return(BaseResponse.Success());
            }

            return(BaseResponse.Error(ErrorCode.SchedulerQueueItemNotFound));
        }
        /// <summary>
        /// Set task as completed with exception
        /// </summary>
        public BaseResponse LogTaskException(SchedulerTaskType type, Exception exception)
        {
            SchedulerQueue schedulerQueue = Uow.SchedulerQueues.GetLastByType(type, SchedulerDestination);

            if (schedulerQueue != null)
            {
                Uow.SchedulerQueues.FinishAndChangeStatus(schedulerQueue, SchedulerExecutionStatus.Failed);
                schedulerQueue.Description = string.Format("InnerException: {0}; StackTrace: {1}", exception.InnerException, exception.StackTrace);

                return(BaseResponse.Success());
            }

            return(BaseResponse.Error(ErrorCode.SchedulerQueueItemNotFound));
        }
 private void ReactivateTask(OptionsPlayTask optionsPlayTask, SchedulerQueue schedulerQueue)
 {
     if (optionsPlayTask != null)
     {
         try
         {
             SchedulerQueue newQueue = optionsPlayTask.CreateSchedulerQueue();
             _schedulerCoreService.CreateQueueTask(newQueue, true);
         }
         catch (Exception newQueueException)
         {
             Logger.FatalError(string.Format(LogMessages.Error_FailedSchedulerJob, AppConfigManager.Environment, schedulerQueue.TaskType), newQueueException);
             _schedulerCoreService.LogTaskException(schedulerQueue.TaskType, newQueueException);
         }
     }
 }
 public static void Run(SchedulerQueue queue)
 {
     while (queue.Count > 0)
     {
         ScheduledItem scheduledItem = queue.Dequeue();
         if (!scheduledItem.IsCanceled)
         {
             TimeSpan timeSpan = scheduledItem.DueTime - Scheduler.CurrentThreadScheduler.Time;
             if (timeSpan.get_Ticks() > 0L)
             {
                 Thread.Sleep(timeSpan);
             }
             if (!scheduledItem.IsCanceled)
             {
                 scheduledItem.Invoke();
             }
         }
     }
 }
Example #12
0
 public static void Run(SchedulerQueue queue)
 {
     while (queue.Count > 0)
     {
         ScheduledItem scheduledItem = queue.Dequeue();
         if (!scheduledItem.IsCanceled)
         {
             TimeSpan timeout = scheduledItem.DueTime - Time;
             if (timeout.Ticks > 0)
             {
                 Thread.Sleep(timeout);
             }
             if (!scheduledItem.IsCanceled)
             {
                 scheduledItem.Invoke();
             }
         }
     }
 }
                public static void Run(SchedulerQueue queue)
                {
                    while (queue.Count > 0)
                    {
                        var item = queue.Dequeue();
                        if (!item.IsCanceled)
                        {
                            var wait = item.DueTime - CurrentThreadScheduler.Time;
                            if (wait.Ticks > 0)
                            {
                                Thread.Sleep(wait);
                            }

                            if (!item.IsCanceled)
                            {
                                item.Invoke();
                            }
                        }
                    }
                }
        // See http://www.cronmaker.com/
        //private const string CronString = "0 {1} {0} ? * MON,TUE,WED,THU,FRI *"; // {0} - hours, {1} - minutes

        private void UpdateTasksQueue()
        {
            ISchedulerCoreService schedulerCoreService = ObjectFactory.GetInstance <ISchedulerCoreService>();

            List <OptionsPlayTask> syrahTraderTasks = new List <OptionsPlayTask>();

            //Get all tasks from current assembly
            Type baseType = typeof(OptionsPlayTask);

            foreach (Type type in Assembly.GetAssembly(baseType).GetTypes())
            {
                if (baseType.IsAssignableFrom(type) && type != baseType)
                {
                    try
                    {
                        if (type != null)
                        {
                            Logger.Debug(type.ToString());
                            OptionsPlayTask instance = (OptionsPlayTask)ObjectFactory.GetInstance(type);
                            syrahTraderTasks.Add(instance);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Error("Error ", ex);
                    }
                }
            }

            //Update db for tasks
            foreach (OptionsPlayTask task in syrahTraderTasks)
            {
                Logger.Debug(task.TaskType.ToString());

                SchedulerQueue schedulerQueue = task.CreateSchedulerQueue();
                schedulerCoreService.CreateQueueTask(schedulerQueue, true);

                Logger.Debug(task.TaskType.ToString());
            }
        }
        private void ExecuteTask(SchedulerQueue schedulerQueue)
        {
            OptionsPlayTask optionsPlayTask = null;

            try
            {
                Func <OptionsPlayTask> syrahTraderTaskFunc;
                if (Tasks.TryGetValue(schedulerQueue.TaskType, out syrahTraderTaskFunc))
                {
                    Logger.Info(string.Format(LogMessages.Info_StartSchedulerTask, schedulerQueue.TaskType));
                    _schedulerCoreService.LogStartTaskExecution(schedulerQueue.TaskType);

                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();

                    optionsPlayTask = syrahTraderTaskFunc.Invoke();
                    optionsPlayTask.Execute();

                    stopwatch.Stop();

                    _schedulerCoreService.LogEndTaskExecution(schedulerQueue.TaskType);
                    Logger.Info(string.Format(LogMessages.Info_StopSchedulerTask, schedulerQueue.TaskType, stopwatch.ElapsedMilliseconds));

                    ReactivateTask(optionsPlayTask, schedulerQueue);
                }
            }
            catch (Exception ex)
            {
                Logger.Debug("OptionPlay error beginning");
                Logger.FatalError(string.Format(LogMessages.Error_FailedSchedulerJob, AppConfigManager.Environment, schedulerQueue.TaskType), ex);
                _schedulerCoreService.LogTaskException(schedulerQueue.TaskType, ex);
                Logger.Debug("Reactivate task start:" + optionsPlayTask.TaskType + ":" + schedulerQueue.Id + ":" + schedulerQueue.PlannedDate);
                //Reactivate failed task
                ReactivateTask(optionsPlayTask, schedulerQueue);
                Logger.Debug("Reactivate task end");
                Logger.Debug("OptionPlay error ending");
            }
        }
        /// <summary>
        /// Create Task(if not exists) in Queue
        /// </summary>
        public BaseResponse CreateQueueTask(SchedulerQueue schedulerQueue, bool terminatePrevious = false)
        {
            SchedulerQueue existsSchedulerQueue = Uow.SchedulerQueues.GetLastByType(schedulerQueue.TaskType, SchedulerDestination);

            if (existsSchedulerQueue == null || existsSchedulerQueue.Status != SchedulerExecutionStatus.InQueue)
            {
                if (terminatePrevious)
                {
                    Uow.SchedulerQueues
                    .ChangeStatus(schedulerQueue.TaskType,
                                  SchedulerExecutionStatus.Pending,
                                  SchedulerExecutionStatus.Terminated,
                                  SchedulerDestination,
                                  true);
                    existsSchedulerQueue = null;
                }

                if (existsSchedulerQueue == null)
                {
                    existsSchedulerQueue = new SchedulerQueue
                    {
                        Status       = SchedulerExecutionStatus.Pending,
                        TaskType     = schedulerQueue.TaskType,
                        PlannedDate  = schedulerQueue.PlannedDate,
                        Destination  = SchedulerDestination,
                        LockerMask   = schedulerQueue.LockerMask,
                        Dependencies = schedulerQueue.Dependencies
                    };

                    Uow.SchedulerQueues.Add(existsSchedulerQueue);

                    return(BaseResponse.Success());
                }
            }

            return(BaseResponse.Error(ErrorCode.SchedulerQueueItemAlreadyExists));
        }
        /// <summary>
        /// Pass scheduler task with id <param name="taskId">task id to scheduler queue</param>
        /// </summary>
        public BaseResponse ExecuteTask(string taskId)
        {
            SchedulerTask schedulerTask = Uow.SchedulerTasks.GetById(taskId, SchedulerDestination);

            if (schedulerTask != null)
            {
                SchedulerQueue schedulerQueue = new SchedulerQueue
                {
                    TaskType     = schedulerTask.Type,
                    LockerMask   = 0,                  //schedulerTask.LockerMask,
                    Dependencies = schedulerTask.Dependencies,
                    Status       = SchedulerExecutionStatus.Pending,
                    PlannedDate  = DateTime.UtcNow.AddMinutes(-1),
                };

                CreateQueueTask(schedulerQueue, true);
            }
            else
            {
                return(ErrorCode.SchedulerTaskNotFound);
            }

            return(BaseResponse.Success());
        }
 private static void SetQueue(SchedulerQueue newQueue)
 {
     s_threadLocalQueue = newQueue;
 }
 private static void SetQueue(SchedulerQueue newQueue)
 {
     s_threadLocalQueue = newQueue;
 }
                public static void Run(SchedulerQueue queue)
                {
                    while (queue.Count > 0)
                    {
                        var item = queue.Dequeue();
                        if (!item.IsCanceled)
                        {
                            var wait = item.DueTime - CurrentThreadScheduler.Time;
                            if (wait.Ticks > 0)
                            {
                                Thread.Sleep(wait);
                            }

                            if (!item.IsCanceled)
                                item.Invoke();
                        }
                    }
                }
 private static void SetQueue(SchedulerQueue newQueue)
 {
     Scheduler.CurrentThreadScheduler.s_threadLocalQueue = newQueue;
 }