-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
132 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters