#mongo-db #job-queue #queue #job #job-scheduler #scheduler #jobs

aide-de-camp-mongodb

MongoDB对aide-de-camp的Queue trait的实现

2个不稳定版本

0.3.0 2024年3月26日
0.2.0 2023年7月30日

#1137 in 数据库接口

Download history 11/week @ 2024-04-03 1/week @ 2024-06-05 1/week @ 2024-06-12

52 每月下载次数

MIT/Apache

26KB
587

aide-de-camp-mongodb

crates.io docs.rs CI Tests License: Apache-2.0 License: MIT

aide-de-camp提供后端MongoDB支持的作业队列实现。

示例

use aide_de_camp::prelude::{
    CancellationToken, Duration, JobProcessor, JobRunner, Queue, RunnerOptions, RunnerRouter, Xid,
};
use aide_de_camp_mongodb::MongoDbQueue;
use async_trait::async_trait;

struct MyJob;

#[async_trait]
impl JobProcessor for MyJob {
    type Payload = Vec<u32>;
    type Error = anyhow::Error;

    async fn handle(
        &self,
        _jid: Xid,
        payload: Self::Payload,
        _cancellation_token: CancellationToken,
    ) -> Result<(), Self::Error> {
        println!("payload: {:?}", payload);
        Ok(())
    }

    fn name() -> &'static str {
        "my_job"
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let queue = MongoDbQueue::new("mongodb://127.0.0.1:27017/queues", None).await?;

    // Add job the queue to run next
    let _jid = queue.schedule::<MyJob>(vec![1, 2, 3], 0).await?;

    // First create a job processor and router
    let router = {
        let mut r = RunnerRouter::default();
        r.add_job_handler(MyJob);
        r
    };
    // Setup runner to at most 10 jobs concurrently
    let mut runner = JobRunner::new(queue, router, 10, RunnerOptions::default());
    // Poll the queue every second, this will block unless something went really wrong.
    // The future supplied as the second parameter will tell the server to shut down when it completes.
    runner
        .run_with_shutdown(Duration::seconds(1), async move {
            // To avoid blocking this doctest, run for 10 milliseconds, then initiate shutdown.
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
            // In a real application, you may want to wait for a CTRL+C event or something similar.
            // You could do this with tokio using the signal module: tokio::signal::ctrl_c().await.expect("failed to install CTRL+C signal handler");
        })
        .await?;
    Ok(())
}

许可证

我决定遵循与aide-de-camp相同的许可模式,因此请根据您的用例选择以下之一

Apache许可证2.0版(LICENSE-APACHE或http://www.apache.org/licenses/LICENSE-2.0)MIT许可证(LICENSE-MIT或http://opensource.org/licenses/MIT

依赖

~27–38MB
~708K SLoC