Understanding custom executers

I'm trying to figure out how one could benefit from writing custom executers. For this reason I'm writing a "queued" executer that would await for each job to fully finish before moving to the next.

final class QueueSerialExecutor: SerialExecutor {
    func enqueue(_ job: UnownedJob) {
        let uuid = UUID()
        print("start job \(uuid)")
        job._runSynchronously(on: asUnownedSerialExecutor())
        print("end job \(uuid)")
    }

    func asUnownedSerialExecutor() -> UnownedSerialExecutor {
        UnownedSerialExecutor(ordinary: self)
    }
}

But as expected, once the job hits a suspension point, _runSynchronously exits and there is no way to know if other jobs will be enqueued or not (i.e. as a result of a Task.wait). My question would be, considering UnownedJob is completely opaque (even _runSynchronously is marked as internal), how would one use a custom executer to manually manage job scheduling?

Custom executors are not currently a supported feature. When they are, there will be a function you can call on a job to run it.

Note that running a job synchronously during enqueue would not be a legal implementation.

5 Likes