Ejemplo n.º 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;
            }
        }
Ejemplo n.º 2
0
        public void UpdateOperation_UpdateFieldWithObject_String_SameAsUpdateFieldWithString(string inputValue)
        {
            var updateOp1 = new UpdateOperation();

            updateOp1.UpdateFieldWithString("field", inputValue);
            string json1 = updateOp1.ToJson();

            var updateOp2 = new UpdateOperation();

            updateOp2.UpdateFieldWithObject("field", inputValue);
            string json2 = updateOp2.ToJson();

            Assert.AreEqual(json1, json2);
        }
Ejemplo n.º 3
0
        public void UpdateOperation_AddsInts_DeserializesCorrectly()
        {
            // Setup
            var updateOp = new UpdateOperation();

            updateOp.UpdateFieldWithNumber("field1", "19");

            // System under test
            string resultJson = updateOp.ToJson();

            // Verify
            string expectedJson = "{\"field1\":19}";

            Assert.AreEqual(expectedJson, resultJson);
        }
Ejemplo n.º 4
0
        public void UpdateOperation_UpdateFieldWithObject_Double_SameAsUpdateFieldWithNumber()
        {
            double value = 3.14;

            var updateOp1 = new UpdateOperation();

            updateOp1.UpdateFieldWithNumber("field", value);
            string json1 = updateOp1.ToJson();

            var updateOp2 = new UpdateOperation();

            updateOp2.UpdateFieldWithObject("field", value);
            string json2 = updateOp2.ToJson();

            Assert.AreEqual(json1, json2);
        }
Ejemplo n.º 5
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());

                // 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;
            }
        }