Beispiel #1
0
        private void ProcessOneBook(Book book)
        {
            try
            {
                _logger.TrackEvent("ProcessOneBook Start");
                string message = $"Processing: {book.BaseUrl}";
                Console.Out.WriteLine(message);
                _logger.LogVerbose(message);

                var initialUpdates = new UpdateOperation();
                initialUpdates.UpdateField(Book.kHarvestStateField, Book.HarvestState.InProgress.ToString());
                initialUpdates.UpdateField(Book.kHarvesterIdField, this.Identifier);

                var startTime = new Parse.Model.Date(DateTime.UtcNow);
                initialUpdates.UpdateField("harvestStartedAt", startTime.ToJson());

                _parseClient.UpdateObject(book.GetParseClassName(), book.ObjectId, initialUpdates.ToJson());

                // Process the book
                var finalUpdates = new UpdateOperation();
                var warnings     = FindBookWarnings(book);
                finalUpdates.UpdateField(Book.kWarningsField, Book.ToJson(warnings));

                // ENHANCE: Do more processing here

                // Write the updates
                finalUpdates.UpdateField(Book.kHarvestStateField, Book.HarvestState.Done.ToString());
                _parseClient.UpdateObject(book.GetParseClassName(), book.ObjectId, finalUpdates.ToJson());

                _logger.TrackEvent("ProcessOneBook End - Success");
            }
            catch (Exception e)
            {
                YouTrackIssueConnector.SubmitToYouTrack(e, $"Unhandled exception thrown while processing book \"{book.BaseUrl}\"");

                // Attempt to write to Parse that processing failed
                if (!String.IsNullOrEmpty(book?.ObjectId))
                {
                    try
                    {
                        var onErrorUpdates = new UpdateOperation();
                        onErrorUpdates.UpdateField(Book.kHarvestStateField, $"\"{Book.HarvestState.Failed.ToString()}\"");
                        onErrorUpdates.UpdateField(Book.kHarvesterIdField, this.Identifier);
                        _parseClient.UpdateObject(book.GetParseClassName(), book.ObjectId, onErrorUpdates.ToJson());
                    }
                    catch (Exception)
                    {
                        // If it fails, just let it be and throw the first exception rather than the nested exception.
                    }
                }
                throw;
            }
        }
        private void ProcessOneBook(Book book)
        {
            try
            {
                _logger.TrackEvent("ProcessOneBook Start");
                string message = $"Processing: {book.BaseUrl}";
                Console.Out.WriteLine(message);
                _logger.LogVerbose(message);

                var initialUpdates = new UpdateOperation();
                initialUpdates.UpdateField(Book.kHarvestStateField, Book.HarvestState.InProgress.ToString());
                initialUpdates.UpdateField(Book.kHarvesterIdField, this.Identifier);

                var startTime = new Parse.Model.Date(DateTime.UtcNow);
                initialUpdates.UpdateField("harvestStartedAt", startTime.ToJson());

                _parseClient.UpdateObject(book.GetParseClassName(), book.ObjectId, initialUpdates.ToJson());

                // Download the book
                _logger.TrackEvent("Download Book");
                string decodedUrl      = HttpUtility.UrlDecode(book.BaseUrl);
                string urlWithoutTitle = RemoveBookTitleFromBaseUrl(decodedUrl);
                string downloadRootDir = Path.Combine(Path.GetTempPath(), Path.Combine("BloomHarvester", this.Identifier));
                _logger.LogVerbose("Download Dir: {0}", downloadRootDir);
                string downloadBookDir = _transfer.HandleDownloadWithoutProgress(urlWithoutTitle, downloadRootDir);

                // Process the book
                var finalUpdates = new UpdateOperation();
                var warnings     = FindBookWarnings(book);
                finalUpdates.UpdateField(Book.kWarningsField, Book.ToJson(warnings));

                // ENHANCE: Do more processing here
                _logger.TrackEvent("Upload Book");

                UploadBook(decodedUrl, downloadBookDir);

                // Write the updates
                finalUpdates.UpdateField(Book.kHarvestStateField, Book.HarvestState.Done.ToString());
                _parseClient.UpdateObject(book.GetParseClassName(), book.ObjectId, finalUpdates.ToJson());

                _logger.TrackEvent("ProcessOneBook End - Success");
            }
            catch (Exception e)
            {
                YouTrackIssueConnector.SubmitToYouTrack(e, $"Unhandled exception thrown while processing book \"{book.BaseUrl}\"");

                // Attempt to write to Parse that processing failed
                if (!String.IsNullOrEmpty(book?.ObjectId))
                {
                    try
                    {
                        var onErrorUpdates = new UpdateOperation();
                        onErrorUpdates.UpdateField(Book.kHarvestStateField, $"\"{Book.HarvestState.Failed.ToString()}\"");
                        onErrorUpdates.UpdateField(Book.kHarvesterIdField, this.Identifier);
                        _parseClient.UpdateObject(book.GetParseClassName(), book.ObjectId, onErrorUpdates.ToJson());
                    }
                    catch (Exception)
                    {
                        // If it fails, just let it be and throw the first exception rather than the nested exception.
                    }
                }
                throw;
            }
        }