Ejemplo n.º 1
0
        private void ProcessFile(string path)
        {
            var stopwatch = Stopwatch.StartNew();

            Console.WriteLine("Processing file: " + path);
            var archivePath = string.Format(@"c:\books\archive\{0}.txt", Guid.NewGuid());

            Console.WriteLine("Archiving to: " + archivePath);
            _streamUser.CopyFile(path, archivePath);

            Console.WriteLine("Initialising db results");
            var bookFeed = new BookFeed
            {
                Path      = path,
                LineCount = 0,
                WordCount = 0,
                ProcessingMilliseconds = 0
            };

            _repository.Add(bookFeed);
            _repository.Save();

            var cancellationTokenSource = new CancellationTokenSource();

            var lines      = File.ReadAllLines(path);
            var wordCounts = new ConcurrentBag <int>();
            var apiTasks   = new List <Task>();

            for (var i = 0; i < lines.Length; i++)
            {
                var lineNumber = i;
                var line       = lines[i];
                apiTasks.Add(Task.Factory.StartNew(() => GetWordCount(path, lineNumber, line, wordCounts, cancellationTokenSource)));
            }
            try
            {
                Task.WaitAll(apiTasks.ToArray(), cancellationTokenSource.Token);
                var wordCount = wordCounts.Sum();

                Console.WriteLine("Saving results to db");
                bookFeed           = _repository.Get(path);
                bookFeed.LineCount = lines.Length;
                bookFeed.WordCount = wordCount;
                bookFeed.ProcessingMilliseconds = stopwatch.ElapsedMilliseconds;
                _repository.Save();
                Console.WriteLine("Complete, took: {0}ms", stopwatch.ElapsedMilliseconds);
                File.Delete(path);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Feed errored: " + ex.Message);
            }
        }
Ejemplo n.º 2
0
 public void Add(BookFeed bookFeed)
 {
     _context.BookFeed.Add(bookFeed);
 }