From 942be550c19f0c6cceea7c33dc4e61d687b54c45 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Tue, 28 Nov 2017 10:08:16 +0100 Subject: [PATCH] Moved hires rasterization to new BSO structure --- FrontSocket/FrontendServices.cs | 28 ++++++----- Logic/Logic.csproj | 2 + Logic/Support/BackendServiceOrder.cs | 12 +++++ .../BackendServiceOrderBase.cs | 15 ++++++ .../RasterizeDocumentHiresOrder.cs | 47 +++++++++++++++++++ Logic/Support/IBackendServiceOrderBase.cs | 10 ++++ .../BotCode => Logic/Support}/PdfProcessor.cs | 2 +- Site/Automation/UploadFileHandler.ashx | 9 +++- Utility/Utility.csproj | 1 - 9 files changed, 110 insertions(+), 16 deletions(-) create mode 100644 Logic/Support/BackendServices/RasterizeDocumentHiresOrder.cs rename {Utility/BotCode => Logic/Support}/PdfProcessor.cs (99%) diff --git a/FrontSocket/FrontendServices.cs b/FrontSocket/FrontendServices.cs index e73581f3..dc860227 100644 --- a/FrontSocket/FrontendServices.cs +++ b/FrontSocket/FrontendServices.cs @@ -14,6 +14,7 @@ using Swarmops.Logic.Cache; using Swarmops.Logic.Security; using Swarmops.Logic.Structure; using Swarmops.Logic.Support; +using Swarmops.Logic.Support.BackendServices; using Swarmops.Logic.Swarm; using WebSocketSharp; using WebSocketSharp.Server; @@ -61,9 +62,16 @@ namespace Swarmops.Frontend.Socket { pdfFilesList.Add(pdfFileString); } - string[] pdfFiles = pdfFilesList.ToArray(); - ConvertPdf (pdfFiles, (string) json["Guid"], Person.FromIdentity((int) json["PersonId"]), Organization.FromIdentity((int) json["OrganizationId"])); + JArray pdfClientNamesArray = (JArray) json["ClientFileNames"]; + List pdfClientNames = new List(); + foreach (string pdfClientName in pdfClientNamesArray) + { + pdfClientNames.Add(pdfClientName); + } + + ConvertPdf(pdfFilesList.ToArray(), pdfClientNames.ToArray(), (string) json["Guid"], Person.FromIdentity((int) json["PersonId"]), Organization.FromIdentity((int) json["OrganizationId"])); + break; case "ConvertPdfHires": // Send to backend @@ -134,7 +142,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, Organization organization) + private void ConvertPdf(string[] pdfFiles, string[] pdfClientNames, string guid, Person person, Organization organization) { List failedConversionFileNames = new List(); @@ -276,8 +284,7 @@ namespace Swarmops.Frontend.Socket long fileLength = new FileInfo(Document.StorageRoot + lastPageFileName).Length; debugWriter.WriteLine("{0:D2}%, saving page #{1}, bytecount {2}", progress, pageCounter, fileLength); - Document.Create(lastPageFileName, - "(BackendConvertedFile) " + pageCounter.ToString(CultureInfo.InvariantCulture), + Document.Create(lastPageFileName, pdfClientNames[fileIndex] + " " + pageCounter.ToString(CultureInfo.InvariantCulture), fileLength, guid, null, person); // Set to readonly, lock out changes, permit all read @@ -310,7 +317,7 @@ namespace Swarmops.Frontend.Socket debugWriter.WriteLine("{0:D2}%, saving last page #{1}, bytecount {2}", progress, pageCounter, fileLengthLastPage); lastDocument = Document.Create(lastPageFileName, - "(BackendConvertedFile) " + pageCounter.ToString(CultureInfo.InvariantCulture), + pdfClientNames[fileIndex] + " " + pageCounter.ToString(CultureInfo.InvariantCulture), fileLengthLastPage, guid, null, person); // Set to readonly, lock out changes, permit all read @@ -320,13 +327,8 @@ namespace Swarmops.Frontend.Socket // Finally, ask the backend to do the high-res conversions, but now we have the basic, fast ones - JObject backendRequest = new JObject(); - backendRequest["BackendRequest"] = "ConvertPdfHires"; - backendRequest["DocumentId"] = lastDocument.Identity; - FrontendLoop.SendMessageUpstream(backendRequest); - - backendRequest["MessageType"] = "Echo"; - Sessions.Broadcast(backendRequest.ToString()); + RasterizeDocumentHiresOrder backendOrder = new RasterizeDocumentHiresOrder(lastDocument); + backendOrder.Create(); } diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 14a3f9e5..5e263cc1 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -220,6 +220,7 @@ + @@ -385,6 +386,7 @@ + diff --git a/Logic/Support/BackendServiceOrder.cs b/Logic/Support/BackendServiceOrder.cs index 1f9c7567..20aad43f 100644 --- a/Logic/Support/BackendServiceOrder.cs +++ b/Logic/Support/BackendServiceOrder.cs @@ -7,7 +7,9 @@ using System.Threading.Tasks; using Swarmops.Basic.Types.System; using Swarmops.Common.Exceptions; using Swarmops.Database; +using Swarmops.Logic.Structure; using Swarmops.Logic.Support.BackendServices; +using Swarmops.Logic.Swarm; namespace Swarmops.Logic.Support { @@ -58,6 +60,11 @@ namespace Swarmops.Logic.Support IBackendServiceOrderBase order = (IBackendServiceOrderBase) methodFromXml.Invoke(null, new object[] { this.OrderXml }); order.ServiceOrderIdentity = this.Identity; // not part of serialization + // Supply with organization and person, if we have them + + order.Person = this.PersonId == 0 ? null : Person.FromIdentity(this.PersonId); + order.Organization = this.OrganizationId == 0 ? null : Organization.FromIdentity(this.OrganizationId); + // Mark as started SwarmDb.GetDatabaseForWriting().SetBackendServiceOrderActive(this.Identity); @@ -72,6 +79,10 @@ namespace Swarmops.Logic.Support } catch (Exception exception) { + if (!order.HasWorkerThread) + { + order.HasTerminated = true; + } order.Close(); SwarmDb.GetDatabaseForWriting().SetBackendServiceOrderException(this.Identity, exception); } @@ -81,6 +92,7 @@ namespace Swarmops.Logic.Support { try { + order.HasTerminated = true; order.Close(); } catch (DatabaseConcurrencyException) diff --git a/Logic/Support/BackendServices/BackendServiceOrderBase.cs b/Logic/Support/BackendServices/BackendServiceOrderBase.cs index 7168ba92..fe0f5156 100644 --- a/Logic/Support/BackendServices/BackendServiceOrderBase.cs +++ b/Logic/Support/BackendServices/BackendServiceOrderBase.cs @@ -71,6 +71,15 @@ namespace Swarmops.Logic.Support.BackendServices throw new NotImplementedException("The base Run() has no implementation. Derive and declare 'override'!"); } + /// + /// If you are deriving this class into something that spawns a worker thread, then that derived + /// class MUST have the worker thread terminate within 1.00 seconds from when Terminate is called. + /// + public virtual void Terminate() + { + throw new NotImplementedException("The base Terminate() has no implementation. Derive and declare 'override'!"); + } + /// /// This function MUST be called when the task has been completed, either on completion of Run() /// if you take a short amount of time, or at the end of your worker thread. @@ -99,6 +108,12 @@ namespace Swarmops.Logic.Support.BackendServices public DateTime CreatedDateTime { get; set; } public string BackendServiceClass { get; set; } public int ServiceOrderIdentity { get; set; } + public bool HasTerminated { get; set; } + + [XmlIgnore] + public Organization Organization { get; set; } + [XmlIgnore] + public Person Person { get; set; } [XmlIgnore] public bool HasWorkerThread { get; set; } diff --git a/Logic/Support/BackendServices/RasterizeDocumentHiresOrder.cs b/Logic/Support/BackendServices/RasterizeDocumentHiresOrder.cs new file mode 100644 index 00000000..a94be08f --- /dev/null +++ b/Logic/Support/BackendServices/RasterizeDocumentHiresOrder.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Swarmops.Logic.Support.BackendServices +{ + [Serializable] + public class RasterizeDocumentHiresOrder: BackendServiceOrderBase, IBackendServiceOrderBase + { + [Obsolete("Do not call the parameterless public ctor directly. It is needed for serialization.", true)] + public RasterizeDocumentHiresOrder() + { } + + public RasterizeDocumentHiresOrder(Document document) + { + this.DocumentId = document.Identity; + } + + public override void Run() + { + Document document = Document.FromIdentity(this.DocumentId); + HasWorkerThread = true; + WorkerThread = new Thread(LongRun); + WorkerThread.Start(this); + } + + private static void LongRun(object orderObject) + { + RasterizeDocumentHiresOrder order = (RasterizeDocumentHiresOrder) orderObject; + Document document = Document.FromIdentity(order.DocumentId); + + PdfProcessor.Rerasterize((Document) document, PdfProcessor.PdfProcessorOptions.HighQuality | PdfProcessor.PdfProcessorOptions.ForceOrphans); + order.Close(); + order.HasTerminated = true; + } + + public override void Terminate() + { + + } + int DocumentId { get; set; } + } +} diff --git a/Logic/Support/IBackendServiceOrderBase.cs b/Logic/Support/IBackendServiceOrderBase.cs index 178d8437..659fb6e9 100644 --- a/Logic/Support/IBackendServiceOrderBase.cs +++ b/Logic/Support/IBackendServiceOrderBase.cs @@ -5,6 +5,8 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using System.Xml.Serialization; +using Swarmops.Logic.Structure; +using Swarmops.Logic.Swarm; namespace Swarmops.Logic.Support { @@ -12,8 +14,16 @@ namespace Swarmops.Logic.Support { void Run(); void Close(); + + bool HasWorkerThread { get; set; } Thread WorkerThread { get; set; } + void Terminate(); + bool HasTerminated { get; set; } + + int ServiceOrderIdentity { get; set; } + Organization Organization { get; set; } + Person Person { get; set; } } } diff --git a/Utility/BotCode/PdfProcessor.cs b/Logic/Support/PdfProcessor.cs similarity index 99% rename from Utility/BotCode/PdfProcessor.cs rename to Logic/Support/PdfProcessor.cs index 9f45a7c0..a3af2758 100644 --- a/Utility/BotCode/PdfProcessor.cs +++ b/Logic/Support/PdfProcessor.cs @@ -10,7 +10,7 @@ using System.Text; using System.Threading.Tasks; using Swarmops.Logic.Support; -namespace Swarmops.Utility.BotCode +namespace Swarmops.Logic.Support { public class PdfProcessor { diff --git a/Site/Automation/UploadFileHandler.ashx b/Site/Automation/UploadFileHandler.ashx index 33746a08..c1b244df 100644 --- a/Site/Automation/UploadFileHandler.ashx +++ b/Site/Automation/UploadFileHandler.ashx @@ -20,6 +20,7 @@ using Swarmops.Logic.Support; using Swarmops.Logic.Swarm; using WebSocketSharp; using Mono.Unix.Native; +using Swarmops.Logic.Support.BackendServices; namespace Swarmops.Frontend.Automation { @@ -157,6 +158,7 @@ namespace Swarmops.Frontend.Automation { HttpPostedFile file = context.Request.Files[i]; string fullName = Path.GetFileName(file.FileName); + string clientFileName = string.Empty; Person uploadingPerson = authData.CurrentUser; Organization currentOrg = authData.CurrentOrganization; @@ -377,6 +379,10 @@ namespace Swarmops.Frontend.Automation // Ask backend for high-res conversion + RasterizeDocumentHiresOrder backendOrder = new RasterizeDocumentHiresOrder(lastDocument); + backendOrder.Create(); + + /* using (WebSocket socket = new WebSocket("ws://localhost:" + SystemSettings.WebsocketPortFrontend + "/Front?Auth=" + Uri.EscapeDataString(authData.Authority.ToEncryptedXml()))) @@ -388,7 +394,7 @@ namespace Swarmops.Frontend.Automation socket.Send(data.ToString()); socket.Ping(); // wait a little little while for send to work socket.Close(); - } + }*/ statuses.Add(new FilesStatus(fullName, file.ContentLength)); } @@ -433,6 +439,7 @@ namespace Swarmops.Frontend.Automation data["ServerRequest"] = "ConvertPdf"; data["PdfFiles"] = JArray.FromObject(pdfsForConversion.ToArray()); data["Guid"] = (string) guid; + data["ClientFileNames"] = JArray.FromObject(pdfClientNames.ToArray()); data["PersonId"] = authData.CurrentUser.Identity; data["OrganizationId"] = authData.CurrentOrganization.Identity; socket.Send(data.ToString()); diff --git a/Utility/Utility.csproj b/Utility/Utility.csproj index 95950b3f..b32c0fa5 100644 --- a/Utility/Utility.csproj +++ b/Utility/Utility.csproj @@ -96,7 +96,6 @@ -