private object DeserializeArgument(string argument, Type type)
        {
            object value;

            try
            {
                value = argument != null
                    ? jsonConverter.ConvertFromJson(argument, type)
                    : null;
            }
            catch (Exception jsonException)
            {
                if (type == typeof(object))
                {
                    value = argument;
                }
                else
                {
                    try
                    {
                        TypeConverter converter = TypeDescriptor.GetConverter(type);
                        value = converter.ConvertFromInvariantString(argument);
                    }
                    catch (Exception)
                    {
                        throw jsonException;
                    }
                }
            }
            return(value);
        }
        public IRunningTask Build(TaskInfo taskInfo)
        {
            string instanceTypeJson = jsonConverter.ConvertFromJson <string>(taskInfo.InstanceType);
            Type   instanceType     = Type.GetType(instanceTypeJson, throwOnError: true, ignoreCase: true);

            Type[]     argumentTypes = jsonConverter.ConvertFromJson <Type[]>(taskInfo.ParametersTypes);
            MethodInfo method        = expressionConverter.GetNonOpenMatchingMethod(instanceType, taskInfo.Method, argumentTypes);

            string[] serializedArguments = jsonConverter.ConvertFromJson <string[]>(taskInfo.Arguments);

            object[] arguments = expressionConverter.DeserializeArguments(method, serializedArguments);


            //TODO: Activate and run task (Smart logic)
            var instance = Activator.CreateInstance(instanceType);

            return(new RunningTask(taskInfo, instance, method, argumentTypes, arguments));
        }
Exemple #3
0
        /// <summary>
        /// Convert task entity into the task model.
        /// </summary>
        /// <param name="taskEntity">The <see cref="TaskEntity"/> instance.</param>
        /// <returns>The <see cref="TaskModel"/> instance.</returns>
        public TaskModel Convert(TaskEntity taskEntity)
        {
            string instanceTypeJson = jsonConverter.ConvertFromJson <string>(taskEntity.InstanceType);
            Type   instanceType     = Type.GetType(instanceTypeJson, throwOnError: true, ignoreCase: true);

            Type[]     argumentTypes = jsonConverter.ConvertFromJson <Type[]>(taskEntity.ParametersTypes);
            MethodInfo method        = expressionConverter.GetNonOpenMatchingMethod(instanceType, taskEntity.Method, argumentTypes);

            string[] serializedArguments = jsonConverter.ConvertFromJson <string[]>(taskEntity.Arguments);

            object[] arguments = expressionConverter.DeserializeArguments(method, serializedArguments);

            ActivationData activationData = new ActivationData(instanceType, method, arguments, argumentTypes);

            ScheduleInformation scheduleInformation = new ScheduleInformation(taskEntity.RepeatCrashCount);

            TaskModel taskModel = new TaskModel(taskEntity.Id, taskEntity.QueueId, taskEntity.ServerId, activationData, taskEntity.TaskState, scheduleInformation);

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