From d23dc6f6af0515ccf2e0696c95c2ff2a414f72cd Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 14 Mar 2019 20:09:34 -0700 Subject: [PATCH] Allow to run future before server service initialization --- actix-server/CHANGES.md | 7 ++++ actix-server/src/builder.rs | 2 +- .../src/{service_config.rs => config.rs} | 36 +++++++++++++++++-- actix-server/src/lib.rs | 4 +-- 4 files changed, 43 insertions(+), 6 deletions(-) rename actix-server/src/{service_config.rs => config.rs} (84%) diff --git a/actix-server/CHANGES.md b/actix-server/CHANGES.md index 92a9a31f..968d905e 100644 --- a/actix-server/CHANGES.md +++ b/actix-server/CHANGES.md @@ -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 diff --git a/actix-server/src/builder.rs b/actix-server/src/builder.rs index 99f2754f..aac8064a 100644 --- a/actix-server/src/builder.rs +++ b/actix-server/src/builder.rs @@ -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}; diff --git a/actix-server/src/service_config.rs b/actix-server/src/config.rs similarity index 84% rename from actix-server/src/service_config.rs rename to actix-server/src/config.rs index 20e7adc6..8f360e14 100644 --- a/actix-server/src/service_config.rs +++ b/actix-server/src/config.rs @@ -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, services: HashMap, + onstart: Vec>>, } 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(&mut self, name: &str, service: F) where F: IntoNewService, @@ -191,6 +213,14 @@ impl ServiceRuntime { panic!("Unknown service: {:?}", name); } } + + /// Execute future before services initialization. + pub fn spawn(&mut self, fut: F) + where + F: Future + 'static, + { + self.onstart.push(Box::new(fut)) + } } type BoxedNewService = Box< diff --git a/actix-server/src/lib.rs b/actix-server/src/lib.rs index 2b0beb49..f9eb5721 100644 --- a/actix-server/src/lib.rs +++ b/actix-server/src/lib.rs @@ -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)]