Exemplo n.º 1
0
        /// <summary>
        /// Start new <c>task</c>.
        /// </summary>
        /// <param name="queueName">The queue's name.</param>
        /// <param name="task">The <see cref="Expression"/> instance.</param>
        /// <param name="scheduleStrategy">Schedule</param>
        /// <returns>The task's identifier.</returns>
        public int StartTask(string queueName, Expression <Action> task, IScheduleStrategy scheduleStrategy)
        {
            using (IUnitOfWork unitOfWork = unitOfWorkFactory.CreateUnitOfWork(contextSettings))
            {
                ActivationData activationData = expressionConverter.Convert(task);

                string[] args = expressionConverter.SerializeArguments(activationData.Arguments);

                TaskInfo entity = new TaskInfo
                {
                    QueueName        = queueName,
                    InstanceType     = jsonConverter.ConvertToJson(activationData.InstanceType),
                    Method           = activationData.Method.Name,
                    ParametersTypes  = jsonConverter.ConvertToJson(activationData.ArgumentTypes),
                    Arguments        = jsonConverter.ConvertToJson(args),
                    TaskState        = TaskStates.New,
                    ExecuteAt        = scheduleStrategy.GetNextExecutionTime(DateTime.UtcNow, TaskStates.New),
                    RepeatCrashCount = 3 //TODO: Settings
                };

                switch (scheduleStrategy.ScheduleType)
                {
                case ScheduleType.Immediately:
                case ScheduleType.ExecuteAt:
                {
                    entity.ScheduleInfoId = -1;
                    break;
                }

                default:
                {
                    ScheduleInfo scheduleInfo = new ScheduleInfo
                    {
                        ScheduleType     = scheduleStrategy.ScheduleType,
                        ScheduleData     = jsonConverter.ConvertToJson(scheduleStrategy),
                        ScheduleDataType = jsonConverter.ConvertToJson(scheduleStrategy.GetType())
                    };
                    unitOfWork.Insert(scheduleInfo);
                    entity.ScheduleInfoId = scheduleInfo.Id;
                    break;
                }
                }

                unitOfWork.Insert(entity);
                unitOfWork.Commit();

                return(entity.Id);
            }
        }
        private void ProcessCompletedTasks(int currentServerId)
        {
            using (IUnitOfWork unitOfWork = unitOfWorkFactory.CreateUnitOfWork(contextSettings))
            {
                while (readyTasks.Count > 0)
                {
                    TaskInfo taskInfo;
                    if (readyTasks.TryDequeue(out taskInfo) == false)
                    {
                        continue;
                    }
                    taskInfo.ServerId = currentServerId;
                    unitOfWork.Update(taskInfo);

                    if (taskInfo.ScheduleInfoId > 0)
                    {
                        var  scheduleInfo = scheduleRepository.Get(taskInfo.ScheduleInfoId);
                        Type type         = jsonConverter.ConvertFromJson <Type>(scheduleInfo.ScheduleDataType);
                        IScheduleStrategy scheduleStrategy = (IScheduleStrategy)jsonConverter.ConvertFromJson(scheduleInfo.ScheduleData, type);

                        TaskInfo entity = new TaskInfo
                        {
                            QueueName        = taskInfo.QueueName,
                            InstanceType     = taskInfo.InstanceType,
                            Method           = taskInfo.Method,
                            ParametersTypes  = taskInfo.ParametersTypes,
                            Arguments        = taskInfo.Arguments,
                            TaskState        = TaskStates.New,
                            ScheduleInfoId   = taskInfo.ScheduleInfoId,
                            ExecuteAt        = scheduleStrategy.GetNextExecutionTime(taskInfo.ExecuteAt, taskInfo.TaskState),
                            RepeatCrashCount = 3 //TODO: Settings
                        };
                        unitOfWork.Insert(entity, false);
                    }
                }
                unitOfWork.Commit();
            }
        }
 public ScheduleService(IEngineerPoolFactory engineerPoolFactory,
                        IScheduleStrategy scheduleStrategy)
 {
     _engineerPoolFactory = engineerPoolFactory;
     _scheduleStrategy    = scheduleStrategy;
 }
Exemplo n.º 4
0
 public MetaWeblog()
 {
     _store = AutofacConfig.IoC.Resolve<IDocumentStore>();
     _notification = AutofacConfig.IoC.Resolve<INotificationService>();
     _schedule = AutofacConfig.IoC.Resolve<IScheduleStrategy>();
 }
 public void AddScheduleStrategy(IScheduleStrategy algoritmStrat)
 {
     this.scheduleStrategy = algoritmStrat;
 }
Exemplo n.º 6
0
 /// <summary>
 /// Start new <c>task</c>.
 /// </summary>
 /// <param name="task">The <see cref="Expression"/> instance.</param>
 /// <param name="scheduleStrategy">Schedule</param>
 /// <returns>The task's identifier.</returns>
 public int StartTask(Expression <Action> task, IScheduleStrategy scheduleStrategy)
 {
     return(StartTask(defaultQueueName, task, scheduleStrategy));
 }
        private void ProcessNewTasks(int currentServerId)
        {
            if (taskProcessing.Slots > 0)
            {
                Queue <IRunningTask> runningTasks = new Queue <IRunningTask>();
                using (IUnitOfWork unitOfWork = unitOfWorkFactory.CreateUnitOfWork(contextSettings))
                {
                    //TODO: Queues
                    ICollection <TaskInfo> tasks = unitOfWork.ExecuteQuery <TaskInfo>(@"
                                       UPDATE TOP (@MaxRows) [DotNetCraftWiseQueue].[dbo].[TaskInfoes]
			                                SET [TaskState] = @UpdatedTaskState,
				                                [ServerId] = @ServerId
			                                OUTPUT inserted.*
			                                Where ( [ServerId] = 0 
					                                AND [TaskState] = @NewTask
					                                AND [QueueName] in (@QueueName)
					                                AND [ExecuteAt] <= @ExecuteAt 
					                                AND [RepeatCrashCount] > 0);"                    ,
                                                                                      new DataBaseParameter("@MaxRows", taskProcessing.Slots),
                                                                                      new DataBaseParameter("@UpdatedTaskState", TaskStates.Running),
                                                                                      new DataBaseParameter("@ServerId", currentServerId),
                                                                                      new DataBaseParameter("@NewTask", TaskStates.New),
                                                                                      new DataBaseParameter("@ExecuteAt", DateTime.UtcNow),
                                                                                      new DataBaseParameter("@QueueName", "default"));

                    foreach (TaskInfo taskInfo in tasks)
                    {
                        try
                        {
                            IRunningTask runningTask = taskBuilder.Build(taskInfo);
                            runningTasks.Enqueue(runningTask);
                            taskInfo.ServerId  = currentServerId;
                            taskInfo.TaskState = TaskStates.Running;
                        }
                        catch (Exception ex)
                        {
                            taskInfo.ServerId  = currentServerId;
                            taskInfo.TaskState = TaskStates.Failed;
                        }

                        if (taskInfo.ScheduleInfoId > 0)
                        {
                            ScheduleInfo      scheduleInfo     = scheduleRepository.Get(taskInfo.ScheduleInfoId);
                            Type              type             = jsonConverter.ConvertFromJson <Type>(scheduleInfo.ScheduleDataType);
                            IScheduleStrategy scheduleStrategy = (IScheduleStrategy)jsonConverter.ConvertFromJson(scheduleInfo.ScheduleData, type);
                            taskInfo.ExecuteAt = scheduleStrategy.GetNextExecutionTime(taskInfo.ExecuteAt, taskInfo.TaskState);
                        }

                        unitOfWork.Update(taskInfo);
                    }

                    if (runningTasks.Count > 0)
                    {
                        unitOfWork.Commit();
                    }
                }

                while (runningTasks.Count > 0)
                {
                    IRunningTask runningTask = runningTasks.Dequeue();
                    taskProcessing.RunTask(runningTask);
                }
            }
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="schedule"></param>
 /// <param name="dishesTime"></param>
 public MealManager(IScheduleStrategy schedule, EnumDishesTime dishesTime)
 {
     _schedule = schedule;
     _dishesTime = dishesTime;
 }
Exemplo n.º 9
0
 /// <summary>
 /// Method to get the MealPlan
 /// </summary>
 /// <param name="plan"></param>
 /// <returns></returns>
 private static MealPlan MealPlan(IScheduleStrategy schedule, EnumDishesTime dishesTime, string plan)
 {
     var manager = new MealManager(schedule, dishesTime).Manager(plan);
     return manager;
 }