Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2009 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/action_processor.h" |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 18 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 19 | #include <string> |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 20 | |
| 21 | #include <base/logging.h> |
| 22 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 23 | #include "update_engine/common/action.h" |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 24 | #include "update_engine/common/error_code_utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 25 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 26 | using std::string; |
| 27 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 28 | namespace chromeos_update_engine { |
| 29 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 | ActionProcessor::~ActionProcessor() { |
Alex Deymo | 2b4268c | 2015-12-04 13:56:25 -0800 | [diff] [blame] | 31 | if (IsRunning()) |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 32 | StopProcessing(); |
Alex Deymo | 2b4268c | 2015-12-04 13:56:25 -0800 | [diff] [blame] | 33 | for (auto action : actions_) |
| 34 | action->SetProcessor(nullptr); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void ActionProcessor::EnqueueAction(AbstractAction* action) { |
| 38 | actions_.push_back(action); |
| 39 | action->SetProcessor(this); |
| 40 | } |
| 41 | |
| 42 | void ActionProcessor::StartProcessing() { |
| 43 | CHECK(!IsRunning()); |
| 44 | if (!actions_.empty()) { |
| 45 | current_action_ = actions_.front(); |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 46 | LOG(INFO) << "ActionProcessor: starting " << current_action_->Type(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 47 | actions_.pop_front(); |
| 48 | current_action_->PerformAction(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void ActionProcessor::StopProcessing() { |
| 53 | CHECK(IsRunning()); |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 54 | if (current_action_) { |
| 55 | current_action_->TerminateProcessing(); |
| 56 | current_action_->SetProcessor(nullptr); |
| 57 | } |
| 58 | LOG(INFO) << "ActionProcessor: aborted " |
| 59 | << (current_action_ ? current_action_->Type() : "") |
| 60 | << (suspended_ ? " while suspended" : ""); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 61 | current_action_ = nullptr; |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 62 | suspended_ = false; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 63 | if (delegate_) |
| 64 | delegate_->ProcessingStopped(this); |
| 65 | } |
| 66 | |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 67 | void ActionProcessor::SuspendProcessing() { |
| 68 | // No current_action_ when not suspended means that the action processor was |
| 69 | // never started or already finished. |
| 70 | if (suspended_ || !current_action_) { |
| 71 | LOG(WARNING) << "Called SuspendProcessing while not processing."; |
| 72 | return; |
| 73 | } |
| 74 | suspended_ = true; |
| 75 | |
| 76 | // If there's a current action we should notify it that it should suspend, but |
| 77 | // the action can ignore that and terminate at any point. |
| 78 | LOG(INFO) << "ActionProcessor: suspending " << current_action_->Type(); |
| 79 | current_action_->SuspendAction(); |
| 80 | } |
| 81 | |
| 82 | void ActionProcessor::ResumeProcessing() { |
| 83 | if (!suspended_) { |
| 84 | LOG(WARNING) << "Called ResumeProcessing while not suspended."; |
| 85 | return; |
| 86 | } |
| 87 | suspended_ = false; |
| 88 | if (current_action_) { |
| 89 | // The current_action_ did not call ActionComplete while suspended, so we |
| 90 | // should notify it of the resume operation. |
| 91 | LOG(INFO) << "ActionProcessor: resuming " << current_action_->Type(); |
| 92 | current_action_->ResumeAction(); |
| 93 | } else { |
| 94 | // The last action called ActionComplete while suspended, so there is |
| 95 | // already a log message with the type of the finished action. We simply |
| 96 | // state that we are resuming processing and the next function will log the |
| 97 | // start of the next action or processing completion. |
| 98 | LOG(INFO) << "ActionProcessor: resuming processing"; |
| 99 | StartNextActionOrFinish(suspended_error_code_); |
| 100 | } |
| 101 | } |
| 102 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 103 | void ActionProcessor::ActionComplete(AbstractAction* actionptr, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 104 | ErrorCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 105 | CHECK_EQ(actionptr, current_action_); |
| 106 | if (delegate_) |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 107 | delegate_->ActionCompleted(this, actionptr, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 108 | string old_type = current_action_->Type(); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 109 | current_action_->ActionCompleted(code); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 110 | current_action_->SetProcessor(nullptr); |
| 111 | current_action_ = nullptr; |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 112 | LOG(INFO) << "ActionProcessor: finished " |
| 113 | << (actions_.empty() ? "last action " : "") << old_type |
| 114 | << (suspended_ ? " while suspended" : "") |
| 115 | << " with code " << utils::ErrorCodeToString(code); |
| 116 | if (!actions_.empty() && code != ErrorCode::kSuccess) { |
| 117 | LOG(INFO) << "ActionProcessor: Aborting processing due to failure."; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 118 | actions_.clear(); |
| 119 | } |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 120 | if (suspended_) { |
| 121 | // If an action finished while suspended we don't start the next action (or |
| 122 | // terminate the processing) until the processor is resumed. This condition |
| 123 | // will be flagged by a nullptr current_action_ while suspended_ is true. |
| 124 | suspended_error_code_ = code; |
| 125 | return; |
| 126 | } |
| 127 | StartNextActionOrFinish(code); |
| 128 | } |
| 129 | |
| 130 | void ActionProcessor::StartNextActionOrFinish(ErrorCode code) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 131 | if (actions_.empty()) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 132 | if (delegate_) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 133 | delegate_->ProcessingDone(this, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 134 | } |
| 135 | return; |
| 136 | } |
| 137 | current_action_ = actions_.front(); |
| 138 | actions_.pop_front(); |
Alex Deymo | 14fd1ec | 2016-02-24 22:03:57 -0800 | [diff] [blame] | 139 | LOG(INFO) << "ActionProcessor: starting " << current_action_->Type(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 140 | current_action_->PerformAction(); |
| 141 | } |
| 142 | |
| 143 | } // namespace chromeos_update_engine |