Example #1
0
        public static void LogToFile(string msg)
        {
            lock (_lock)
            {
                msg = $"{(DateTime.Now - _startTime).TotalSeconds:0.##} - {msg}";

                File.AppendAllText(LogFile, msg + "\r\n");
            }
        }
Example #2
0
 // NOTE: this method will be called from various threads.
 internal void RememberProcessedFile(string filename)
 {
     // Utilities.LockPerfTimer l1_clk = Utilities.LockPerfChecker.Start();
     lock (filenames_processed_lock)
     {
         // l1_clk.LockPerfTimerStop();
         File.AppendAllText(Filename_Store, filename + "\n");
         filenames_processed.Add(filename);
     }
 }
Example #3
0
 public static void LogStraightToFile(string msg)
 {
     if (!LoggingSettings.LogToFile || LogFile == default)
     {
         return;
     }
     lock (_logLock)
     {
         File.AppendAllText(LogFile.ToString(), $"{(DateTime.Now - _startTime).TotalSeconds:0.##} - {msg}\r\n", new UTF8Encoding(false, true));
     }
 }
Example #4
0
        internal static void DaemonGet(Library library, string fingerprint)
        {
            string filename_full  = PDFDocumentFileLocations.DocumentPath(library, fingerprint, "pdf");
            string filename_short = Path.GetFileName(filename_full);
            string pdf_path       = IntranetLibraryTools.GetLibraryPDFPath(library.WebLibraryDetail.IntranetPath, filename_short);

            DirectoryTools.CreateDirectory(Path.GetDirectoryName(filename_full));

            Logging.Info("+Copying down {0}", fingerprint);
            File.Copy(pdf_path, filename_full);
            Logging.Info("-Copying down {0}", fingerprint);

            // Write the audit
            if (true)
            {
                string audit_filename  = IntranetLibraryTools.GetLibraryAuditFilename(library.WebLibraryDetail.IntranetPath);
                string audit_directory = Path.GetDirectoryName(audit_filename);

                if (Directory.Exists(audit_directory))
                {
                    string audit_data = String.Format(
                        "{0}\t{1}\t{2}\t{3}\t{4}\t{5}\r\n"
                        , DateTime.UtcNow.ToString("yyyyMMdd.hhmmss")
                        , ConfigurationManager.Instance.ConfigurationRecord.Account_Username
                        , ConfigurationManager.Instance.ConfigurationRecord.Account_Nickname
                        , Environment.UserName
                        , filename_short
                        , pdf_path
                        );

                    try
                    {
                        File.AppendAllText(audit_filename, audit_data);
                    }
                    catch (Exception ex)
                    {
                        Logging.Warn(ex, "Unable to write intranet sync audit data.");
                    }
                }
            }
        }
Example #5
0
        public static PDFDocument AddNewPDFDocumentsToLibraryWithMetadata_SYNCHRONOUS(Library library, bool suppress_notifications, bool suppress_signal_that_docs_have_changed, FilenameWithMetadataImport[] filename_with_metadata_imports)
        {
            Stopwatch clk = new Stopwatch();

            clk.Start();

            // Notify if there is just a single doc
            suppress_notifications = suppress_notifications || (filename_with_metadata_imports.Length > 1);

            StatusManager.Instance.ClearCancelled("BulkLibraryDocument");

            PDFDocument last_added_pdf_document = null;

            int successful_additions = 0;

            for (int i = 0; i < filename_with_metadata_imports.Length; ++i)
            {
                if (Utilities.Shutdownable.ShutdownableManager.Instance.IsShuttingDown)
                {
                    Logging.Debug特("ImportingIntoLibrary: Breaking out of outer processing loop due to application termination");
                    break;
                }

                if (StatusManager.Instance.IsCancelled("BulkLibraryDocument"))
                {
                    Logging.Warn("User chose to stop bulk adding documents to the library");
                    break;
                }
                StatusManager.Instance.UpdateStatus("BulkLibraryDocument", String.Format("Adding document {0} of {1} to your library", i, filename_with_metadata_imports.Length), i, filename_with_metadata_imports.Length, true);

                // Relinquish control to the UI thread to make sure responsiveness remains tolerable at 100% CPU load.
                if (i % 10 == 7) // random choice for this heuristic: every tenth ADD should *yield* to the UI
                {
                    Utilities.GUI.WPFDoEvents.WaitForUIThreadActivityDone();
                }

                FilenameWithMetadataImport filename_with_metadata_import = filename_with_metadata_imports[i];

                try
                {
                    string filename = filename_with_metadata_import.filename;
                    string bibtex   = filename_with_metadata_import.bibtex;

                    // Although the outside world may allow us to be signalling, we will not do it unless we are the n-100th doc or the last doc
                    bool local_suppress_signal_that_docs_have_changed = suppress_signal_that_docs_have_changed;
                    if (!local_suppress_signal_that_docs_have_changed)
                    {
                        if ((i != filename_with_metadata_imports.Length - 1) && (0 != i % 100))
                        {
                            local_suppress_signal_that_docs_have_changed = true;
                        }
                    }

                    PDFDocument pdf_document = library.AddNewDocumentToLibrary_SYNCHRONOUS(filename, filename, filename, bibtex, filename_with_metadata_import.tags, filename_with_metadata_import.notes, suppress_notifications, local_suppress_signal_that_docs_have_changed);
                    if (null != pdf_document)
                    {
                        ++successful_additions;
                    }
                    last_added_pdf_document = pdf_document;
                }
                catch (Exception ex)
                {
                    Logging.Warn(ex, "There was a problem adding a document to the library:\n{0}", filename_with_metadata_import);

                    // if the problem report file doesn't exist yet, we have to create it:
                    Utilities.LockPerfTimer l2_clk = Utilities.LockPerfChecker.Start();
                    lock (problematic_import_documents_lock)
                    {
                        l2_clk.LockPerfTimerStop();
                        if (null == problematic_import_documents_filename)
                        {
                            problematic_import_documents_filename = TempFile.GenerateTempFilename("qiqqa-import-problem-report.txt");
                        }
                    }

                    // then always append the entire report chunk at once as multiple threads MAY
                    // be appending to the report simultaneously!
                    Utilities.LockPerfTimer l3_clk = Utilities.LockPerfChecker.Start();
                    lock (problematic_import_documents_lock)
                    {
                        l3_clk.LockPerfTimerStop();
                        File.AppendAllText(
                            problematic_import_documents_filename,
                            "The following files caused problems while being imported into Qiqqa:\r\n\r\n"
                            );

#if DEBUG
                        File.AppendAllText(
                            problematic_import_documents_filename,
                            String.Format(
                                "----------\r\n{0}\r\n{1}\r\n{2}\r\n----------\r\n"
                                , ex.Message
                                , ex.StackTrace
                                , filename_with_metadata_import
                                )
                            );
#else
                        File.AppendAllText(
                            problematic_import_documents_filename,
                            String.Format(
                                "----------\r\n{0}\r\n{1}\r\n----------\r\n"
                                , ex.Message
                                , filename_with_metadata_import
                                )
                            );
#endif
                    }
                }
            }

            if (filename_with_metadata_imports.Length > 0)
            {
                StatusManager.Instance.UpdateStatus("BulkLibraryDocument", String.Format("Added {0} of {1} document(s) to your library", successful_additions, filename_with_metadata_imports.Length));
            }
            else
            {
                StatusManager.Instance.ClearStatus("BulkLibraryDocument");
            }

            // If there have been some import problems, report them to the user.
            // However, we should not wait for the user response!
            Utilities.LockPerfTimer l1_clk = Utilities.LockPerfChecker.Start();
            lock (problematic_import_documents_lock)
            {
                l1_clk.LockPerfTimerStop();
                if (null != problematic_import_documents_filename)
                {
                    problematic_import_documents_alert_showing++;

                    // only show a single alert, not a plethora of 'em!
                    if (1 == problematic_import_documents_alert_showing)
                    {
                        // once the user has ack'ed or nack'ed the message, that handler
                        // will RESET the 'showing' counter and the party can start all over again!
                        SafeThreadPool.QueueUserWorkItem(o => AlertUserAboutProblematicImports());
                    }
                }
            }

            Logging.Debug特("AddNewPDFDocumentsToLibraryFromFolder_SYNCHRONOUS: time spent: {0} ms", clk.ElapsedMilliseconds);

            return(last_added_pdf_document);
        }
        public static PDFDocument AddNewPDFDocumentsToLibraryWithMetadata_SYNCHRONOUS(WebLibraryDetail web_library_detail, bool suppress_notifications, FilenameWithMetadataImport[] filename_with_metadata_imports)
        {
            WPFDoEvents.AssertThisCodeIs_NOT_RunningInTheUIThread();

            Stopwatch clk            = Stopwatch.StartNew();
            Stopwatch breathing_time = Stopwatch.StartNew();

            // Notify if there is just a single doc
            suppress_notifications = suppress_notifications || (filename_with_metadata_imports.Length > 1);

            StatusManager.Instance.ClearCancelled("BulkLibraryDocument");

            PDFDocument last_added_pdf_document = null;

            int successful_additions = 0;

            for (int i = 0; i < filename_with_metadata_imports.Length; ++i)
            {
                if (ShutdownableManager.Instance.IsShuttingDown)
                {
                    Logging.Debug特("ImportingIntoLibrary: Breaking out of outer processing loop due to application termination");
                    break;
                }

                if (StatusManager.Instance.IsCancelled("BulkLibraryDocument"))
                {
                    Logging.Warn("User chose to stop bulk adding documents to the library");
                    break;
                }
                StatusManager.Instance.UpdateStatus("BulkLibraryDocument", String.Format("Adding document {0} of {1} to your library", i + 1, filename_with_metadata_imports.Length), i, filename_with_metadata_imports.Length, true);

                FilenameWithMetadataImport filename_with_metadata_import = filename_with_metadata_imports[i];

                try
                {
                    string filename = filename_with_metadata_import.filename;
                    string bibtex   = filename_with_metadata_import.bibtex;

                    PDFDocument pdf_document = web_library_detail.Xlibrary.AddNewDocumentToLibrary_SYNCHRONOUS(filename, web_library_detail, filename, filename, bibtex, filename_with_metadata_import.tags, filename_with_metadata_import.notes, suppress_notifications);
                    if (null != pdf_document)
                    {
                        ++successful_additions;
                    }
                    last_added_pdf_document = pdf_document;
                }
                catch (Exception ex)
                {
                    Logging.Warn(ex, "There was a problem adding a document to the library:\n{0}", filename_with_metadata_import);

                    // if the problem report file doesn't exist yet, we have to create it:
                    // Utilities.LockPerfTimer l2_clk = Utilities.LockPerfChecker.Start();
                    lock (problematic_import_documents_lock)
                    {
                        // l2_clk.LockPerfTimerStop();
                        if (null == problematic_import_documents_filename)
                        {
                            problematic_import_documents_filename = TempFile.GenerateTempFilename("qiqqa-import-problem-report.txt");
                        }
                    }

                    // then always append the entire report chunk at once as multiple threads MAY
                    // be appending to the report simultaneously!
                    // Utilities.LockPerfTimer l3_clk = Utilities.LockPerfChecker.Start();
                    lock (problematic_import_documents_lock)
                    {
                        // l3_clk.LockPerfTimerStop();
                        File.AppendAllText(
                            problematic_import_documents_filename,
                            "The following files caused problems while being imported into Qiqqa:\r\n\r\n"
                            );

                        File.AppendAllText(
                            problematic_import_documents_filename,
                            String.Format(
                                "----------\r\n{0}\r\n{1}\r\n{2}\r\n----------\r\n"
                                , ex.Message
                                , ex.StackTrace
                                , filename_with_metadata_import
                                )
                            );
                    }
                }

                if (breathing_time.ElapsedMilliseconds >= FolderWatcher.MAX_SECONDS_PER_ITERATION)
                {
                    Logging.Info("AddNewPDFDocumentsToLibraryWithMetadata_SYNCHRONOUS: Taking a nap due to MAX_SECONDS_PER_ITERATION: {0} seconds consumed", breathing_time.ElapsedMilliseconds / 1E3);

                    ShutdownableManager.Sleep(FolderWatcher.SECONDS_TO_RELAX_PER_ITERATION);

                    breathing_time.Restart();
                }
            }

            if (filename_with_metadata_imports.Length > 0)
            {
                StatusManager.Instance.UpdateStatus("BulkLibraryDocument", String.Format("Added {0} of {1} document(s) to your library", successful_additions, filename_with_metadata_imports.Length));
            }
            else
            {
                StatusManager.Instance.ClearStatus("BulkLibraryDocument");
            }

            // If there have been some import problems, report them to the user.
            // However, we should not wait for the user response!
            // Utilities.LockPerfTimer l1_clk = Utilities.LockPerfChecker.Start();
            lock (problematic_import_documents_lock)
            {
                // l1_clk.LockPerfTimerStop();
                if (null != problematic_import_documents_filename)
                {
                    problematic_import_documents_alert_showing++;

                    // only show a single alert, not a plethora of 'em!
                    if (1 == problematic_import_documents_alert_showing)
                    {
                        // once the user has ack'ed or nack'ed the message, that handler
                        // will RESET the 'showing' counter and the party can start all over again!
                        SafeThreadPool.QueueUserWorkItem(o => AlertUserAboutProblematicImports());
                    }
                }
            }

            Logging.Debug特("AddNewPDFDocumentsToLibraryFromFolder_SYNCHRONOUS: time spent: {0} ms", clk.ElapsedMilliseconds);

            return(last_added_pdf_document);
        }
Example #7
0
 public override void AppendAllText(string path, string contents, Encoding encoding)
 {
     AfsFile.AppendAllText(path, contents, encoding);
 }
Example #8
0
 public override void AppendAllText(string path, string contents)
 {
     AfsFile.AppendAllText(path, contents);
 }