Exemplo n.º 1
0
        /// <summary>
        /// Move source file to pool
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public SpoolFile MoveIn(string filePath)
        {
            var spoolFile = new SpoolFile(_configuration.Name, Index);

            try
            {
                var ext  = FilePathUtil.GetPathExtension(filePath);
                var path = GenerateFilePath(ext);
                spoolFile.Path = path;

                //直接移动
                File.Move(filePath, path);

                //Write queue
                _pendingQueue.Enqueue(spoolFile);

                //是否写满了(需要按照待处理的文件数量+处理中的数量进行计算,避免当关闭自动归还功能时,磁盘下的文件还有大量的堆积)
                if (_pendingQueue.Count + _progressingDict.Count > _configuration.TrainMaxFileCount)
                {
                    var info = BuildInfo();
                    var args = new TrainWriteOverEventArgs()
                    {
                        Train = info,
                    };
                    OnWriteOver?.Invoke(this, args);
                }

                return(spoolFile);
            }
            catch (Exception ex)
            {
                _logger.LogError("Move file to train failed, FilePool:'{0}',Train:'{1}',FilePath:'{2}',Exception:{3}.", _configuration.Name, Index, filePath, ex.Message);
                throw ex;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Write file
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fileExt"></param>
        /// <returns></returns>
        public async ValueTask <SpoolFile> WriteFileAsync(Stream stream, string fileExt)
        {
            var spoolFile = new SpoolFile(_configuration.Name, Index);

            try
            {
                var path = GenerateFilePath(fileExt);
                spoolFile.Path = path;
                //Write file
                await WriteInternalAsync(stream, path);

                //Write queue
                _pendingQueue.Enqueue(spoolFile);

                //是否写满了(需要按照待处理的文件数量+处理中的数量进行计算,避免当关闭自动归还功能时,磁盘下的文件还有大量的堆积)
                if (_pendingQueue.Count + _progressingDict.Count > _configuration.TrainMaxFileCount)
                {
                    var info = BuildInfo();
                    var args = new TrainWriteOverEventArgs()
                    {
                        Train = info,
                    };
                    OnWriteOver?.Invoke(this, args);
                }

                return(spoolFile);
            }
            catch (Exception ex)
            {
                _logger.LogError("Write file to train failed, FilePool:'{0}',Train:'{1}',FileExt:'{2}',Exception:{3}.", _configuration.Name, Index, fileExt, ex.Message);
                throw ex;
            }
            finally
            {
                //流释放
                stream?.Close();
                stream?.Dispose();
            }
        }