4个版本 (2个破坏性版本)
新 0.3.0 | 2024年8月16日 |
---|---|
0.2.1 | 2024年8月11日 |
0.2.0 | 2024年8月11日 |
0.1.0 | 2024年8月11日 |
#253 in 异步
每月下载量 437次
36KB
663 行
Girlboss
Girlboss是一个简单的异步作业管理器,具有进度跟踪功能。
Girlboss允许您启动后台任务并监控它们的进度。您可以自行跟踪作业,或者使用提供的管理器通过任何ID查找作业。
例如,在Web服务器中,用户启动作业并定期检查其状态时,这可能非常有用。
作业和状态报告
您可以启动定期报告其进度的后台作业,然后您可以从另一个任务/线程中检查进度。
use std::time::Duration;
use girlboss::{Job, Monitor};
use tokio::time::sleep;
#[tokio::main]
async fn main() {
// Start a job.
let job = Job::start(long_running_task);
// Check the status of the job.
sleep(Duration::from_millis(50)).await;
assert_eq!(job.status().message(), "Computing the meaning of life...");
// Wait for the job to complete.
job.wait().await.expect("job failed");
assert_eq!(job.status().message(), "The meaning of life is 42");
// See how long the job took.
assert!(job.elapsed() >= Duration::from_millis(200));
}
/// A long running task that we want to run in the background.
async fn long_running_task(mon: Monitor) {
write!(mon, "Computing the meaning of life...");
sleep(Duration::from_millis(200)).await;
let meaning = 42;
write!(mon, "The meaning of life is {meaning}");
}
作业管理器
一个Girlboss
实例管理一组作业,允许您通过ID查找它们。它还保留作业在完成后的状态,直到被覆盖或清除。
您可以选择任何实现了Ord
的类型作为您的作业ID类型。在这里,我们选择了String
。
use std::time::Duration;
use girlboss::{Girlboss, Monitor};
use tokio::time::sleep;
#[tokio::main]
async fn main() {
// Create a manager with strings as job IDs.
let manager: Girlboss<String> = Girlboss::new();
// Start a job.
let job = manager.start("myJobId", long_running_task).await.unwrap();
// Wait for the job to complete.
let job_2 = manager.get("myJobId").await.expect("job not found");
job_2.wait().await.expect("job failed");
// Check the job status after it's finished.
let job_3 = manager.get("myJobId").await.expect("job not found");
assert_eq!(job_3.status().message(), "The meaning of life is 42");
// When we look up the job again, we get the same job back.
assert_eq!(job, job_2);
assert_eq!(job, job_3);
}
async fn long_running_task(mon: Monitor) {
write!(mon, "Computing the meaning of life...");
sleep(Duration::from_millis(200)).await;
let meaning = 42;
write!(mon, "The meaning of life is {meaning}");
}
错误处理
作业可以返回一个可选的错误,然后通过状态报告。
use girlboss::{Job, Monitor};
#[tokio::main]
async fn main() {
let job = Job::start(long_running_task);
// Wait for the job to finish, which returns an `Err` because it failed.
job.wait().await.unwrap_err();
// The status message provides more details about the error.
assert_eq!(job.status().message(), "The meaning of life could not be found");
assert_eq!(job.succeeded(), false);
}
async fn long_running_task(mon: Monitor) -> Result<(), &'static str> {
write!(mon, "Computing the meaning of life...");
// The error type can be anything that implements Display, which includes
// all std::error::Error implementors.
Err("The meaning of life could not be found")
}
许可证
依赖关系
~3–5MB
~84K SLoC