Struct feattle::BackgroundSync
source · pub struct BackgroundSync<F> { /* private fields */ }
Expand description
Spawn a tokio task to poll Feattles::reload()
continuously
A feattles instance will only ask the persistence layer for the current values when the
Feattles::reload()
method is called. This type would do so regularly for you, until the
Feattles
instance is dropped.
Example
use feattle_core::{feattles, Feattles};
use feattle_sync::BackgroundSync;
use feattle_core::persist::NoPersistence;
use std::sync::Arc;
feattles! {
struct MyToggles {
a: bool,
}
}
// `NoPersistence` here is just a mock for the sake of the example
let toggles = Arc::new(MyToggles::new(Arc::new(NoPersistence)));
BackgroundSync::new(&toggles).start().await;
Implementations§
source§impl<F> BackgroundSync<F>
impl<F> BackgroundSync<F>
sourcepub fn new(feattles: &Arc<F>) -> BackgroundSync<F>
pub fn new(feattles: &Arc<F>) -> BackgroundSync<F>
Create a new poller for the given feattles instance. It will call Arc::downgrade()
to
detect when the value is dropped.
sourcepub fn interval(&mut self, value: Duration) -> &mut BackgroundSync<F>
pub fn interval(&mut self, value: Duration) -> &mut BackgroundSync<F>
Set both Self::ok_interval
and Self::err_interval
sourcepub fn ok_interval(&mut self, value: Duration) -> &mut BackgroundSync<F>
pub fn ok_interval(&mut self, value: Duration) -> &mut BackgroundSync<F>
After a successful reload, will wait for this long before starting the next one. By default this is 30 seconds.
sourcepub fn err_interval(&mut self, value: Duration) -> &mut BackgroundSync<F>
pub fn err_interval(&mut self, value: Duration) -> &mut BackgroundSync<F>
After a failed reload, will wait for this long before starting the next one. By default this is 60 seconds.
source§impl<F> BackgroundSync<F>where
F: Feattles + Sync + Send + 'static,
impl<F> BackgroundSync<F>where F: Feattles + Sync + Send + 'static,
sourcepub fn spawn(self) -> JoinHandle<()>
👎Deprecated: use start_sync()
that will try a first update right away
pub fn spawn(self) -> JoinHandle<()>
start_sync()
that will try a first update right awaySpawn a new tokio task, returning its handle. Usually you do not want to anything with the returned handle, since the task will run by itself until the feattles instance gets dropped.
Operational logs are generated with the crate log
.
sourcepub async fn start(
self
) -> impl Future<Output = Option<Box<dyn Error + Sync + Send, Global>>>
pub async fn start( self ) -> impl Future<Output = Option<Box<dyn Error + Sync + Send, Global>>>
Start the sync operation by executing an update right now and then spawning a new tokio task.
This call will block until the first update returns. If it fails, the obtained error will be returned.
Note that the return type is Option<_>
and not Result<_>
, to avoid confusion: even if
the first update fails, the sync process will continue in the background.
The tokio task will run by itself until the feattles instance gets dropped.
Operational logs are generated with the crate log
.