use std::sync::Arc;
use std::time::Duration;
use crate::{Result, SDK_METADATA};
use eppo_core::background::BackgroundThread;
use eppo_core::configuration_fetcher::{ConfigurationFetcher, ConfigurationFetcherConfig};
use eppo_core::configuration_poller::start_configuration_poller;
use eppo_core::configuration_store::ConfigurationStore;
#[cfg(doc)]
use eppo_core::Error;
pub(crate) struct PollerThreadConfig {
pub(crate) store: Arc<ConfigurationStore>,
pub(crate) base_url: String,
pub(crate) api_key: String,
}
pub struct PollerThread {
thread: BackgroundThread,
poller: eppo_core::configuration_poller::ConfigurationPoller,
}
impl PollerThread {
pub(crate) fn start(config: PollerThreadConfig) -> Result<PollerThread> {
let fetcher = ConfigurationFetcher::new(ConfigurationFetcherConfig {
base_url: config.base_url,
api_key: config.api_key,
sdk_metadata: SDK_METADATA.clone(),
});
let thread = BackgroundThread::start()?;
let poller = start_configuration_poller(
thread.runtime(),
fetcher,
config.store,
eppo_core::configuration_poller::ConfigurationPollerConfig::default(),
);
Ok(PollerThread { thread, poller })
}
#[deprecated]
pub fn wait_for_configuration(&self) -> Result<()> {
self.thread
.runtime()
.async_runtime
.block_on(self.poller.wait_for_configuration())
}
pub fn wait_for_configuration_timeout(&self, duration: Duration) -> Result<()> {
self.thread.runtime().async_runtime.block_on(async move {
tokio::time::timeout(duration, self.poller.wait_for_configuration())
.await
.map_err(|_| crate::Error::Timeout)?
})
}
pub fn stop(&self) {
self.thread.kill();
}
pub fn shutdown(self) -> Result<()> {
self.thread.graceful_shutdown();
Ok(())
}
}