/// <summary> /// Adds a controller definition to the instance. /// </summary> /// <typeparam name="TFlowFileController">Gets the flow controller type.</typeparam> /// <param name="controllerName">Gets the flow controller name.</param> /// <returns></returns> public FlowControllerDefinition AddController <TFlowFileController>(string controllerName) where TFlowFileController : IFlowFileController { FlowControllerDefinition def; if (Controllers == null) { Controllers = new List <FlowControllerDefinition>(); } var lastOutputDirectory = Controllers.LastOrDefault()?.Output; // defaults Controllers.Add(def = new FlowControllerDefinition { ControllerName = controllerName, TypeName = typeof(TFlowFileController).AssemblyQualifiedName, // the last output directory will be the input to this one Input = lastOutputDirectory ?? $@"{BasePath}\{controllerName}\In", Output = $@"{BasePath}\{controllerName}\Out", PollInterval = 2, // seconds ExecutionOrder = Controllers.Count }); return(def); }
/// <summary> /// Starts a controller. /// </summary> /// <param name="definition">Definition to configure the controller.</param> protected void StartController(FlowControllerDefinition definition) { Guard.ArgumentNotNull(definition, nameof(definition)); if (_logger.IsDebugEnabled) { _logger.Debug($"Starting controller {definition.ControllerName}."); } //flow controller type Type flowControllerType; // resolve from precompiled list of well known processors // or assembly resolve if (_dictOfControllerTypes.ContainsKey(definition.ControllerName)) { flowControllerType = _dictOfControllerTypes[definition.ControllerName]; } else { flowControllerType = Type.GetType(definition.TypeName); } // ReSharper disable once UsePatternMatching IFlowFileController flowController = _flowControllerFactory .Create(flowControllerType); // flow controller if (flowController == null) { throw new InvalidOperationException( $"Controller type does not implement {typeof(IFlowFileController).Name}."); } // build rules that will be used to validate outgoing entitities var specProviderBuilder = new SpecProviderBuilder(_specProviderFactory); // spec providers expected to be single instance in // the application's scope if (definition.FieldValidationRules != null) { Type type = null; if (_logger.IsTraceEnabled) { _logger.Trace($"Activating controller type {flowController?.TargetEntityType?.AssemblyQualifiedTypeName}."); } try { type = Type.GetType(flowController.TargetEntityType.AssemblyQualifiedTypeName); } catch (Exception ex) { throw new ApplicationException($"Can't load type {flowController.TargetEntityType.AssemblyQualifiedTypeName}.", ex); } specProviderBuilder.Build( type, definition.FieldValidationRules.ToList()); } // configure data source log flowController.Config.InDirectory = definition.Input; flowController.Config.OutDirectory = definition.Output; flowController.Flow = _config.Flow; flowController.ControllerName = definition.ControllerName; // apply controller configuration details if (definition.ConfigurationDetails != null) { var type = flowController.GetType(); if (_logger.IsDebugEnabled) { _logger.Debug($"Configuring properties of controller type {type.Name} has been initialized."); } foreach (var configDetail in definition.ConfigurationDetails) { var property = type.GetProperty(configDetail.Key); if (property != null) { if (property.GetSetMethod() != null) { property.SetValue(flowController, configDetail.Value); } } } } // complete initialization flowController.Initialize(); if (_logger.IsDebugEnabled) { _logger.Debug($"Flow controller {definition.ControllerName} has been initialized."); } FlowFileControllerService processor = GetControllerService(flowController); // configure polling internval processor.Interval = TimeSpan.FromSeconds(definition.PollInterval); // log how many files were processed processor.FlowFileProcessed += (o, e) => { Processed++; }; // start reading from incoming data source processor.Start(); if (_logger.IsDebugEnabled) { _logger.Debug($"Flow controller {definition.ControllerName} has started."); } // active processor _processor = processor; // add to collection of initialized controllers _flowControllers.Add(flowController); }