Skip to content

Commit

Permalink
Issue #170: AppManager status fix logic on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqiushi committed Apr 12, 2023
1 parent beed213 commit 4c479a9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 42 deletions.
30 changes: 12 additions & 18 deletions src/service/app-manager/src/app_cmd_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,21 @@ impl AppCmdExecutor {
_retry_count: u32,
) -> BuckyResult<()> {
let app_id = cmd.app_id();
let cmd_code = cmd.cmd();
// if app already running, return success
if self.app_controller.is_app_running(app_id).await? {
let _ = self
.post_change_status(
app_id,
status.clone(),
cmd_code,
AppLocalStatusCode::Starting,
AppLocalStatusCode::Running,
SubErrorCode::None,
)
.await;
return Ok(());
}
let cmd_code = cmd.cmd();
//info!("will execute cmd, app:{}, cmd: {:?}", app_id, cmd_code);

self.pre_change_status(
Expand Down Expand Up @@ -248,22 +258,6 @@ impl AppCmdExecutor {
_retry_count: u32,
) -> BuckyResult<()> {
let app_id = cmd.app_id();

match self.app_controller.is_app_running(app_id).await {
Ok(running) => {
// if app already not running, return ok
if !running {
return Ok(());
}
}
Err(e) => {
// NotFound means app already uninstalled, return ok
if e.code() == BuckyErrorCode::NotFound {
return Ok(());
}
}
}

let cmd_code = cmd.cmd();

self.pre_change_status(
Expand Down Expand Up @@ -320,7 +314,7 @@ impl AppCmdExecutor {
status.clone(),
cmd_code,
AppLocalStatusCode::Installing,
true,
false,
)
.await?;

Expand Down
44 changes: 20 additions & 24 deletions src/service/app-manager/src/app_manager_ex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,24 +333,19 @@ impl AppManager {
async fn fix_status_on_startup(&self) {
let status_list = self.status_list.read().unwrap().clone();
for (app_id, status_a) in status_list {
let mut status_clone = None;
{
let mut status = status_a.lock().unwrap();
let status_code = status.status();
let status_code = status_a.lock().unwrap().status();
let fix_status = match status_code {
AppLocalStatusCode::Stopping => {
info!("find app {} status {} on startup, try stop again", app_id, status_code);
match self.cmd_executor.as_ref().unwrap().execute_stop(
if let Err(e) = self.cmd_executor.as_ref().unwrap().execute_stop(
status_a.clone(),
&AppCmd::stop(self.owner.clone(), app_id.clone()),
0).await {
Ok(_) => {
Some(AppLocalStatusCode::Stop)
}
Err(e) => {
error!("stop app {} on startup err {}", app_id, e);
Some(AppLocalStatusCode::StopFailed)
}
warn!("stop app {} on startup failed, err {}", &app_id, e);
Some(AppLocalStatusCode::StopFailed)
} else {
None
}
},
AppLocalStatusCode::Starting => {
Expand All @@ -360,7 +355,7 @@ impl AppManager {
&AppCmd::start(self.owner.clone(), app_id.clone()),
0).await {
Ok(_) => {
Some(AppLocalStatusCode::Running)
None
}
Err(e) => {
error!("start app {} on startup err {}", app_id, e);
Expand All @@ -370,15 +365,16 @@ impl AppManager {
},
AppLocalStatusCode::Installing => {
info!("find app {} status {} on startup, try install again", app_id, status_code);
let version = status_a.lock().unwrap().version().map(|s|s.to_owned());
match self.cmd_executor.as_ref().unwrap().execute_install(
status_a.clone(),
&AppCmd::install(self.owner.clone(), app_id.clone(), status.version().unwrap(), true),
&AppCmd::install(self.owner.clone(), app_id.clone(), &version.unwrap(), true),
0).await {
Ok(_) => {
Some(AppLocalStatusCode::Running)
None
}
Err(e) => {
error!("start app {} on startup err {}", app_id, e);
error!("install app {} on startup err {}", app_id, e);
Some(AppLocalStatusCode::InstallFailed)
}
}
Expand All @@ -390,29 +386,29 @@ impl AppManager {
&AppCmd::uninstall(self.owner.clone(), app_id.clone()),
0).await {
Ok(_) => {
Some(AppLocalStatusCode::Uninstalled)
None
}
Err(e) => {
error!("start app {} on startup err {}", app_id, e);
error!("uninstall app {} on startup err {}", app_id, e);
Some(AppLocalStatusCode::UninstallFailed)
}
}
},
_ => None,
};
if fix_status.is_some() {
let fix_code = fix_status.unwrap();
status.set_status(fix_code);
status_clone = Some(status.clone());
if let Some(fix_code) = fix_status {
let new_status = {
let mut status = status_a.lock().unwrap();
status.set_status(fix_code);
status.clone()
};
let _ = self.non_helper.put_local_status(&new_status).await;
info!(
"### fix app status on startup, app:{}, from {} to {}",
app_id, status_code, fix_code
);
}
}
if let Some(new_status) = status_clone {
let _ = self.non_helper.put_local_status(&new_status).await;
}
}
}

Expand Down

0 comments on commit 4c479a9

Please sign in to comment.