use super::ENV_HANDOVER_PIPE;
use crate::pipes::FdStringExt;
use async_trait::async_trait;
use std::env;
use std::io;
use std::pin::Pin;
use tokio::fs::File;
use tokio::io::{AsyncRead, AsyncWrite};
pub type PipeReader = Pin<Box<dyn AsyncRead + Send>>;
pub type PipeWriter = Pin<Box<dyn AsyncWrite + Send>>;
#[async_trait]
pub trait LifecycleHandler: Send {
async fn send_to_new_process(&mut self, _write_pipe: PipeWriter) -> io::Result<()> {
Ok(())
}
async fn pre_new_process(&mut self) {}
async fn new_process_failed(&mut self) {}
}
pub struct NullLifecycleHandler;
impl LifecycleHandler for NullLifecycleHandler {}
pub fn receive_from_old_process() -> Option<PipeReader> {
if let Ok(handover_fd) = env::var(ENV_HANDOVER_PIPE) {
unsafe { File::from_fd_string(&handover_fd) }
.ok()
.map(|x| Box::pin(x) as PipeReader)
} else {
None
}
}