actix-redis-jobs

Crates.ioactix-redis-jobs
lib.rsactix-redis-jobs
version0.2.0-beta.1
sourcesrc
created_at2020-04-25 08:17:56.151685
updated_at2021-03-22 08:43:21.552371
descriptionSimple and reliable background processing for Rust using Actix and Redis
homepage
repositoryhttps://github.com/geofmureithi/actix-jobs.git
max_upload_size
id233875
size88,136
Geoffrey Mureithi (geofmureithi)

documentation

https://docs.rs/actix-redis-jobs

README

Actix-redis-jobs

Simple and reliable background processing for Rust using Actix and Redis

Getting Started

To get started, just add to Cargo.toml

[dependencies]
actix-redis-jobs = { version = "0.2.0-beta.0" }

Prerequisites

A running redis server is required. You can quickly use docker:

docker run --name some-redis -d redis

Usage

use actix::prelude::*;
use log::info;
use serde::{Deserialize, Serialize};
use futures::future::BoxFuture;

use actix_redis_jobs::{
    JobContext, JobHandler, JobResult, Producer, RedisConsumer, RedisStorage, ScheduleJob,
    WorkManager,
};

#[derive(Serialize, Deserialize, Message, Clone)]
#[rtype(result = "()")]
enum Math {
    Sum(isize, isize),
    Multiply(isize, isize),
}

impl JobHandler for Math {
    fn handle(&self, _ctx: &JobContext) -> BoxFuture<JobResult> {
        let fut = async move {
            match self {
                Math::Sum(first, second) => {
                    info!(
                        "Sum result for {} and {} is {}",
                        first,
                        second,
                        first + second
                    );
                    JobResult::Result(Ok(()))
                }
                Math::Multiply(first, second) => {
                    info!(
                        "Multiply result for {} and {} is {}",
                        first,
                        second,
                        first * second
                    );
                    JobResult::Result(Ok(()))
                }
            }
        };
        Box::pin(fut)
    }
}

#[actix_rt::main]
async fn main() {
    std::env::set_var("RUST_LOG", "info");
    env_logger::init();
    let storage = RedisStorage::new("redis://127.0.0.1/");
    let producer = Producer::start(&storage, "math");
    let sum = Math::Sum(1, 2);
    let multiply = Math::Multiply(9, 8);
    let scheduled = ScheduleJob::new(sum).in_minutes(1);
    producer.do_send(scheduled);
    producer.do_send(multiply);

    WorkManager::create(move |worker| {
        worker.consumer(RedisConsumer::<Math>::new(&storage, "math").workers(2))
    })
    .run()
    .await;
}

Built With

  • actix - Actor framework for Rust
  • redis-rs - Redis library for rust

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

  • Inspiration: This project is inspired by Curlyq which is written in GoLang
Commit count: 53

cargo fmt