1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 00:01:11 +01:00

Allow to run future before server service initialization

This commit is contained in:
Nikolay Kim 2019-03-14 20:09:34 -07:00
parent f3aa48309f
commit d23dc6f6af
4 changed files with 43 additions and 6 deletions

View File

@ -1,5 +1,12 @@
# Changes
## [0.4.1] - 2019-03-13
### Added
* Allow to run future before server service initialization
## [0.4.0] - 2019-03-12
### Changed

View File

@ -12,8 +12,8 @@ use num_cpus;
use tokio_timer::sleep;
use crate::accept::{AcceptLoop, AcceptNotify, Command};
use crate::config::{ConfiguredService, ServiceConfig};
use crate::server::{Server, ServerCommand};
use crate::service_config::{ConfiguredService, ServiceConfig};
use crate::services::{InternalServiceFactory, ServiceFactory, StreamNewService};
use crate::signals::{Signal, Signals};
use crate::worker::{self, Worker, WorkerAvailability, WorkerClient};

View File

@ -121,9 +121,25 @@ impl InternalServiceFactory for ConfiguredService {
fut.push(ns.new_service(&config).map(move |service| (token, service)));
}
Box::new(join_all(fut).map_err(|e| {
error!("Can not construct service: {:?}", e);
}))
// on start futures
if rt.onstart.is_empty() {
Box::new(join_all(fut).map_err(|e| {
error!("Can not construct service: {:?}", e);
}))
} else {
// run onstart future and then construct services
Box::new(
join_all(rt.onstart)
.map_err(|e| {
error!("Can not construct service: {:?}", e);
})
.and_then(move |_| {
join_all(fut).map_err(|e| {
error!("Can not construct service: {:?}", e);
})
}),
)
}
}
}
@ -153,6 +169,7 @@ fn not_configured(_: &mut ServiceRuntime) {
pub struct ServiceRuntime {
names: HashMap<String, Token>,
services: HashMap<Token, BoxedNewService>,
onstart: Vec<Box<Future<Item = (), Error = ()>>>,
}
impl ServiceRuntime {
@ -160,6 +177,7 @@ impl ServiceRuntime {
ServiceRuntime {
names,
services: HashMap::new(),
onstart: Vec::new(),
}
}
@ -171,6 +189,10 @@ impl ServiceRuntime {
}
}
/// Register service.
///
/// Name of the service must be registered during configuration stage with
/// *ServiceConfig::bind()* or *ServiceConfig::listen()* methods.
pub fn service<T, F>(&mut self, name: &str, service: F)
where
F: IntoNewService<T, ServerConfig>,
@ -191,6 +213,14 @@ impl ServiceRuntime {
panic!("Unknown service: {:?}", name);
}
}
/// Execute future before services initialization.
pub fn spawn<F>(&mut self, fut: F)
where
F: Future<Item = (), Error = ()> + 'static,
{
self.onstart.push(Box::new(fut))
}
}
type BoxedNewService = Box<

View File

@ -2,9 +2,9 @@
mod accept;
mod builder;
mod config;
mod counter;
mod server;
mod service_config;
mod services;
mod signals;
pub mod ssl;
@ -13,8 +13,8 @@ mod worker;
pub use actix_server_config::{Io, Protocol, ServerConfig};
pub use self::builder::ServerBuilder;
pub use self::config::{ServiceConfig, ServiceRuntime};
pub use self::server::Server;
pub use self::service_config::{ServiceConfig, ServiceRuntime};
pub use self::services::ServiceFactory;
#[doc(hidden)]