Skip to content

Commit

Permalink
beta.2 (#49)
Browse files Browse the repository at this point in the history
* beta.2

* rm print + fix warning
  • Loading branch information
tanner0101 authored Dec 9, 2019
1 parent 6999d96 commit d0cdfad
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 106 deletions.
9 changes: 1 addition & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: swift test --enable-test-discovery
- run: swift test --enable-test-discovery --sanitize=thread
bionic:
container:
image: vapor/swift:5.1-bionic
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: swift test --enable-test-discovery
thread:
container:
image: vapor/swift:5.1-bionic
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.library(name: "Jobs", targets: ["Jobs"]),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", .branch("master"))
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta.2")
],
targets: [
.target(name: "Jobs", dependencies: ["Vapor"]),
Expand Down
110 changes: 110 additions & 0 deletions Sources/Jobs/Application+Jobs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import Foundation
import Vapor
import NIO

extension Application {
public var jobs: Jobs {
.init(application: self)
}

public struct Jobs {
public struct Provider {
let run: (Application) -> ()

public init(_ run: @escaping (Application) -> ()) {
self.run = run
}
}

final class Storage {
public var configuration: JobsConfiguration
let command: JobsCommand
var driver: JobsDriver?

public init(_ application: Application) {
self.configuration = .init(logger: application.logger)
self.command = .init(application: application)
application.commands.use(self.command, as: "jobs")
}

}

struct Key: StorageKey {
typealias Value = Storage
}

struct Lifecycle: LifecycleHandler {
func shutdown(_ application: Application) {
application.jobs.storage.command.shutdown()
if let driver = application.jobs.storage.driver {
driver.shutdown()
}
}
}

public var configuration: JobsConfiguration {
get { self.storage.configuration }
nonmutating set { self.storage.configuration = newValue }
}

public var queue: JobsQueue {
self.queue(.default)
}

public var driver: JobsDriver {
guard let driver = self.storage.driver else {
fatalError("No Jobs driver configured. Configure with app.jobs.use(...)")
}
return driver
}

var storage: Storage {
if self.application.storage[Key.self] == nil {
self.initialize()
}
return self.application.storage[Key.self]!
}

let application: Application

public func queue(
_ name: JobsQueueName,
logger: Logger? = nil,
on eventLoop: EventLoop? = nil
) -> JobsQueue {
return self.driver.makeQueue(
with: .init(
queueName: name,
configuration: self.configuration,
logger: logger ?? self.application.logger,
on: eventLoop ?? self.application.eventLoopGroup.next()
)
)
}

public func add<J>(_ job: J) where J: Job {
self.configuration.add(job)
}

public func use(_ provider: Provider) {
provider.run(self.application)
}

public func use(custom driver: JobsDriver) {
self.storage.driver = driver
}

public func schedule<J>(_ job: J) -> ScheduleBuilder
where J: ScheduledJob
{
let builder = ScheduleBuilder()
_ = self.storage.configuration.schedule(job, builder: builder)
return builder
}

func initialize() {
self.application.lifecycle.use(Lifecycle())
self.application.storage[Key.self] = .init(application)
}
}
}
6 changes: 3 additions & 3 deletions Sources/Jobs/JobsCommand.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vapor
import class NIOConcurrencyHelpers.Atomic
import class NIOConcurrencyHelpers.NIOAtomic
import class NIO.RepeatedTask

/// The command to start the Queue job
Expand Down Expand Up @@ -31,7 +31,7 @@ public final class JobsCommand: Command {
private var didShutdown: Bool


let isShuttingDown: Atomic<Bool>
let isShuttingDown: NIOAtomic<Bool>

private var eventLoopGroup: EventLoopGroup {
self.application.eventLoopGroup
Expand All @@ -42,7 +42,7 @@ public final class JobsCommand: Command {
self.application = application
self.jobTasks = []
self.scheduledTasks = [:]
self.isShuttingDown = .init(value: false)
self.isShuttingDown = .makeAtomic(value: false)
self.signalSources = []
self.didShutdown = false
self.lock = .init()
Expand Down
91 changes: 0 additions & 91 deletions Sources/Jobs/JobsProvider.swift

This file was deleted.

17 changes: 17 additions & 0 deletions Sources/Jobs/Request+Jobs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Foundation
import Vapor
import NIO

extension Request {
public var jobs: JobsQueue {
self.jobs(.default)
}

public func jobs(_ queue: JobsQueueName) -> JobsQueue {
self.application.jobs.queue(
queue,
logger: self.logger,
on: self.eventLoop
)
}
}
3 changes: 0 additions & 3 deletions Tests/JobsTests/JobsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ final class JobsTests: XCTestCase {
func testVaporIntegration() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.use(Jobs.self)
app.jobs.use(custom: TestDriver())

let promise = app.eventLoopGroup.next().makePromise(of: String.self)
Expand Down Expand Up @@ -40,8 +39,6 @@ final class JobsTests: XCTestCase {
let app = Application(.testing)
defer { app.shutdown() }

app.use(Jobs.self)

// yearly
app.jobs.schedule(Cleanup())
.yearly()
Expand Down

0 comments on commit d0cdfad

Please sign in to comment.