Update tokio-zmq, optionally use futures-zmq instead

This commit is contained in:
asonix 2018-11-16 18:09:53 -06:00
parent a877b5c9e8
commit 960c0235c4
13 changed files with 126 additions and 109 deletions

View file

@ -16,7 +16,7 @@ members = [
] ]
[features] [features]
default = ["jobs-actix", "jobs-server-tokio", "jobs-tokio"] default = ["jobs-actix", "jobs-server-tokio", "jobs-server-tokio/tokio-zmq", "jobs-tokio"]
[dependencies.jobs-actix] [dependencies.jobs-actix]
version = "0.1" version = "0.1"

View file

@ -1 +1 @@
RUST_LOG=info RUST_LOG=server_jobs_example=info

View file

@ -12,9 +12,19 @@ serde = "1.0"
serde_json = "1.0" serde_json = "1.0"
tokio = "0.1" tokio = "0.1"
tokio-threadpool = "0.1" tokio-threadpool = "0.1"
tokio-zmq = "0.6.1"
zmq = "0.8" zmq = "0.8"
[features]
default = ["tokio-zmq"]
[dependencies.jobs-core] [dependencies.jobs-core]
version = "0.1" version = "0.1"
path = "../jobs-core" path = "../jobs-core"
[dependencies.tokio-zmq]
version = "0.8"
optional = true
[dependencies.futures-zmq]
version = "0.3"
optional = true

View file

@ -1,8 +1,3 @@
#[macro_use]
extern crate failure;
#[macro_use]
extern crate log;
use failure::Error; use failure::Error;
mod server; mod server;

View file

@ -4,9 +4,10 @@ use std::{
sync::Arc, sync::Arc,
}; };
use failure::Error; use failure::{Error, Fail};
use futures::{future::poll_fn, Future}; use futures::{future::poll_fn, Future};
use jobs_core::Storage; use jobs_core::Storage;
use log::{error, info};
use tokio_threadpool::blocking; use tokio_threadpool::blocking;
use zmq::Context; use zmq::Context;

View file

@ -1,8 +1,12 @@
use std::{collections::BTreeMap, sync::Arc, time::Duration}; use std::{collections::BTreeMap, sync::Arc, time::Duration};
use failure::Error; use failure::Error;
use futures::{future::lazy, Future, Stream}; use futures::{Future, Stream};
#[cfg(feature = "futures-zmq")]
use futures_zmq::{prelude::*, Multipart, Rep};
use log::{error, info};
use tokio::timer::Delay; use tokio::timer::Delay;
#[cfg(feature = "tokio-zmq")]
use tokio_zmq::{prelude::*, Multipart, Rep}; use tokio_zmq::{prelude::*, Multipart, Rep};
use zmq::Message; use zmq::Message;
@ -85,11 +89,10 @@ impl ResetPortMapConfig {
} }
fn build(self) -> impl Future<Item = (), Error = Error> { fn build(self) -> impl Future<Item = (), Error = Error> {
lazy(|| { Rep::builder(self.config.context.clone())
let rep = Rep::builder(self.config.context.clone())
.bind(&self.address) .bind(&self.address)
.build()?; .build()
.map(|rep| {
let config = PortMapConfig { let config = PortMapConfig {
rep, rep,
address: self.address, address: self.address,
@ -98,8 +101,7 @@ impl ResetPortMapConfig {
}; };
tokio::spawn(config.run()); tokio::spawn(config.run());
Ok(())
}) })
.from_err()
} }
} }

View file

@ -1,13 +1,14 @@
use std::{sync::Arc, time::Duration}; use std::{sync::Arc, time::Duration};
use failure::Error; use failure::{Error, Fail};
use futures::{ use futures::{future::poll_fn, Future, Stream};
future::{lazy, poll_fn}, #[cfg(feature = "futures-zmq")]
Future, Stream, use futures_zmq::{prelude::*, Multipart, Pull};
};
use jobs_core::{JobInfo, Storage}; use jobs_core::{JobInfo, Storage};
use log::{error, info, trace};
use tokio::timer::Delay; use tokio::timer::Delay;
use tokio_threadpool::blocking; use tokio_threadpool::blocking;
#[cfg(feature = "tokio-zmq")]
use tokio_zmq::{prelude::*, Multipart, Pull}; use tokio_zmq::{prelude::*, Multipart, Pull};
use crate::server::{coerce, Config}; use crate::server::{coerce, Config};
@ -112,11 +113,10 @@ impl ResetPullConfig {
} }
fn build(self) -> impl Future<Item = (), Error = Error> { fn build(self) -> impl Future<Item = (), Error = Error> {
lazy(|| { Pull::builder(self.config.context.clone())
let puller = Pull::builder(self.config.context.clone())
.bind(&self.address) .bind(&self.address)
.build()?; .build()
.map(|puller| {
let config = PullConfig { let config = PullConfig {
puller, puller,
address: self.address, address: self.address,
@ -125,8 +125,7 @@ impl ResetPullConfig {
}; };
tokio::spawn(config.run()); tokio::spawn(config.run());
Ok(())
}) })
.from_err()
} }
} }

View file

@ -1,14 +1,14 @@
use std::{sync::Arc, time::Duration}; use std::{sync::Arc, time::Duration};
use failure::Error; use failure::Error;
use futures::{ use futures::{future::poll_fn, stream::iter_ok, Future, Stream};
future::{lazy, poll_fn}, #[cfg(feature = "futures-zmq")]
stream::iter_ok, use futures_zmq::{prelude::*, Multipart, Push};
Future, Stream,
};
use jobs_core::{JobInfo, Storage}; use jobs_core::{JobInfo, Storage};
use log::{error, info};
use tokio::timer::{Delay, Interval}; use tokio::timer::{Delay, Interval};
use tokio_threadpool::blocking; use tokio_threadpool::blocking;
#[cfg(feature = "tokio-zmq")]
use tokio_zmq::{prelude::*, Multipart, Push}; use tokio_zmq::{prelude::*, Multipart, Push};
use zmq::Message; use zmq::Message;
@ -136,12 +136,11 @@ impl ResetPushConfig {
} }
fn build(self) -> impl Future<Item = (), Error = Error> { fn build(self) -> impl Future<Item = (), Error = Error> {
lazy(|| {
info!("Building and spawning new server"); info!("Building and spawning new server");
let pusher = Push::builder(self.config.context.clone()) Push::builder(self.config.context.clone())
.bind(&self.address) .bind(&self.address)
.build()?; .build()
.map(|pusher| {
let config = PushConfig { let config = PushConfig {
pusher, pusher,
address: self.address, address: self.address,
@ -151,8 +150,7 @@ impl ResetPushConfig {
}; };
tokio::spawn(config.run()); tokio::spawn(config.run());
Ok(())
}) })
.from_err()
} }
} }

View file

@ -3,6 +3,7 @@ use std::{sync::Arc, time::Duration};
use failure::Error; use failure::Error;
use futures::{future::poll_fn, Future, Stream}; use futures::{future::poll_fn, Future, Stream};
use jobs_core::Storage; use jobs_core::Storage;
use log::{error, info};
use tokio::timer::{Delay, Interval}; use tokio::timer::{Delay, Interval};
use tokio_threadpool::blocking; use tokio_threadpool::blocking;

View file

@ -2,7 +2,11 @@ use std::sync::Arc;
use failure::Error; use failure::Error;
use futures::{future::IntoFuture, Future}; use futures::{future::IntoFuture, Future};
#[cfg(feature = "futures-zmq")]
use futures_zmq::{prelude::*, Push};
use jobs_core::JobInfo; use jobs_core::JobInfo;
use log::{debug, trace};
#[cfg(feature = "tokio-zmq")]
use tokio_zmq::{prelude::*, Push}; use tokio_zmq::{prelude::*, Push};
use zmq::{Context, Message}; use zmq::{Context, Message};

View file

@ -1,12 +1,16 @@
use std::{sync::Arc, time::Duration}; use std::{sync::Arc, time::Duration};
use failure::Error; use failure::{Error, Fail};
use futures::{ use futures::{
future::{lazy, Either, IntoFuture}, future::{Either, IntoFuture},
Future, Stream, Future, Stream,
}; };
#[cfg(feature = "futures-zmq")]
use futures_zmq::{prelude::*, Multipart, Pull, Push};
use jobs_core::{JobInfo, Processors}; use jobs_core::{JobInfo, Processors};
use log::{error, info};
use tokio::timer::Delay; use tokio::timer::Delay;
#[cfg(feature = "tokio-zmq")]
use tokio_zmq::{prelude::*, Multipart, Pull, Push}; use tokio_zmq::{prelude::*, Multipart, Pull, Push};
use zmq::{Context, Message}; use zmq::{Context, Message};
@ -98,15 +102,15 @@ impl ResetWorker {
} }
fn build(self) -> impl Future<Item = (), Error = Error> { fn build(self) -> impl Future<Item = (), Error = Error> {
lazy(|| { Push::builder(self.context.clone())
let push = Push::builder(self.context.clone())
.connect(&self.push_address) .connect(&self.push_address)
.build()?; .build()
.join(
let pull = Pull::builder(self.context.clone()) Pull::builder(self.context.clone())
.connect(&self.pull_address) .connect(&self.pull_address)
.build()?; .build(),
)
.map(|(push, pull)| {
let config = Worker { let config = Worker {
push, push,
pull, pull,
@ -118,9 +122,8 @@ impl ResetWorker {
}; };
tokio::spawn(config.run()); tokio::spawn(config.run());
Ok(())
}) })
.from_err()
} }
} }

View file

@ -1,7 +1,9 @@
use std::{collections::BTreeMap, sync::Arc}; use std::{collections::BTreeMap, sync::Arc};
use failure::Fail;
use futures::Future; use futures::Future;
use jobs_core::{Processor, Processors}; use jobs_core::{Processor, Processors};
use log::{error, info};
use zmq::Context; use zmq::Context;
mod config; mod config;

View file

@ -1,7 +1,10 @@
use std::{collections::BTreeMap, sync::Arc}; use std::{collections::BTreeMap, sync::Arc};
use failure::Error; use failure::{Error, Fail};
use futures::{future::lazy, Future}; use futures::Future;
#[cfg(feature = "futures-zmq")]
use futures_zmq::{prelude::*, Req};
#[cfg(feature = "tokio-zmq")]
use tokio_zmq::{prelude::*, Req}; use tokio_zmq::{prelude::*, Req};
use zmq::{Context, Message}; use zmq::{Context, Message};
@ -12,11 +15,10 @@ impl PortMap {
address: String, address: String,
context: Arc<Context>, context: Arc<Context>,
) -> impl Future<Item = BTreeMap<String, usize>, Error = Error> { ) -> impl Future<Item = BTreeMap<String, usize>, Error = Error> {
lazy(move || { Req::builder(context.clone())
let req = Req::builder(context.clone()).connect(&address).build()?; .connect(&address)
.build()
Ok(req) .from_err()
})
.and_then(|req| { .and_then(|req| {
Message::from_slice(b"h") Message::from_slice(b"h")
.map_err(Error::from) .map_err(Error::from)