Exemple #1
0
        public bool Execute(ISessionContext context)
        {
            var _logger  = context.GetLogger();
            var _options = context.Options;

            IFileTransfer input               = null;
            string        lastFile            = null;
            int           filesCount          = 0;
            int           maxFilesCount       = Int32.MaxValue;
            var           fileTransferService = context.GetContainer().GetInstanceOf <IFileService> ();

            try
            {
                var searchPath = _options.Get("inputPath", "");
                if (String.IsNullOrEmpty(searchPath))
                {
                    throw new ArgumentNullException("inputPath");
                }

                var deleteSourceFile = _options.Get <bool>("deleteSourceFile", false);

                // prepare paths
                input = fileTransferService.Open(searchPath, _options);
                if (!input.IsOpened())
                {
                    throw new Exception("Invalid searchPath: " + searchPath);
                }

                maxFilesCount = _options.Get("maxFileCount", maxFilesCount);
                if (maxFilesCount <= 0)
                {
                    maxFilesCount = Int32.MaxValue;
                }

                var defaultEncoding = Encoding.GetEncoding(_options.Get("encoding", "ISO-8859-1"));

                // open connection
                string line;
                Layout layout = new Layout();

                foreach (var f in input.GetFileStreams())
                {
                    lastFile = f.FileName;
                    _logger.Info("File found: " + lastFile);
                    filesCount++;

                    // read file
                    using (var reader = new StreamReader(f.FileStream, defaultEncoding, true))
                    {
                        int n = 1;
                        while ((line = reader.ReadLine()) != null)
                        {
                            context.Emit(layout.Create()
                                         .Set("fileName", f.FileName)
                                         .Set("fileNumber", filesCount)
                                         .Set("filePath", searchPath)
                                         .Set("lineNumber", n++)
                                         .Set("line", line));
                        }
                    }

                    // If backup folder exists, move file
                    if (!String.IsNullOrWhiteSpace(_options.Get("backupLocation", "")))
                    {
                        // TODO: implement move operation if location are the same!
                        using (var backupLocation = fileTransferService.Open(_options.Get("backupLocation", ""), _options))
                        {
                            var destName = backupLocation.Details.GetDestinationPath(f.FileName);
                            backupLocation.SendFile(input.GetFileStream(f.FileName).FileStream, destName, true);
                            _logger.Info("Backup file created: " + destName);
                        }
                    }

                    // If DeleSource is set
                    if (deleteSourceFile)
                    {
                        input.RemoveFile(f.FileName);
                        _logger.Info("File deleted: " + f.FileName);
                    }

                    // limit
                    if (filesCount >= maxFilesCount)
                    {
                        break;
                    }
                }

                if (filesCount > 0)
                {
                    _logger.Success("Done");
                    return(true);
                }
                else
                {
                    _logger.Debug("No Files Found on: " + searchPath);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                context.Error = ex.Message;
                _logger.Error(ex);
                try
                {
                    if (lastFile != null && input != null && !String.IsNullOrEmpty(_options.Get("errorLocation", "")))
                    {
                        // move files
                        using (var parsedErrorLocation = fileTransferService.Open(_options.Get("errorLocation", ""), _options))
                        {
                            var destName = parsedErrorLocation.Details.GetDestinationPath(lastFile);
                            parsedErrorLocation.SendFile(input.GetFileStream(lastFile).FileStream, destName, true);
                        }
                    }
                }
                catch { }
                return(false);
            }
            finally
            {
                if (input != null)
                {
                    input.Dispose();
                }
            }
        }
        public bool Execute(ISessionContext context)
        {
            _context = context;
            _logger  = context.GetLogger();
            _options = context.Options;

            IFileTransfer input = null;

            _fileTransferService = context.GetContainer().GetInstanceOf <IFileService> ();
            _deleteSourceFile    = _options.Get <bool> ("deleteSourceFile", false);
            _retryCount          = _options.Get <int> ("retryCount", 2);
            _retryWait           = TimeSpan.FromMilliseconds(_options.Get <int> ("retryWaitMs", 250));

            try
            {
                var searchPath = _options.Get("inputPath", _options.Get("searchPath", ""));
                if (String.IsNullOrEmpty(searchPath))
                {
                    throw new ArgumentNullException("inputPath");
                }

                if (String.IsNullOrEmpty(_options.Get("outputPath", "")))
                {
                    throw new ArgumentNullException("outputPath");
                }

                _maxFilesCount = _options.Get("maxFileCount", _maxFilesCount);
                if (_maxFilesCount <= 0)
                {
                    _maxFilesCount = Int32.MaxValue;
                }

                // prepare paths
                input = _fileTransferService.Open(searchPath, _options);
                if (!input.IsOpened())
                {
                    throw new Exception(String.Format("Invalid inputPath, {0}: {1}", input.LastError ?? "", searchPath));
                }

                // try move files
                ParallelTasks <FileTransferInfo> .Process(input.ListFiles().Take(_maxFilesCount), 0, _options.Get("maxConcurrency", 2), f => MoveFileInternal(f, searchPath));

                if (!String.IsNullOrEmpty(input.LastError))
                {
                    _logger.Warn(input.LastError);
                }

                if (_filesCount > 0)
                {
                    _logger.Success("Done");
                    return(true);
                }
                else
                {
                    _logger.Debug("No Files Found on: " + searchPath);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                context.Error = ex.Message;
                _logger.Error(ex);
                return(false);
            }
            finally
            {
                if (input != null)
                {
                    input.Dispose();
                }
            }
        }