From 4a2357534a156ca9fd4c956017790bddacb4ae95 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sun, 1 Oct 2017 16:59:44 +0200 Subject: [PATCH] Broadcasting PDF conversion progress --- FrontSocket/FrontendServices.cs | 47 +++++++++++++++++++------- Site/Automation/UploadFileHandler.ashx | 1 + Site/Controls/v5/Base/FileUpload.ascx | 35 +++++-------------- 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/FrontSocket/FrontendServices.cs b/FrontSocket/FrontendServices.cs index f16756a4..8dc66f31 100644 --- a/FrontSocket/FrontendServices.cs +++ b/FrontSocket/FrontendServices.cs @@ -62,7 +62,7 @@ namespace Swarmops.Frontend.Socket } string[] pdfFiles = pdfFilesList.ToArray(); - ConvertPdf (pdfFiles, (string) json["Guid"], Person.FromIdentity((int) json["PersonId"])); + ConvertPdf (pdfFiles, (string) json["Guid"], Person.FromIdentity((int) json["PersonId"]), Organization.FromIdentity((int) json["OrganizationId"])); break; default: @@ -127,7 +127,7 @@ namespace Swarmops.Frontend.Socket // Sessions.Broadcast("{\"messageType\":\"EditorCount\"," + String.Format("\"editorCount\":\"{0}\"", Sessions.ActiveIDs.ToArray().Length) + '}'); } - private void ConvertPdf(string[] pdfFiles, string guid, Person person) + private void ConvertPdf(string[] pdfFiles, string guid, Person person, Organization organization) { List failedConversionFileNames = new List(); @@ -205,11 +205,15 @@ namespace Swarmops.Frontend.Socket " " + Document.StorageRoot + relativeFileName + "-%04d.png\""); int pageCounter = 0; // the first produced page will be zero - int currentPageBaseProgress = progressFileStep * fileIndex + currentFilePageStep * pageCounter; + int currentPageBaseProgress = progressFileStep * fileIndex; string testPageFileName = String.Format("{0}-{1:D4}.png", relativeFileName, pageCounter); debugWriter.WriteLine("{0:D2}%, testPageFileName set to {1}", progress, testPageFileName); string lastPageFileName = testPageFileName; + // Convert works by first calling imagemagick that creates /tmp/magick-* files + + int startMagickCount = Directory.EnumerateFileSystemEntries("/tmp/magick-*").Count(); + while (pageCounter < currentFilePageCount) { while (!File.Exists(Document.StorageRoot + testPageFileName)) @@ -225,16 +229,25 @@ namespace Swarmops.Frontend.Socket process.WaitForExit(250); // this is a more elaborate version of thread.sleep that prevents Apache recycling } + + if (pageCounter == 0) + { + // If first page hasn't appeared yet, check for the Magick temp files + + int currentMagickCount = Directory.EnumerateFileSystemEntries("/tmp/magick-*").Count(); + int currentFilePercentage = currentMagickCount*50/pageCounter; + if (currentFilePercentage > 50) + { + currentFilePercentage = 50; // we may be not the only one converting right now + } + BroadcastGuidProgress(organization, guid, + progressFileStep*fileIndex + currentFilePercentage*100/progressFileStep); + } } - currentPageBaseProgress = progressFileStep * fileIndex + currentFilePageStep * (pageCounter + 1); - progress = currentPageBaseProgress; - if (progress > 99) - { - progress = 99; // can't reach 100 before finished - } + progress = progressFileStep * fileIndex + currentFilePageStep/2 + currentFilePageStep * (pageCounter + 1) / 2; debugWriter.WriteLine("{0:D2}%, found page #{1}", progress, pageCounter + 1); - GuidCache.Set("Pdf-" + guid + "-Progress", progress); + BroadcastGuidProgress(organization, guid, progress); // If the page# file that has appeared is 1+, then the preceding file is ready @@ -286,14 +299,22 @@ namespace Swarmops.Frontend.Socket } finally { - debugWriter.WriteLine("Thread exiting"); + debugWriter.WriteLine("Handler exiting"); - GuidCache.Set("Pdf-" + guid + "-Progress", 100); - // only here may the caller fetch the results + BroadcastGuidProgress(organization, guid, 100); } } } + private void BroadcastGuidProgress(Organization organization, string guid, int progress) + { + JObject json = new JObject(); + json["Guid"] = guid; + json["Progress"] = progress; + + FrontendLoop.BroadcastToOrganization(organization, json); + } + public Authority Authority { get { return this._authority; } } private Authority _authority = null; diff --git a/Site/Automation/UploadFileHandler.ashx b/Site/Automation/UploadFileHandler.ashx index e52a1638..bc13f0b3 100644 --- a/Site/Automation/UploadFileHandler.ashx +++ b/Site/Automation/UploadFileHandler.ashx @@ -366,6 +366,7 @@ namespace Swarmops.Frontend.Automation data["PdfFiles"] = JArray.FromObject(pdfsForConversion.ToArray()); data["Guid"] = (string) guid; data["PersonId"] = authData.CurrentUser.Identity; + data["OrganizationId"] = authData.CurrentOrganization.Identity; socket.Send(data.ToString()); socket.Ping(); // wait a little little while for send to work socket.Close(); diff --git a/Site/Controls/v5/Base/FileUpload.ascx b/Site/Controls/v5/Base/FileUpload.ascx index dd4a3bef..86acd081 100644 --- a/Site/Controls/v5/Base/FileUpload.ascx +++ b/Site/Controls/v5/Base/FileUpload.ascx @@ -92,35 +92,16 @@ }); - function <%=this.ClientID%>_repingPdfProgress() { + function progressUpdateCallback_<%=this.GuidString%>(progress) { - var jsonData = {}; - jsonData.guid = "Pdf-<%=GuidString%>"; + $("#<%=this.ClientID %>_DivProgressPdfConvert .progressbar-value").animate( + { + width: progressPercent + "%" + }, { queue: false }); - SwarmopsJS.ajaxCall( - "/Automation/Json-ByGuid.aspx/GetNonsocketProgress", - jsonData, - function (result) { - if (result.Success) { - var progressPercent = parseInt(result.DisplayMessage); - $("#<%=this.ClientID %>_DivProgressPdfConvert .progressbar-value").animate( - { - width: progressPercent + "%" - }, { queue: false }); - - if (progressPercent < 100) { - setTimeout(function() { <%=this.ClientID%>_repingPdfProgress(); }, 500); - } else { - // PDF CONVERSION COMPLETE, handle this - } - } - }, - function (error) { - // try again - - setTimeout(function() { <%=this.ClientID%>_repingPdfProgress(); }, 500); - } - ); + if (progressPercent >= 100) { + alertify.success("Percent >= 100"); + } }