Split Run() method 57/216857/22
authorSangyoon Jang <jeremy.jang@samsung.com>
Mon, 21 Oct 2019 11:02:50 +0000 (20:02 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Tue, 24 Mar 2020 05:12:39 +0000 (14:12 +0900)
Change-Id: Iaa5d720bfac4c302ef7d3bb5ebc6087d4e0e7917
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/common/app_installer.cc
src/common/app_installer.h

index 6b529d4..bc618e8 100644 (file)
@@ -111,8 +111,8 @@ const int kLogMaximumRotation = 3;
 namespace common_installer {
 
 AppInstaller::AppInstaller(const char* package_type, PkgMgrPtr pkgmgr)
-    : pkgmgr_(pkgmgr),
-      context_(new InstallerContext()) {
+    : pkgmgr_(pkgmgr), context_(new InstallerContext()),
+      status_(Step::Status::OK), result_(Result::OK) {
   context_->pkg_type.set(package_type);
   context_->installation_mode.set(pkgmgr->GetInstallationMode());
 
@@ -195,89 +195,108 @@ void AppInstaller::Init() {
 }
 
 AppInstaller::Result AppInstaller::Run() {
-  Init();
-
-  std::list<std::unique_ptr<Step>>::iterator it(steps_.begin());
-  std::list<std::unique_ptr<Step>>::iterator itStart(steps_.begin());
-  std::list<std::unique_ptr<Step>>::iterator itEnd(steps_.end());
-
-  Step::Status status = Step::Status::OK;
-  Result ret = Result::OK;
-  unsigned total_steps = steps_.size();
-  unsigned current_step = 1;
-
-  std::shared_ptr<utils::FileLogBackend> failure_logger =
+  failure_logger_ =
       std::shared_ptr<utils::FileLogBackend>(new utils::FileLogBackend(
             kLogFileName, kLogRotationSize, kLogMaximumRotation));
-  ::utils::LogCore::GetCore().AddLogBackend(failure_logger);
-
-  for (; it != itEnd; ++it, ++current_step) {
-    status = SafeExecute(*it, &Step::precheck, "precheck");
-    if (status == Step::Status::OK)
-      status = SafeExecute(*it, &Step::process, "process");
-
-    if (status != Step::Status::OK) {
-      if (status != Step::Status::RECOVERY_DONE) {
-        LOG(ERROR) << "Error during processing";
-        ret = Result::ERROR;
-      }
-      break;
-    }
-    SendProgress(current_step * kProgressRange / total_steps);
-  }
+  ::utils::LogCore::GetCore().AddLogBackend(failure_logger_);
 
-  if (it != itEnd && ret == Result::ERROR) {
-    LOG(ERROR) << "Failure occurs in step: " << (*it)->name();
-    do {
-      if (SafeExecute(*it, &Step::undo, "undo") != Step::Status::OK) {
-        LOG(ERROR) << "Error during undo operation(" << (*it)->name()
-                   << "), but continuing...";
-        ret = Result::UNDO_ERROR;
-      }
-    } while (it-- != itStart);
-    failure_logger->WriteLogToFile();
-    SendFinished(status);
+  Process();
+  if (it_ != steps_.end() && result_ == Result::ERROR) {
+    LOG(ERROR) << "Failure occurs in step: " << (*it_)->name();
+    Undo();
   } else {
-    const auto& recovery_file =
-      context_->recovery_info.get().recovery_file;
-    if (recovery_file) {
-      recovery_file->set_cleanup(true);
-      recovery_file->WriteAndCommitFileContent();
-    }
-    SendFinished(status);
-    do {
-      if (it == itEnd)
-        --it;
-      if (SafeExecute(*it, &Step::clean, "clean") != Step::Status::OK) {
-        LOG(ERROR) << "Error during clean operation(" << (*it)->name() << ")";
-        ret = Result::CLEANUP_ERROR;
-        break;
-      }
-    } while (it-- != itStart);
+    Clean();
   }
   sync();
 
   if ((context_->installation_mode.get() == InstallationMode::OFFLINE) &&
-      (ret == Result::ERROR)) {
+      (result_ == Result::ERROR)) {
     std::fstream info_file("/tmp/.preload_install_error",
         std::ios::out | std::ios::app);
     info_file << context_->pkgid.get() << std::endl;
     info_file.close();
   }
 
+  LogHistory(status_ == Step::Status::OK ? true : false);
+
+  return result_;
+}
+
+void AppInstaller::LogHistory(bool success) {
   std::shared_ptr<utils::FileLogBackend> history_logger =
       std::shared_ptr<utils::FileLogBackend>(new utils::FileLogBackend(
           kHistoryFileName, kLogRotationSize, kLogMaximumRotation));
   std::string history = "pkgid:" + context_->pkgid.get() + "|mode:" +
       GetRequestTypeString(context_->request_type.get());
-  if (status == Step::Status::OK)
+  if (success)
     history += "|SUCCESS";
   else
     history += "|FAIL";
   history_logger->WriteLog(::utils::LogLevel::LOG_INFO, "", history);
   history_logger->WriteLogToFile();
+}
+
+AppInstaller::Result AppInstaller::Process() {
+  Init();
+
+  unsigned total_steps = steps_.size();
+  unsigned current_step = 1;
+
+  for (it_ = steps_.begin(); it_ != steps_.end(); ++it_, ++current_step) {
+    status_ = SafeExecute(*it_, &Step::precheck, "precheck");
+    if (status_ == Step::Status::OK)
+      status_ = SafeExecute(*it_, &Step::process, "process");
+
+    if (status_ != Step::Status::OK) {
+      if (status_ != Step::Status::RECOVERY_DONE) {
+        LOG(ERROR) << "Error during processing";
+        result_ = Result::ERROR;
+      }
+      break;
+    }
+    SendProgress(current_step * kProgressRange / total_steps);
+  }
+
+  return result_;
+}
+
+AppInstaller::Result AppInstaller::Undo() {
+  do {
+    if (SafeExecute(*it_, &Step::undo, "undo") != Step::Status::OK) {
+      LOG(ERROR) << "Error during undo operation(" << (*it_)->name()
+                 << "), but continuing...";
+      result_ = Result::UNDO_ERROR;
+    }
+  } while (it_-- != steps_.begin());
+  sync();
+  failure_logger_->WriteLogToFile();
+
+  SendFinished(status_);
+
+  return result_;
+}
+
+AppInstaller::Result AppInstaller::Clean() {
+  const auto& recovery_file =
+    context_->recovery_info.get().recovery_file;
+  if (recovery_file) {
+    recovery_file->set_cleanup(true);
+    recovery_file->WriteAndCommitFileContent();
+  }
+  SendFinished(status_);
+
+  do {
+    if (it_ == steps_.end())
+      --it_;
+    if (SafeExecute(*it_, &Step::clean, "clean") != Step::Status::OK) {
+      LOG(ERROR) << "Error during clean operation(" << (*it_)->name() << ")";
+      result_ = Result::CLEANUP_ERROR;
+      break;
+    }
+  } while (it_-- != steps_.begin());
+  sync();
 
-  return ret;
+  return result_;
 }
 
 void AppInstaller::InstallSteps() {
index ea7817f..89a2eaa 100644 (file)
@@ -18,6 +18,7 @@
 #include "common/pkgmgr_interface.h"
 #include "common/pkgmgr_signal.h"
 #include "common/step/step.h"
+#include "common/utils/file_logbackend.h"
 #include "common/utils/macros.h"
 
 namespace common_installer {
@@ -214,6 +215,27 @@ class AppInstaller {
   Result Run();
 
   /**
+   * \brief runs the process operations of steps
+   *
+   * \return Result of the run (eg Result:OK)
+   */
+  Result Process();
+
+  /**
+   * \brief runs the undo operations of steps
+   *
+   * \return Result of the run (eg Result:OK)
+   */
+  Result Undo();
+
+  /**
+   * \brief runs the clean operations of steps
+   *
+   * \return Result of the run (eg Result:OK)
+   */
+  Result Clean();
+
+  /**
    * \brief This method can be used to check
    *        the number of the registered steps.
    *
@@ -253,6 +275,7 @@ class AppInstaller {
   void Init();
 
   std::list<std::unique_ptr<Step>> steps_;
+  std::list<std::unique_ptr<Step>>::iterator it_;
 
   // data used to send signal
   std::unique_ptr<PkgmgrSignal> pi_;
@@ -264,6 +287,11 @@ class AppInstaller {
                            std::string name);
   void HandleStepError(Step::Status result, const std::string& error);
 
+  std::shared_ptr<utils::FileLogBackend> failure_logger_;
+  Step::Status status_;
+  Result result_;
+  void LogHistory(bool success);
+
   SCOPE_LOG_TAG(AppInstaller)
 
   DISALLOW_COPY_AND_ASSIGN(AppInstaller);