Esempio n. 1
0
        protected override void OnStart(string[] args)
        {
            string instanceId = new IdService().GetCurrentInstanceId();
            string bucketName = ConfigurationManager.AppSettings["BucketName"];
            int    checkInputParamsInterval = Convert.ToInt32(ConfigurationManager.AppSettings["CheckInputParamsInterval"]);
            var    regionEndpoint           = RegionEndpoint.GetBySystemName(ConfigurationManager.AppSettings["RegionEndpoint"]);

            using (var storage = new AlgoStorage(bucketName, instanceId, regionEndpoint))
            {
                string algoName = null;

                try
                {
                    var result = storage.GetInputParams(checkInputParamsInterval);
                    storage.DeleteInputParams(result.FileName);
                    algoName = result.AlgoName;
                    var parameters = AlgoParams.Parse(result.Parameters);

                    var launcherRootDirectory = ConfigurationManager.AppSettings["LauncherRootDirectory"];
                    var algoRootDirectory     = Path.Combine(launcherRootDirectory, algoName);

                    storage.Download(algoRootDirectory, algoName);

                    var activator  = new AlgoActivator();
                    var algoResult = activator.Activate(algoRootDirectory, parameters);
                    storage.UploadSuccessResult(algoResult, algoName);
                }
                catch (Exception e)
                {
                    storage.UploadErrorResult(e, algoName);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Устанавливает свойства для параметра алгоритма
        /// </summary>
        /// <param name="entryPoint">Информация о методе</param>
        /// <param name="instance">Экземпляр параметра</param>
        /// <param name="parameters">Параметры для передачи в алгоритм</param>
        private void MapInputParams(MethodInfo entryPoint, object instance, AlgoParams parameters)
        {
            if (parameters == null)
            {
                return;
            }

            var parameterInfo = entryPoint.GetParameters().FirstOrDefault();
            var properties    = parameterInfo.ParameterType.GetProperties();

            foreach (var property in properties)
            {
                if (!property.CanWrite)
                {
                    continue;
                }

                var parameterName = parameters.Keys.FirstOrDefault(p => p.ToLower() == property.Name.ToLower());

                if (parameterName != null)
                {
                    property.SetValue(instance, Convert.ChangeType(parameters[parameterName], property.PropertyType));
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Запускает алгоритм
        /// </summary>
        /// <param name="rootDirectory">Директория с файлами алгоритма</param>
        /// <param name="parameters">Параметры</param>
        /// <returns></returns>
        public object Activate(string rootDirectory, AlgoParams parameters = null)
        {
            if (string.IsNullOrEmpty(rootDirectory))
            {
                throw new ArgumentNullException("rootDirectory");
            }

            var entryPoint = FindEntryPoint(rootDirectory);

            return(CallEntryPointMethod(entryPoint, parameters));
        }
Esempio n. 4
0
        /// <summary>
        /// Вызывает точку входа в алгоритм
        /// </summary>
        /// <param name="entryPoint">Информация о методе</param>
        /// <param name="parameters">Параметры алгоритма</param>
        /// <returns></returns>
        private object CallEntryPointMethod(MethodInfo entryPoint, AlgoParams parameters)
        {
            var assembly = entryPoint.Module.Assembly;
            var typeName = assembly.GetName().Name + "." + EntryPointClassName;
            var type     = assembly.GetType(typeName);

            var algoInstance      = Activator.CreateInstance(type);
            var algoInputInstance = CreateInputInstance(entryPoint);

            MapInputParams(entryPoint, algoInputInstance, parameters);

            return(entryPoint.Invoke(algoInstance, new[] { algoInputInstance }));
        }
Esempio n. 5
0
        public static AlgoParams Parse(string input)
        {
            var segments = input.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            var result   = new AlgoParams();

            foreach (var segment in segments)
            {
                string key   = Regex.Match(segment, @"^.+(?=\=)").Value;
                string value = Regex.Match(segment, @"(?<=\=).+$").Value;

                result.Add(key, value);
            }

            return(result);
        }