Example #1
0
        /// <summary>
        /// Corresponds to CLI "run" keyword. Executes handler specified by run options.
        /// </summary>
        public void Execute()
        {
            Type recordType = ActivatorUtil.ResolveType(Type, ActivatorSettings.Assemblies)
                              ?? throw new ArgumentException($"Type '{Type}' not found");

            // Register type in BsonClassMap if not yet registered.
            // This part is critical for derived types, since collection creation registers only base type.
            if (!BsonClassMap.IsClassMapRegistered(recordType))
            {
                MethodInfo registerMap = typeof(BsonClassMap)
                                         .GetMethod(nameof(BsonClassMap.RegisterClassMap), new Type[] { })
                                         .MakeGenericMethod(recordType);
                registerMap.Invoke(null, new object[] { });
            }

            (Type keyType, Type baseRecordType) = GetRecordTypeArguments(recordType);

            MethodInfo createHandlerMethod = typeof(RunCommand)
                                             .GetMethod(nameof(CreateHandler), BindingFlags.Static | BindingFlags.NonPublic)
                                             .MakeGenericMethod(keyType, baseRecordType, recordType);

            // TODO: changes to db naming should be synchronized with client, afterwards connection should be parsed from source
            string connectionLiteral = "ConnectionString=";

            // Extract ConnectionString from source string
            string[] sourceParams     = Source.Split(',');
            string   connectionString = sourceParams.First(t => t.StartsWith(connectionLiteral)).Substring(connectionLiteral.Length);

            // Convert connection string to db name and hosts
            MongoUrl url = MongoUrl.Create(connectionString);

            // Use case insensitive parsing for the environment type
            var dataSource = new TemporalMongoDataSource
            {
                EnvType     = Enum.Parse <EnvType>(EnvType, true),
                EnvGroup    = EnvGroup,
                EnvName     = EnvName,
                MongoServer = new MongoServerKey {
                    MongoServerUri = $"mongodb://{url.Server}"
                }
            };

            var context = new Context();

            context.DataSource = dataSource;
            context.DataSet    = TemporalId.Empty;

            object record = createHandlerMethod.Invoke(null, new object[] { context, this });

            MethodInfo handlerMethod = record.GetType().GetMethod(Handler)
                                       ?? throw new ArgumentException($"Method '{Handler}' not found");

            // Check that method has [HandlerMethod] attribute before calling it.
            if (handlerMethod.GetCustomAttribute <HandlerMethodAttribute>() == null)
            {
                throw new Exception($"Cannot run {Handler} method, missing [HandlerMethod] attribute.");
            }

            handlerMethod.Invoke(record, ActivatorUtil.CreateParameterValues(handlerMethod, Arguments));
        }
Example #2
0
        private Context GetTargetContext()
        {
            var dataSource = new TemporalMongoDataSource
            {
                EnvType     = Enum.Parse <EnvType>(EnvType, true),
                EnvGroup    = EnvGroup,
                EnvName     = EnvName,
                MongoServer = new MongoServerKey {
                    MongoServerUri = $"mongodb://{Host}"
                },
            };

            var context = new Context
            {
                DataSource = dataSource,
                DataSet    = TemporalId.Empty
            };

            return(context);
        }
Example #3
0
        /// <summary>
        /// Convert records stored in csv format to mongo storage.
        /// </summary>
        public void Execute()
        {
            // TODO - remove placeholder names
            var dataSource = new TemporalMongoDataSource
            {
                EnvType     = EnvType.User,
                EnvGroup    = "TEMP",
                EnvName     = "Default",
                MongoServer = new MongoServerKey {
                    MongoServerUri = "mongodb://localhost:27017"
                }                                                                                // TODO - specify server URI
            };

            Context context = new Context();

            context.DataSource = dataSource;
            context.DataSet    = TemporalId.Empty;

            // Process all directories inside given folder
            foreach (var dir in Directory.GetDirectories(CsvPath))
            {
                ProcessDirectory(context, dir, TemporalId.Empty);
            }
        }