JobQueue
一个具有简单API的持久性任务队列,不依赖Operation
/OperationQueue
,无存储依赖,支持手动执行顺序,每种任务类型的并发限制,延迟任务,等等。
特性
- 无
Operation
或OperationQueue
依赖 - 每种任务类型的并发性
- 手动处理顺序
- 延迟任务
- 暂停的任务
- 计划的任务
- 重复的任务
- 速率限制
- 自定义执行排序
- 存储无关
- 内存存储实现
- YapDatabase存储实现
- Couchbase Lite存储实现
- Core Data存储实现
- Realm存储实现
依赖项
ReactiveSwift
我并不想在这里做出艰难的选择,但我没有时间想出一个更具灵活性的实现方案。理想状态下,我希望对其进行重构,使其有一个核心实现,通过实现异步行为的适配器与之交互,以便为GCD、RxSwift、ReactiveSwift、Combine等编写适配器。
安装说明
为了支持CouchbaseLite存储,您必须使用Cocoapods进行安装。
路线图
目前这是alpha级别的代码。在使用于生产之前需要更多的测试和性能分析。
优先级包括
- 实现剩余的存储提供者(YapDB & Realm)
- 测试
- 性能分析
如何使用
创建任务
struct Email: Codable {
var recipientAddress: String
var senderAddress: String
var subject: String
var body: String
}
// If you inherit from the `DefaultJob<T>` class, all you need to do is override and implement the `process(?)` function
class SendAnEmail: DefaultJob<Email> {
override func process(
details: JobDetails,
payload: Email,
queue: JobQueueProtocol,
done: @escaping JobCompletion
) {
send(email: payload) { error in
if let error = error {
done(.failure(error))
} else {
done(.success(()))
}
}
}
}
创建作业队列实例
let schedulers = JobQueueSchedulers()
let storage = JobQueueInMemoryStorage()
let queue = JobQueue(
name: "email",
schedulers: schedulers,
storage: storage
)
// Register the processor and allow for processing up to 4 concurrent SendEmail jobs
queue.register(SendAnEmail.self, concurrency: 4)
// Resume the queue
queue.resume().startWithCompleted {
print("Resumed queue.")
}
添加作业
let email = Email(
recipient: "[email protected]",
sender: "[email protected]",
subject: "Job Queue question",
body: "Do you even use a job queue?"
)
let details = try! JobDetails(SendAnEmail.self, id: "email 1", queueName: queue.name, payload: email)
queue.store(details)
.startWithCompleted {
print("Stored a job")
}