From 3f516490d7040a970c72aaf7a66f93fbf9f674ac Mon Sep 17 00:00:00 2001 From: Graham Burgsma Date: Thu, 9 Apr 2020 17:05:14 -0400 Subject: [PATCH 1/2] Upgrade to Vapor 4 --- .gitignore | 3 ++- Package.swift | 16 +++++++++------- Sources/wkhtmltopdf/Document+Generate.swift | 10 +++++----- Tests/wkhtmltopdfTests/wkhtmltopdfTests.swift | 16 ++++++++++++---- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index cff3dd9..9c8b198 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ Packages .build .DS_Store *.xcodeproj - +Package.resolved +.swiftpm diff --git a/Package.swift b/Package.swift index 14a86b4..b719e3a 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:4.2 +// swift-tools-version:5.2 import PackageDescription let package = Package( @@ -6,20 +6,22 @@ let package = Package( products: [ .library( name: "wkhtmltopdf", - targets: ["wkhtmltopdf"]), + targets: ["wkhtmltopdf"] + ), ], dependencies: [ - .package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"), - .package(url: "https://github.com/vapor/service.git", from: "1.0.0") + .package(url: "https://github.com/apple/swift-nio.git", from: "2.13.1"), ], targets: [ .target( name: "wkhtmltopdf", dependencies: [ - "Service" - ]), + .product(name: "NIO", package: "swift-nio") + ] + ), .testTarget( name: "wkhtmltopdfTests", - dependencies: ["wkhtmltopdf", "Vapor"]), + dependencies: ["wkhtmltopdf"] + ), ] ) diff --git a/Sources/wkhtmltopdf/Document+Generate.swift b/Sources/wkhtmltopdf/Document+Generate.swift index a6f255f..8a8a1f0 100644 --- a/Sources/wkhtmltopdf/Document+Generate.swift +++ b/Sources/wkhtmltopdf/Document+Generate.swift @@ -1,16 +1,16 @@ import Foundation -import Service +import NIO extension Document { - public func generatePDF(on container: Container) throws -> Future { - let sharedThreadPool = try container.make(BlockingIOThreadPool.self) - - return sharedThreadPool.runIfActive(eventLoop: container.eventLoop) { () -> Data in + public func generatePDF(on eventLoop: EventLoop) throws -> EventLoopFuture { + return NIOThreadPool(numberOfThreads: 1).runIfActive(eventLoop: eventLoop) { let fileManager = FileManager.default + // Create the temp folder if it doesn't already exist let workDir = "/tmp/vapor-wkhtmltopdf" try fileManager.createDirectory(atPath: workDir, withIntermediateDirectories: true) + // Save input pages to temp files, and build up args to wkhtmltopdf var wkArgs: [String] = [ "--zoom", self.zoom, diff --git a/Tests/wkhtmltopdfTests/wkhtmltopdfTests.swift b/Tests/wkhtmltopdfTests/wkhtmltopdfTests.swift index 42e235b..a3b5c07 100644 --- a/Tests/wkhtmltopdfTests/wkhtmltopdfTests.swift +++ b/Tests/wkhtmltopdfTests/wkhtmltopdfTests.swift @@ -1,22 +1,30 @@ import XCTest -import Vapor +import NIO @testable import wkhtmltopdf class wkhtmltopdfTests: XCTestCase { - var app: Application! + var group: EventLoopGroup! override func setUp() { super.setUp() - app = try! Application(config: .default(), environment: .testing, services: .default()) + group = MultiThreadedEventLoopGroup(numberOfThreads: 1) + } + + override func tearDown() { + super.tearDown() + + XCTAssertNoThrow(try group.syncShutdownGracefully()) } func testStringPDF() throws { + let eventLoop = group.next() + let document = Document(margins: 15) let page1 = Page("

Page from direct HTML

") document.pages = [page1] - let data = try document.generatePDF(on: app).wait() + let data = try document.generatePDF(on: eventLoop).wait() // Cop-out test, just ensuring that the returned data is something XCTAssert(data.count > 50) // Visual test From 909a4e06067f4d8af4a6274f6bdd9475f7f68bfa Mon Sep 17 00:00:00 2001 From: Graham Burgsma Date: Mon, 11 May 2020 18:45:58 -0400 Subject: [PATCH 2/2] Added option to specify threadpool on generatePDF. --- Sources/wkhtmltopdf/Document+Generate.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/wkhtmltopdf/Document+Generate.swift b/Sources/wkhtmltopdf/Document+Generate.swift index 8a8a1f0..25b60d7 100644 --- a/Sources/wkhtmltopdf/Document+Generate.swift +++ b/Sources/wkhtmltopdf/Document+Generate.swift @@ -3,8 +3,8 @@ import NIO extension Document { - public func generatePDF(on eventLoop: EventLoop) throws -> EventLoopFuture { - return NIOThreadPool(numberOfThreads: 1).runIfActive(eventLoop: eventLoop) { + public func generatePDF(on threadPool: NIOThreadPool = NIOThreadPool(numberOfThreads: 1), eventLoop: EventLoop) throws -> EventLoopFuture { + return threadPool.runIfActive(eventLoop: eventLoop) { let fileManager = FileManager.default // Create the temp folder if it doesn't already exist