public void FtpFileSystemWatcherCreatedEvent() { var fileName = "SuperFile.tmp"; var credentials = new NetworkCredential("anonymous", ""); var changed = new HashSet <string>(); var added = new HashSet <string>(); var removed = new HashSet <string>(); using var ftpServer = new FtpTestServer(FTP_SERVER, FTP_PORT); using var watcher = new FtpFileSystemWatcher(FTP_ADRESS, credentials); watcher.Changed += (e, arg) => changed.Add(arg.FullPath); watcher.Created += (e, arg) => added.Add(arg.FullPath); watcher.Deleted += (e, arg) => removed.Add(arg.FullPath); watcher.Frequency = 500; // ultra fast watching watcher.EnableRaisingEvents = true; Thread.Sleep(1000); // ADD A FILE using (var stream = File.OpenRead(CreateTmpFile(fileName, "a little test"))) { ftpServer.AddFile(fileName, stream); } Thread.Sleep(1500); // wait the event // CHANGE A FILE using (var stream = File.OpenRead(CreateTmpFile(fileName, "SuperFile content updated"))) { ftpServer.AddFile(fileName, stream); } Thread.Sleep(1500); // wait the event // REMOVE A FILE ftpServer.RemoveFile(fileName); Thread.Sleep(1500); // wait the event Check.That(added).Contains(fileName); Check.That(changed).Contains(fileName); Check.That(removed).Contains(fileName); }
public void Start() { SqlWatchers = new Dictionary <int, SqlWatcher>(); if (!Directory.Exists(SreConfigurationSection.CurrentConfig.LocalFileWatcher.DirectoryPull)) { Directory.CreateDirectory(SreConfigurationSection.CurrentConfig.LocalFileWatcher.DirectoryPull); } WindowsUtility.SetFolderPermission(SreConfigurationSection.CurrentConfig.LocalFileWatcher.DirectoryPull); Trace.TraceInformation("Pullers initialization started."); if (SymplusCoreConfigurationSection.CurrentConfig.TempDirectory.Length > Constants.MaxTempFolderPath) { throw new ConfigurationErrorsException(string.Format("'{0}' can not have more than '{1}' length", SymplusCoreConfigurationSection.CurrentConfig.TempDirectory, Constants.MaxTempFolderPath)); } _Manager = new Manager(); List <int> allDataSourceIds = _Manager.GetAllDataSourceIds(false); foreach (int appId in allDataSourceIds) { string dir = Path.Combine(SreConfigurationSection.CurrentConfig.LocalFileWatcher.DirectoryPull, appId.ToString()); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } } StartLocalFileSystemWatcher(); List <SreDataSource> dataSources = _Manager.GetDataSources(); foreach (SreDataSource dataSource in dataSources) { List <SreKey> keys = Cache.Instance.Bag[dataSource.Id + ".keys"] as List <SreKey>; if (keys == null) { keys = _Manager.GetApplicationKeys(dataSource.Id, true); Cache.Instance.Bag.Add(dataSource.Id + ".keys", keys); } if ((dataSource.DataFeederType != null) && ((DataFeederTypes)dataSource.DataFeederType == DataFeederTypes.PullFtp)) { #region Initializing FtpPullers string ftpRemoteLocation = FindSREKeyUsingType(keys, SreKeyTypes.FtpRemoteLocation); string ftpUserName = FindSREKeyUsingType(keys, SreKeyTypes.FtpUserName); string ftpPassword = FindSREKeyUsingType(keys, SreKeyTypes.FtpPassword); string strinterval = FindSREKeyUsingType(keys, SreKeyTypes.FtpWatchInterval); int ftpWatchIntervalInMinutes = 0; int.TryParse(strinterval, out ftpWatchIntervalInMinutes); if (ftpWatchIntervalInMinutes == 0) { ftpWatchIntervalInMinutes = 1; } string ftpLocalLocation = Path.Combine(SreConfigurationSection.CurrentConfig.LocalFileWatcher.DirectoryPull, dataSource.Id.ToString()); string appOutputFolder = Path.Combine(SreConfigurationSection.CurrentConfig.LocalFileWatcher.DirectoryOutput, dataSource.Id.ToString(), DateTime.Now.ToString("yyyyMMdd")); Dictionary <string, object> datasourceParameters = new Dictionary <string, object>(); lock (_lock) { datasourceParameters.Add("DataSourceId", dataSource.Id); datasourceParameters.Add("OutputFolder", appOutputFolder); datasourceParameters.Add("ProcessingBy", dataSource.ProcessingBy); } FtpFileSystemWatcher ftpWatcher = new FtpFileSystemWatcher(datasourceParameters, ftpRemoteLocation, ftpLocalLocation, ftpWatchIntervalInMinutes, ftpUserName, ftpPassword, false, true); Trace.TraceInformation("Initialized 'PullFtp'; '{0}' [ftpRemoteLocation = {1}, ftpLocalLocation = {2}, ftpUserName = {3},ftpPassword = {4}, interval = {5}]", dataSource.Name, ftpRemoteLocation, ftpLocalLocation, ftpUserName, ftpPassword, ftpWatchIntervalInMinutes); Registry.Instance.FtpWatchers.Add(ftpWatcher); #endregion Initializing FtpPullers } else if ((dataSource.DataFeederType != null) && ((DataFeederTypes)dataSource.DataFeederType == DataFeederTypes.PullLocalFileSystem)) { if (IsLocalFileSystemFoldersOverriden(keys)) { SreFileSystemWatcher sreFileSystemWatcher = new SreFileSystemWatcher(dataSource.Id, keys); Watchers.FileSystemWatcherEventHandler handler = new Watchers.FileSystemWatcherEventHandler(FileDownloaded); sreFileSystemWatcher.FileDownloaded -= handler; sreFileSystemWatcher.FileDownloaded += handler; sreFileSystemWatcher.StartWatching(); Registry.Instance.DataSourceFileWatcher.Add(dataSource.Id, sreFileSystemWatcher); } //we dont have to do anything if it is generic } else if ((dataSource.DataFeederType != null) && ((DataFeederTypes)dataSource.DataFeederType == DataFeederTypes.PullSql)) { #region Initializing SQL Pullers string connectionStringKeyName = FindSREKeyUsingName(keys, SreKeyTypes.PullSqlConnectionString); string returnType = FindSREKeyUsingType(keys, SreKeyTypes.PullSqlReturnType); string selectQuery = FindSREKeyUsingType(keys, SreKeyTypes.SqlQuery); string updateQuery = FindSREKeyUsingType(keys, SreKeyTypes.SqlUpdateQueryProcessing); string interfaceName = FindSREKeyUsingType(keys, SreKeyTypes.PullSqlInterfaceName); string strinterval = FindSREKeyUsingType(keys, SreKeyTypes.SqlWatchInterval); int interval = 0; int.TryParse(strinterval, out interval); if (interval <= 0) { interval = 1; } string appPullFolder = SreConfigurationSection.CurrentConfig.LocalFileWatcher.DirectoryPull + "\\" + dataSource.Id; string appOutputFolder = SreConfigurationSection.CurrentConfig.LocalFileWatcher.DirectoryOutput + "\\" + dataSource.Id + "\\" + DateTime.Now.ToString("yyyyMMdd"); Dictionary <string, object> applicationParameters = new Dictionary <string, object>(); lock (_lock) { applicationParameters.Add("DataSourceId", dataSource.Id); applicationParameters.Add("PullFolder", appPullFolder); applicationParameters.Add("OutputFolder", appOutputFolder); applicationParameters.Add("ProcessingBy", dataSource.ProcessingBy); } SqlWatcher sqlWatcher = new SqlWatcher(applicationParameters, connectionStringKeyName, returnType, selectQuery, updateQuery, interfaceName, interval); sqlWatcher.StartDownloading(); SqlWatchers.Add(dataSource.Id, sqlWatcher); Trace.TraceInformation("Initialized 'PullSql'; '{0}' [Connection String Name = {1}, ReturnType = {2}, Select Query = {3}, Update Query = {4}, InterfaceName = {5}, Interval = {6}]", dataSource.Name, connectionStringKeyName, returnType == "D" ? "Direct" : "Interfaced", selectQuery, updateQuery, interfaceName, interval); #endregion Initializing SQL Pullers } #region Initializing Pushers // if ((!string.IsNullOrEmpty(dataSource.PusherType)) //&& (Type.GetType(dataSource.PusherType) != null)) // { // lock (_lock) // { // Pushers pusher = null; // if (!Registry.Instance.DataSourcePushers.ContainsKey(dataSource.Id)) // { // Trace.TraceInformation("Initializing 'Pusher'; '{0}' = '{1}'.", dataSource.Name, dataSource.PusherType); // object objPusher = Activator.CreateInstance(Type.GetType(dataSource.PusherType)); // pusher = (Pushers)objPusher; // Registry.Instance.DataSourcePushers.Add(dataSource.Id, pusher); // } // else // { // Trace.TraceInformation("Initializing 2 'Pusher'; '{0}' = '{1}'.", dataSource.Name, dataSource.PusherType); // pusher = Registry.Instance.DataSourcePushers[dataSource.Id]; // } // PullersEventHandler pusherHandler = new PullersEventHandler(pusher.FileProcessed); // Registry.Instance.Pullers.FileProcessed -= pusherHandler; // Registry.Instance.Pullers.FileProcessed += pusherHandler; // } // } #endregion Initializing Pushers Trace.Flush(); } Trace.TraceInformation("Pullers initialization finished."); }