/// <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(); } }
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); } } }