Rename job::Try into job::Status

This commit is contained in:
Alexander Krotov
2019-12-28 15:04:56 +01:00
parent 83c98c2d55
commit 25842894d2
3 changed files with 67 additions and 59 deletions

View File

@@ -13,7 +13,7 @@ use crate::constants::*;
use crate::context::Context; use crate::context::Context;
use crate::dc_tools::*; use crate::dc_tools::*;
use crate::e2ee; use crate::e2ee;
use crate::job::*; use crate::job::{self, job_add, job_kill_action};
use crate::login_param::{CertificateChecks, LoginParam}; use crate::login_param::{CertificateChecks, LoginParam};
use crate::oauth2::*; use crate::oauth2::*;
use crate::param::Params; use crate::param::Params;
@@ -37,8 +37,8 @@ pub fn configure(context: &Context) {
warn!(context, "There is already another ongoing process running.",); warn!(context, "There is already another ongoing process running.",);
return; return;
} }
job_kill_action(context, Action::ConfigureImap); job_kill_action(context, job::Action::ConfigureImap);
job_add(context, Action::ConfigureImap, 0, Params::new(), 0); job_add(context, job::Action::ConfigureImap, 0, Params::new(), 0);
} }
/// Check if the context is already configured. /// Check if the context is already configured.
@@ -50,15 +50,15 @@ pub fn dc_is_configured(context: &Context) -> bool {
* Configure JOB * Configure JOB
******************************************************************************/ ******************************************************************************/
#[allow(non_snake_case, unused_must_use)] #[allow(non_snake_case, unused_must_use)]
pub fn JobConfigureImap(context: &Context) -> Try { pub fn JobConfigureImap(context: &Context) -> job::Status {
if !context.sql.is_open() { if !context.sql.is_open() {
error!(context, "Cannot configure, database not opened.",); error!(context, "Cannot configure, database not opened.",);
progress!(context, 0); progress!(context, 0);
return Try::Finished(Err(format_err!("Database not opened"))); return job::Status::Finished(Err(format_err!("Database not opened")));
} }
if !context.alloc_ongoing() { if !context.alloc_ongoing() {
progress!(context, 0); progress!(context, 0);
return Try::Finished(Err(format_err!("Cannot allocated ongoing process"))); return job::Status::Finished(Err(format_err!("Cannot allocated ongoing process")));
} }
let mut success = false; let mut success = false;
let mut imap_connected_here = false; let mut imap_connected_here = false;
@@ -441,7 +441,7 @@ pub fn JobConfigureImap(context: &Context) -> Try {
context.free_ongoing(); context.free_ongoing();
progress!(context, if success { 1000 } else { 0 }); progress!(context, if success { 1000 } else { 0 });
Try::Finished(Ok(())) job::Status::Finished(Ok(()))
} }
fn get_offline_autoconfig(context: &Context, param: &LoginParam) -> Option<LoginParam> { fn get_offline_autoconfig(context: &Context, param: &LoginParam) -> Option<LoginParam> {

View File

@@ -43,7 +43,7 @@ enum Thread {
/// Job try result. /// Job try result.
#[derive(Debug, Display)] #[derive(Debug, Display)]
pub enum Try { pub enum Status {
Finished(std::result::Result<(), Error>), Finished(std::result::Result<(), Error>),
RetryNow, RetryNow,
RetryLater, RetryLater,
@@ -55,7 +55,7 @@ macro_rules! job_try {
match $expr { match $expr {
::std::result::Result::Ok(val) => val, ::std::result::Result::Ok(val) => val,
::std::result::Result::Err(err) => { ::std::result::Result::Err(err) => {
return $crate::job::Try::Finished(Err(err.into())); return $crate::job::Status::Finished(Err(err.into()));
} }
} }
}; };
@@ -172,13 +172,13 @@ impl Job {
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn SendMsgToSmtp(&mut self, context: &Context) -> Try { fn SendMsgToSmtp(&mut self, context: &Context) -> Status {
/* connect to SMTP server, if not yet done */ /* connect to SMTP server, if not yet done */
if !context.smtp.lock().unwrap().is_connected() { if !context.smtp.lock().unwrap().is_connected() {
let loginparam = LoginParam::from_database(context, "configured_"); let loginparam = LoginParam::from_database(context, "configured_");
if let Err(err) = context.smtp.lock().unwrap().connect(context, &loginparam) { if let Err(err) = context.smtp.lock().unwrap().connect(context, &loginparam) {
warn!(context, "SMTP connection failure: {:?}", err); warn!(context, "SMTP connection failure: {:?}", err);
return Try::RetryLater; return Status::RetryLater;
} }
} }
@@ -210,7 +210,7 @@ impl Job {
this happends if dc_delete_msgs() was called this happends if dc_delete_msgs() was called
before the generated mime was sent out */ before the generated mime was sent out */
if 0 != self.foreign_id && !message::exists(context, MsgId::new(self.foreign_id)) { if 0 != self.foreign_id && !message::exists(context, MsgId::new(self.foreign_id)) {
return Try::Finished(Err(format_err!( return Status::Finished(Err(format_err!(
"Not sending Message {} as it was deleted", "Not sending Message {} as it was deleted",
self.foreign_id self.foreign_id
))); )));
@@ -231,19 +231,19 @@ impl Job {
warn!(context, "SMTP failed to send: {}", err); warn!(context, "SMTP failed to send: {}", err);
smtp.disconnect(); smtp.disconnect();
self.pending_error = Some(err.to_string()); self.pending_error = Some(err.to_string());
Try::RetryLater Status::RetryLater
} }
Err(crate::smtp::send::Error::EnvelopeError(err)) => { Err(crate::smtp::send::Error::EnvelopeError(err)) => {
// Local error, job is invalid, do not retry. // Local error, job is invalid, do not retry.
smtp.disconnect(); smtp.disconnect();
warn!(context, "SMTP job is invalid: {}", err); warn!(context, "SMTP job is invalid: {}", err);
Try::Finished(Err(Error::SmtpError(err))) Status::Finished(Err(Error::SmtpError(err)))
} }
Err(crate::smtp::send::Error::NoTransport) => { Err(crate::smtp::send::Error::NoTransport) => {
// Should never happen. // Should never happen.
// It does not even make sense to disconnect here. // It does not even make sense to disconnect here.
error!(context, "SMTP job failed because SMTP has no transport"); error!(context, "SMTP job failed because SMTP has no transport");
Try::Finished(Err(format_err!("SMTP has not transport"))) Status::Finished(Err(format_err!("SMTP has not transport")))
} }
Ok(()) => { Ok(()) => {
// smtp success, update db ASAP, then delete smtp file // smtp success, update db ASAP, then delete smtp file
@@ -252,20 +252,20 @@ impl Job {
} }
// now also delete the generated file // now also delete the generated file
dc_delete_file(context, filename); dc_delete_file(context, filename);
Try::Finished(Ok(())) Status::Finished(Ok(()))
} }
} }
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn MoveMsg(&mut self, context: &Context) -> Try { fn MoveMsg(&mut self, context: &Context) -> Status {
let imap_inbox = &context.inbox_thread.read().unwrap().imap; let imap_inbox = &context.inbox_thread.read().unwrap().imap;
let msg = job_try!(Message::load_from_db(context, MsgId::new(self.foreign_id))); let msg = job_try!(Message::load_from_db(context, MsgId::new(self.foreign_id)));
if let Err(err) = imap_inbox.ensure_configured_folders(context, true) { if let Err(err) = imap_inbox.ensure_configured_folders(context, true) {
warn!(context, "could not configure folders: {:?}", err); warn!(context, "could not configure folders: {:?}", err);
return Try::RetryLater; return Status::RetryLater;
} }
let dest_folder = context let dest_folder = context
.sql .sql
@@ -282,21 +282,23 @@ impl Job {
&dest_folder, &dest_folder,
&mut dest_uid, &mut dest_uid,
) { ) {
ImapActionResult::RetryLater => Try::RetryLater, ImapActionResult::RetryLater => Status::RetryLater,
ImapActionResult::Success => { ImapActionResult::Success => {
message::update_server_uid(context, &msg.rfc724_mid, &dest_folder, dest_uid); message::update_server_uid(context, &msg.rfc724_mid, &dest_folder, dest_uid);
Try::Finished(Ok(())) Status::Finished(Ok(()))
} }
ImapActionResult::Failed => Try::Finished(Err(format_err!("IMAP action failed"))), ImapActionResult::Failed => {
ImapActionResult::AlreadyDone => Try::Finished(Ok(())), Status::Finished(Err(format_err!("IMAP action failed")))
}
ImapActionResult::AlreadyDone => Status::Finished(Ok(())),
} }
} else { } else {
Try::Finished(Err(format_err!("No mvbox folder configured"))) Status::Finished(Err(format_err!("No mvbox folder configured")))
} }
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn DeleteMsgOnImap(&mut self, context: &Context) -> Try { fn DeleteMsgOnImap(&mut self, context: &Context) -> Status {
let imap_inbox = &context.inbox_thread.read().unwrap().imap; let imap_inbox = &context.inbox_thread.read().unwrap().imap;
let mut msg = job_try!(Message::load_from_db(context, MsgId::new(self.foreign_id))); let mut msg = job_try!(Message::load_from_db(context, MsgId::new(self.foreign_id)));
@@ -315,19 +317,19 @@ impl Job {
let res = imap_inbox.delete_msg(context, &mid, server_folder, &mut msg.server_uid); let res = imap_inbox.delete_msg(context, &mid, server_folder, &mut msg.server_uid);
if res == ImapActionResult::RetryLater { if res == ImapActionResult::RetryLater {
// XXX RetryLater is converted to RetryNow here // XXX RetryLater is converted to RetryNow here
return Try::RetryNow; return Status::RetryNow;
} }
} }
Message::delete_from_db(context, msg.id); Message::delete_from_db(context, msg.id);
Try::Finished(Ok(())) Status::Finished(Ok(()))
} else { } else {
/* eg. device messages have no Message-ID */ /* eg. device messages have no Message-ID */
Try::Finished(Ok(())) Status::Finished(Ok(()))
} }
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn EmptyServer(&mut self, context: &Context) -> Try { fn EmptyServer(&mut self, context: &Context) -> Status {
let imap_inbox = &context.inbox_thread.read().unwrap().imap; let imap_inbox = &context.inbox_thread.read().unwrap().imap;
if self.foreign_id & DC_EMPTY_MVBOX > 0 { if self.foreign_id & DC_EMPTY_MVBOX > 0 {
if let Some(mvbox_folder) = context if let Some(mvbox_folder) = context
@@ -340,19 +342,19 @@ impl Job {
if self.foreign_id & DC_EMPTY_INBOX > 0 { if self.foreign_id & DC_EMPTY_INBOX > 0 {
imap_inbox.empty_folder(context, "INBOX"); imap_inbox.empty_folder(context, "INBOX");
} }
Try::Finished(Ok(())) Status::Finished(Ok(()))
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn MarkseenMsgOnImap(&mut self, context: &Context) -> Try { fn MarkseenMsgOnImap(&mut self, context: &Context) -> Status {
let imap_inbox = &context.inbox_thread.read().unwrap().imap; let imap_inbox = &context.inbox_thread.read().unwrap().imap;
let msg = job_try!(Message::load_from_db(context, MsgId::new(self.foreign_id))); let msg = job_try!(Message::load_from_db(context, MsgId::new(self.foreign_id)));
let folder = msg.server_folder.as_ref().unwrap(); let folder = msg.server_folder.as_ref().unwrap();
match imap_inbox.set_seen(context, folder, msg.server_uid) { match imap_inbox.set_seen(context, folder, msg.server_uid) {
ImapActionResult::RetryLater => Try::RetryLater, ImapActionResult::RetryLater => Status::RetryLater,
ImapActionResult::AlreadyDone => Try::Finished(Ok(())), ImapActionResult::AlreadyDone => Status::Finished(Ok(())),
ImapActionResult::Success | ImapActionResult::Failed => { ImapActionResult::Success | ImapActionResult::Failed => {
// XXX the message might just have been moved // XXX the message might just have been moved
// we want to send out an MDN anyway // we want to send out an MDN anyway
@@ -363,16 +365,16 @@ impl Job {
{ {
if let Err(err) = send_mdn(context, msg.id) { if let Err(err) = send_mdn(context, msg.id) {
warn!(context, "could not send out mdn for {}: {}", msg.id, err); warn!(context, "could not send out mdn for {}: {}", msg.id, err);
return Try::Finished(Err(err)); return Status::Finished(Err(err));
} }
} }
Try::Finished(Ok(())) Status::Finished(Ok(()))
} }
} }
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn MarkseenMdnOnImap(&mut self, context: &Context) -> Try { fn MarkseenMdnOnImap(&mut self, context: &Context) -> Status {
let folder = self let folder = self
.param .param
.get(Param::ServerFolder) .get(Param::ServerFolder)
@@ -381,12 +383,12 @@ impl Job {
let uid = self.param.get_int(Param::ServerUid).unwrap_or_default() as u32; let uid = self.param.get_int(Param::ServerUid).unwrap_or_default() as u32;
let imap_inbox = &context.inbox_thread.read().unwrap().imap; let imap_inbox = &context.inbox_thread.read().unwrap().imap;
if imap_inbox.set_seen(context, &folder, uid) == ImapActionResult::RetryLater { if imap_inbox.set_seen(context, &folder, uid) == ImapActionResult::RetryLater {
return Try::RetryLater; return Status::RetryLater;
} }
if self.param.get_bool(Param::AlsoMove).unwrap_or_default() { if self.param.get_bool(Param::AlsoMove).unwrap_or_default() {
if let Err(err) = imap_inbox.ensure_configured_folders(context, true) { if let Err(err) = imap_inbox.ensure_configured_folders(context, true) {
warn!(context, "configuring folders failed: {:?}", err); warn!(context, "configuring folders failed: {:?}", err);
return Try::RetryLater; return Status::RetryLater;
} }
let dest_folder = context let dest_folder = context
.sql .sql
@@ -396,15 +398,15 @@ impl Job {
if ImapActionResult::RetryLater if ImapActionResult::RetryLater
== imap_inbox.mv(context, &folder, uid, &dest_folder, &mut dest_uid) == imap_inbox.mv(context, &folder, uid, &dest_folder, &mut dest_uid)
{ {
Try::RetryLater Status::RetryLater
} else { } else {
Try::Finished(Ok(())) Status::Finished(Ok(()))
} }
} else { } else {
Try::Finished(Err(format_err!("MVBOX is not configured"))) Status::Finished(Err(format_err!("MVBOX is not configured")))
} }
} else { } else {
Try::Finished(Ok(())) Status::Finished(Ok(()))
} }
} }
} }
@@ -784,7 +786,7 @@ fn job_perform(context: &Context, thread: Thread, probe_network: bool) {
); );
let try_res = match job.action { let try_res = match job.action {
Action::Unknown => Try::Finished(Err(format_err!("Unknown job id found"))), Action::Unknown => Status::Finished(Err(format_err!("Unknown job id found"))),
Action::SendMsgToSmtp => job.SendMsgToSmtp(context), Action::SendMsgToSmtp => job.SendMsgToSmtp(context),
Action::EmptyServer => job.EmptyServer(context), Action::EmptyServer => job.EmptyServer(context),
Action::DeleteMsgOnImap => job.DeleteMsgOnImap(context), Action::DeleteMsgOnImap => job.DeleteMsgOnImap(context),
@@ -794,10 +796,10 @@ fn job_perform(context: &Context, thread: Thread, probe_network: bool) {
Action::SendMdn => job.SendMsgToSmtp(context), Action::SendMdn => job.SendMsgToSmtp(context),
Action::ConfigureImap => JobConfigureImap(context), Action::ConfigureImap => JobConfigureImap(context),
Action::ImexImap => match JobImexImap(context, &job) { Action::ImexImap => match JobImexImap(context, &job) {
Ok(()) => Try::Finished(Ok(())), Ok(()) => Status::Finished(Ok(())),
Err(err) => { Err(err) => {
error!(context, "{}", err); error!(context, "{}", err);
Try::Finished(Err(err)) Status::Finished(Err(err))
} }
}, },
Action::MaybeSendLocations => location::JobMaybeSendLocations(context, &job), Action::MaybeSendLocations => location::JobMaybeSendLocations(context, &job),
@@ -806,10 +808,10 @@ fn job_perform(context: &Context, thread: Thread, probe_network: bool) {
} }
Action::Housekeeping => { Action::Housekeeping => {
sql::housekeeping(context); sql::housekeeping(context);
Try::Finished(Ok(())) Status::Finished(Ok(()))
} }
Action::SendMdnOld => Try::Finished(Ok(())), Action::SendMdnOld => Status::Finished(Ok(())),
Action::SendMsgToSmtpOld => Try::Finished(Ok(())), Action::SendMsgToSmtpOld => Status::Finished(Ok(())),
}; };
info!( info!(
@@ -820,10 +822,10 @@ fn job_perform(context: &Context, thread: Thread, probe_network: bool) {
try_res try_res
}) })
.find(|try_res| match try_res { .find(|try_res| match try_res {
Try::RetryNow => false, Status::RetryNow => false,
_ => true, _ => true,
}) })
.unwrap_or(Try::RetryNow); .unwrap_or(Status::RetryNow);
if Action::ConfigureImap == job.action || Action::ImexImap == job.action { if Action::ConfigureImap == job.action || Action::ImexImap == job.action {
context context
@@ -843,7 +845,7 @@ fn job_perform(context: &Context, thread: Thread, probe_network: bool) {
} }
match try_res { match try_res {
Try::RetryNow | Try::RetryLater => { Status::RetryNow | Status::RetryLater => {
let tries = job.tries + 1; let tries = job.tries + 1;
if tries < JOB_RETRIES { if tries < JOB_RETRIES {
@@ -898,7 +900,7 @@ fn job_perform(context: &Context, thread: Thread, probe_network: bool) {
// to give other jobs a chance being tried at least once. // to give other jobs a chance being tried at least once.
break; break;
} }
Try::Finished(res) => { Status::Finished(res) => {
if let Err(err) = res { if let Err(err) = res {
warn!( warn!(
context, context,

View File

@@ -11,7 +11,7 @@ use crate::context::*;
use crate::dc_tools::*; use crate::dc_tools::*;
use crate::error::Error; use crate::error::Error;
use crate::events::Event; use crate::events::Event;
use crate::job::*; use crate::job::{self, job_action_exists, job_add, Job};
use crate::message::{Message, MsgId}; use crate::message::{Message, MsgId};
use crate::mimeparser::SystemMessage; use crate::mimeparser::SystemMessage;
use crate::param::*; use crate::param::*;
@@ -228,7 +228,7 @@ pub fn send_locations_to_chat(context: &Context, chat_id: u32, seconds: i64) {
schedule_MAYBE_SEND_LOCATIONS(context, false); schedule_MAYBE_SEND_LOCATIONS(context, false);
job_add( job_add(
context, context,
Action::MaybeSendLocationsEnded, job::Action::MaybeSendLocationsEnded,
chat_id as i32, chat_id as i32,
Params::new(), Params::new(),
seconds + 1, seconds + 1,
@@ -240,8 +240,14 @@ pub fn send_locations_to_chat(context: &Context, chat_id: u32, seconds: i64) {
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn schedule_MAYBE_SEND_LOCATIONS(context: &Context, force_schedule: bool) { fn schedule_MAYBE_SEND_LOCATIONS(context: &Context, force_schedule: bool) {
if force_schedule || !job_action_exists(context, Action::MaybeSendLocations) { if force_schedule || !job_action_exists(context, job::Action::MaybeSendLocations) {
job_add(context, Action::MaybeSendLocations, 0, Params::new(), 60); job_add(
context,
job::Action::MaybeSendLocations,
0,
Params::new(),
60,
);
}; };
} }
@@ -542,7 +548,7 @@ pub fn save(
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn JobMaybeSendLocations(context: &Context, _job: &Job) -> Try { pub fn JobMaybeSendLocations(context: &Context, _job: &Job) -> job::Status {
let now = time(); let now = time();
let mut continue_streaming = false; let mut continue_streaming = false;
info!( info!(
@@ -629,11 +635,11 @@ pub fn JobMaybeSendLocations(context: &Context, _job: &Job) -> Try {
if continue_streaming { if continue_streaming {
schedule_MAYBE_SEND_LOCATIONS(context, true); schedule_MAYBE_SEND_LOCATIONS(context, true);
} }
Try::Finished(Ok(())) job::Status::Finished(Ok(()))
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn JobMaybeSendLocationsEnded(context: &Context, job: &mut Job) -> Try { pub fn JobMaybeSendLocationsEnded(context: &Context, job: &mut Job) -> job::Status {
// this function is called when location-streaming _might_ have ended for a chat. // this function is called when location-streaming _might_ have ended for a chat.
// the function checks, if location-streaming is really ended; // the function checks, if location-streaming is really ended;
// if so, a device-message is added if not yet done. // if so, a device-message is added if not yet done.
@@ -662,7 +668,7 @@ pub fn JobMaybeSendLocationsEnded(context: &Context, job: &mut Job) -> Try {
context.call_cb(Event::ChatModified(chat_id)); context.call_cb(Event::ChatModified(chat_id));
} }
} }
Try::Finished(Ok(())) job::Status::Finished(Ok(()))
} }
#[cfg(test)] #[cfg(test)]