/// <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); } }
/// <summary> /// Convert task model into the task entity. /// </summary> /// <param name="taskModel">The <see cref="TaskModel"/> instance.</param> /// <returns>The <see cref="TaskEntity"/> instance.</returns> public TaskEntity Convert(TaskModel taskModel) { logger.WriteTrace("Converting {0} into the TaskEntity...", taskModel); string[] args = expressionConverter.SerializeArguments(taskModel.ActivationData.Arguments); TaskEntity entity = new TaskEntity { Id = taskModel.Id, QueueId = taskModel.QueueId, InstanceType = jsonConverter.ConvertToJson(taskModel.ActivationData.InstanceType), Method = taskModel.ActivationData.Method.Name, ParametersTypes = jsonConverter.ConvertToJson(taskModel.ActivationData.ArgumentTypes), Arguments = jsonConverter.ConvertToJson(args), TaskState = taskModel.TaskState, RepeatCrashCount = taskModel.ScheduleInformation.RepeatCrashCount }; logger.WriteTrace("Converting {0} into the TaskEntity has been successfully completed. Result = {1}", taskModel, entity); return(entity); }