some cleanup

This commit is contained in:
dignifiedquire
2020-03-18 02:23:22 +01:00
parent 563b550f08
commit 94c6a01420
3 changed files with 117 additions and 125 deletions

View File

@@ -67,7 +67,8 @@ async fn main() {
println!("------ RUN ------"); println!("------ RUN ------");
ctx.run().await; ctx.run().await;
println!("sending a message"); println!("--- SENDING A MESSAGE ---");
let contact_id = Contact::create(&ctx, "dignifiedquire", "dignifiedquire@gmail.com") let contact_id = Contact::create(&ctx, "dignifiedquire", "dignifiedquire@gmail.com")
.await .await
.unwrap(); .unwrap();

View File

@@ -128,7 +128,12 @@ impl Context {
} }
pub async fn run(&self) { pub async fn run(&self) {
self.inner.scheduler.write().await.run(self.clone()).await let ctx = self.clone();
println!("RUN LOCK START");
let l = &mut *self.inner.scheduler.write().await;
println!("RUN LOCK AQ");
l.run(ctx);
println!("RUN LOCK DONE");
} }
pub async fn stop(&self) { pub async fn stop(&self) {

View File

@@ -40,32 +40,8 @@ impl Context {
} }
} }
impl Scheduler { async fn inbox_loop(ctx: Context, inbox_handlers: ImapConnectionHandlers) {
/// Start the scheduler, panics if it is already running. info!(ctx, "starting inbox loop");
pub async fn run(&mut self, ctx: Context) {
match self {
Scheduler::Stopped => {
let (
(
((inbox, inbox_handlers), (mvbox, mvbox_handlers)),
(sentbox, sentbox_handlers),
),
(smtp, smtp_handlers),
) = ImapConnectionState::new()
.join(ImapConnectionState::new())
.join(ImapConnectionState::new())
.join(SmtpConnectionState::new())
.await;
*self = Scheduler::Running {
inbox,
mvbox,
sentbox,
smtp,
};
let ctx1 = ctx.clone();
task::spawn(async move {
info!(ctx1, "starting inbox loop");
let ImapConnectionHandlers { let ImapConnectionHandlers {
mut connection, mut connection,
stop_receiver, stop_receiver,
@@ -73,49 +49,42 @@ impl Scheduler {
} = inbox_handlers; } = inbox_handlers;
let fut = async move { let fut = async move {
connection.connect_configured(&ctx1).await.unwrap(); connection.connect_configured(&ctx).await.unwrap();
loop { loop {
// TODO: correct value // TODO: correct value
let probe_network = false; let probe_network = false;
match job::load_next(&ctx1, Thread::Imap, probe_network) match job::load_next(&ctx, Thread::Imap, probe_network)
.timeout(Duration::from_millis(200)) .timeout(Duration::from_millis(200))
.await .await
{ {
Ok(Some(job)) => { Ok(Some(job)) => {
job::perform_job( job::perform_job(&ctx, job::Connection::Inbox(&mut connection), job).await;
&ctx1,
job::Connection::Inbox(&mut connection),
job,
)
.await;
} }
Ok(None) | Err(async_std::future::TimeoutError { .. }) => { Ok(None) | Err(async_std::future::TimeoutError { .. }) => {
let watch_folder = let watch_folder = get_watch_folder(&ctx, "configured_inbox_folder")
get_watch_folder(&ctx1, "configured_inbox_folder")
.await .await
.ok_or_else(|| { .ok_or_else(|| Error::WatchFolderNotFound("not-set".to_string()))
Error::WatchFolderNotFound("not-set".to_string())
})
.unwrap(); .unwrap();
// fetch // fetch
connection.fetch(&ctx1, &watch_folder).await.unwrap_or_else( connection
|err| { .fetch(&ctx, &watch_folder)
error!(ctx1, "{}", err); .await
}, .unwrap_or_else(|err| {
); error!(ctx, "{}", err);
});
// idle // idle
if connection.can_idle() { if connection.can_idle() {
connection connection
.idle(&ctx1, Some(watch_folder)) .idle(&ctx, Some(watch_folder))
.await .await
.unwrap_or_else(|err| { .unwrap_or_else(|err| {
error!(ctx1, "{}", err); error!(ctx, "{}", err);
}); });
} else { } else {
connection.fake_idle(&ctx1, Some(watch_folder)).await; connection.fake_idle(&ctx, Some(watch_folder)).await;
} }
} }
} }
@@ -124,15 +93,10 @@ impl Scheduler {
fut.race(stop_receiver.recv()).await; fut.race(stop_receiver.recv()).await;
shutdown_sender.send(()).await; shutdown_sender.send(()).await;
}); }
// TODO: mvbox async fn smtp_loop(ctx: Context, smtp_handlers: SmtpConnectionHandlers) {
info!(ctx, "starting smtp loop");
// TODO: sentbox
let ctx1 = ctx.clone();
task::spawn(async move {
info!(ctx1, "starting smtp loop");
let SmtpConnectionHandlers { let SmtpConnectionHandlers {
mut connection, mut connection,
stop_receiver, stop_receiver,
@@ -144,17 +108,12 @@ impl Scheduler {
loop { loop {
// TODO: correct value // TODO: correct value
let probe_network = false; let probe_network = false;
match job::load_next(&ctx1, Thread::Smtp, probe_network) match job::load_next(&ctx, Thread::Smtp, probe_network)
.timeout(Duration::from_millis(200)) .timeout(Duration::from_millis(200))
.await .await
{ {
Ok(Some(job)) => { Ok(Some(job)) => {
job::perform_job( job::perform_job(&ctx, job::Connection::Smtp(&mut connection), job).await;
&ctx1,
job::Connection::Smtp(&mut connection),
job,
)
.await;
} }
Ok(None) | Err(async_std::future::TimeoutError { .. }) => { Ok(None) | Err(async_std::future::TimeoutError { .. }) => {
use futures::future::FutureExt; use futures::future::FutureExt;
@@ -170,9 +129,36 @@ impl Scheduler {
fut.race(stop_receiver.recv()).await; fut.race(stop_receiver.recv()).await;
shutdown_sender.send(()).await; shutdown_sender.send(()).await;
}); }
impl Scheduler {
/// Start the scheduler, panics if it is already running.
pub fn run(&mut self, ctx: Context) {
match self {
Scheduler::Stopped => {
let (mvbox, mvbox_handlers) = ImapConnectionState::new();
let (sentbox, sentbox_handlers) = ImapConnectionState::new();
let (smtp, smtp_handlers) = SmtpConnectionState::new();
let (inbox, inbox_handlers) = ImapConnectionState::new();
let ctx1 = ctx.clone();
let _ = task::spawn(async move { inbox_loop(ctx1, inbox_handlers).await });
// TODO: mvbox
// TODO: sentbox
let ctx1 = ctx.clone();
let _ = task::spawn(async move { smtp_loop(ctx1, smtp_handlers).await });
*self = Scheduler::Running {
inbox,
mvbox,
sentbox,
smtp,
};
info!(ctx, "scheduler is running"); info!(ctx, "scheduler is running");
println!("RUN DONE");
} }
Scheduler::Running { .. } => { Scheduler::Running { .. } => {
// TODO: return an error // TODO: return an error
@@ -271,7 +257,7 @@ pub(crate) struct SmtpConnectionState {
} }
impl SmtpConnectionState { impl SmtpConnectionState {
async fn new() -> (Self, SmtpConnectionHandlers) { fn new() -> (Self, SmtpConnectionHandlers) {
let (stop_sender, stop_receiver) = channel(1); let (stop_sender, stop_receiver) = channel(1);
let (shutdown_sender, shutdown_receiver) = channel(1); let (shutdown_sender, shutdown_receiver) = channel(1);
let (idle_interrupt_sender, idle_interrupt_receiver) = channel(1); let (idle_interrupt_sender, idle_interrupt_receiver) = channel(1);
@@ -320,7 +306,7 @@ pub(crate) struct ImapConnectionState {
impl ImapConnectionState { impl ImapConnectionState {
/// Construct a new connection. /// Construct a new connection.
async fn new() -> (Self, ImapConnectionHandlers) { fn new() -> (Self, ImapConnectionHandlers) {
let (stop_sender, stop_receiver) = channel(1); let (stop_sender, stop_receiver) = channel(1);
let (idle_interrupt_sender, idle_interrupt_receiver) = channel(1); let (idle_interrupt_sender, idle_interrupt_receiver) = channel(1);
let (shutdown_sender, shutdown_receiver) = channel(1); let (shutdown_sender, shutdown_receiver) = channel(1);