-
-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ScheduledJob management [Draft] #74
Comments
Just to make sure I'm understanding the question right, are you talking about scheduling a new job on the fly, not at startup? If so, then yes, I don't believe that's possible right now since we capture all of the scheduled jobs on startup. |
Yep, I mean on the fly. Probably there is already a workaround: import Queues
import Vapor
extension Application.Queues {
func schedule(_ task: Task) -> TaskContainer { .init(task, schedule(task)) }
}
final class TaskContainer {
let execute: ScheduleBuilder
let task: Task
init(_ task: Task, _ scheduleBuilder: ScheduleBuilder) {
self.task = task
self.execute = scheduleBuilder
}
func store(in variable: inout TaskContainer) {
variable = self
}
func store<Key: StorageKey>(in storage: inout Storage, forKey key: Key.Type)
where Key.Value == TaskContainer {
storage[key] = self
}
}
final class Task: ScheduledJob {
private var _action: (() -> Void)?
private let lock: Lock = .init()
private init(action: (() -> Void)?) {
self._action = action
}
init(action: @escaping () -> Void) {
self._action = action
}
func cancel() {
lock.withLock {
self._action = .none
}
}
func `override`(with task: Task) {
lock.withLock {
self._action = task._action
}
}
@inlinable
func run(context: QueueContext) -> EventLoopFuture<Void> {
_action?()
return context.eventLoop.future(())
}
@inlinable
static func empty() -> Task { .init(action: .none) }
} and if it works maybe it would be nice to have some kind of manager object out-of-the-box. 🌚 Usage // initial setup
app.queues
.schedule(.sendPushnotifications(app))
.store(in: &app.storage, forKey: Task.StorageKeys.SendPushNotifications.self)
.execute.weekly().on(.monday).at(.noon)
// edits
app.storage[Task.StorageKeys.SendPushNotifications.self].map { container in
container.execute.minutely().at(0)
}
app.storage[Task.StorageKeys.SendPushNotifications.self].map { container in
container.task.cancel()
}
app.storage[Task.StorageKeys.SendPushNotifications.self].map { container in
container.task.override(with: .sendPushNotifications(app))
} |
+1 for this functionality. |
Now it's not possible to edit the schedule of a job or to remove a job from the queue.
The text was updated successfully, but these errors were encountered: