Ejemplo n.º 1
0
        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();
                }
            }
        }
Ejemplo n.º 2
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();
                }
            }
        }
Ejemplo n.º 3
0
        private bool MoveFileInternal(FileTransferInfo f, string searchPath)
        {
            _logger.Info("File found: " + f.FileName);
            var           fileIx = System.Threading.Interlocked.Increment(ref _filesCount);
            IFileTransfer output = null, input = null;

            // try to execte trasnfer
            try
            {
                var destinationPath = _options.Get("outputPath", "");

                // open destination
                // use a lock to avoid hitting the server to open multiple connections at the same time
                lock (_fileTransferService)
                {
                    input  = _fileTransferService.Open(searchPath, _options);
                    output = _fileTransferService.Open(destinationPath, _options);
                    if (!output.IsOpened())
                    {
                        throw new Exception(String.Format("Invalid destinationPath, {0}: {1}", output.LastError ?? "", destinationPath));
                    }
                }

                // get input stream
                var inputStream = input.GetFileStream(f.FileName);

                // upload file
                var fileName = output.Details.GetDestinationPath(f.FileName);
                if (!output.SendFile(inputStream.FileStream, fileName, true))
                {
                    _logger.Error(output.LastError);

                    // move to error folder
                    MoveToErrorLocation(f.FileName, searchPath);
                    // continue to next file
                    return(true);
                }

                _logger.Debug("File moved: " + f.FileName);

                // emit file info
                lock (_context)
                {
                    _context.Emit(_layout.Create()
                                  .Set("fileName", fileName)
                                  .Set("fileNumber", _filesCount)
                                  .Set("filePath", destinationPath)
                                  .Set("sourcePath", searchPath)
                                  .Set("sourceFileName", f.FileName));
                }

                // If backup folder exists, move file
                MoveToBackupLocation(f.FileName, searchPath);

                // If DeleSource is set
                if (_deleteSourceFile)
                {
                    TryDeleteFile(f.FileName, searchPath);
                }
            }
            catch (Exception ex)
            {
                _context.Error = ex.Message;
                _logger.Error(ex);
                MoveToErrorLocation(f.FileName, searchPath);
                return(false);
            }
            finally
            {
                if (input != null)
                {
                    input.Dispose();
                }
                if (output != null)
                {
                    output.Dispose();
                }
            }
            return(true);
        }