mirror of https://github.com/breard-r/acmed.git
				
				
			
			
			
				Browse Source
			
			
			Rewrite the main event loop using async
			
				
		Rewrite the main event loop using async
	
		
	
			
				Manual threads have some huge drawbacks and are therefore not well suited for this task. Using async with a multi-threaded runtime, however, does not have those drawbacks and keep the advantage of a multi-threaded environment. This is only the first part of the switch to async, the next step being to use it in file operation, HTTP requests and sleeps.pull/88/head
				 5 changed files with 130 additions and 94 deletions
			
			
		- 
					26README.md
 - 
					3acmed/Cargo.toml
 - 
					65acmed/src/certificate_manager.rs
 - 
					19acmed/src/main.rs
 - 
					111acmed/src/main_event_loop.rs
 
@ -0,0 +1,65 @@ | 
				
			|||
use crate::acme_proto::request_certificate;
 | 
				
			|||
use crate::certificate::Certificate;
 | 
				
			|||
use crate::logs::HasLogger;
 | 
				
			|||
use crate::{AccountSync, EndpointSync};
 | 
				
			|||
use std::time::Duration;
 | 
				
			|||
use tokio::time::sleep;
 | 
				
			|||
 | 
				
			|||
#[derive(Clone, Debug)]
 | 
				
			|||
pub struct CertificateManager {
 | 
				
			|||
	cert: Certificate,
 | 
				
			|||
}
 | 
				
			|||
 | 
				
			|||
impl CertificateManager {
 | 
				
			|||
	pub fn new(cert: Certificate) -> Self {
 | 
				
			|||
		Self { cert }
 | 
				
			|||
	}
 | 
				
			|||
 | 
				
			|||
	pub fn get_id(&self) -> String {
 | 
				
			|||
		self.cert.get_id()
 | 
				
			|||
	}
 | 
				
			|||
 | 
				
			|||
	pub fn get_account_name(&self) -> String {
 | 
				
			|||
		self.cert.account_name.clone()
 | 
				
			|||
	}
 | 
				
			|||
 | 
				
			|||
	pub fn get_endpoint_name(&self) -> String {
 | 
				
			|||
		self.cert.endpoint_name.clone()
 | 
				
			|||
	}
 | 
				
			|||
 | 
				
			|||
	pub async fn renew(
 | 
				
			|||
		&mut self,
 | 
				
			|||
		account_s: AccountSync,
 | 
				
			|||
		endpoint_s: EndpointSync,
 | 
				
			|||
	) -> (&mut Self, AccountSync, EndpointSync) {
 | 
				
			|||
		loop {
 | 
				
			|||
			match self.cert.should_renew() {
 | 
				
			|||
				Ok(true) => break,
 | 
				
			|||
				Ok(false) => {}
 | 
				
			|||
				Err(e) => {
 | 
				
			|||
					self.cert.warn(&e.message);
 | 
				
			|||
				}
 | 
				
			|||
			}
 | 
				
			|||
			sleep(Duration::from_secs(crate::DEFAULT_SLEEP_TIME)).await;
 | 
				
			|||
		}
 | 
				
			|||
		let mut account = account_s.write().await;
 | 
				
			|||
		let mut endpoint = endpoint_s.write().await;
 | 
				
			|||
		let (status, is_success) =
 | 
				
			|||
			match request_certificate(&self.cert, &mut endpoint, &mut account) {
 | 
				
			|||
				Ok(_) => ("success".to_string(), true),
 | 
				
			|||
				Err(e) => {
 | 
				
			|||
					let e = e.prefix("unable to renew the certificate");
 | 
				
			|||
					self.cert.warn(&e.message);
 | 
				
			|||
					(e.message, false)
 | 
				
			|||
				}
 | 
				
			|||
			};
 | 
				
			|||
		match self.cert.call_post_operation_hooks(&status, is_success) {
 | 
				
			|||
			Ok(_) => {}
 | 
				
			|||
			Err(e) => {
 | 
				
			|||
				let e = e.prefix("post-operation hook error");
 | 
				
			|||
				self.cert.warn(&e.message);
 | 
				
			|||
			}
 | 
				
			|||
		};
 | 
				
			|||
		(self, account_s.clone(), endpoint_s.clone())
 | 
				
			|||
	}
 | 
				
			|||
}
 | 
				
			|||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue