Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/update_check_scheduler.h" |
| 6 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 7 | #include "update_engine/certificate_checker.h" |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 8 | #include "update_engine/utils.h" |
| 9 | |
| 10 | namespace chromeos_update_engine { |
| 11 | |
| 12 | const int UpdateCheckScheduler::kTimeoutOnce = 7 * 60; // at 7 minutes |
| 13 | const int UpdateCheckScheduler::kTimeoutPeriodic = 45 * 60; // every 45 minutes |
| 14 | const int UpdateCheckScheduler::kTimeoutRegularFuzz = 10 * 60; // +/- 5 minutes |
| 15 | const int UpdateCheckScheduler::kTimeoutMaxBackoff = 4 * 60 * 60; // 4 hours |
| 16 | |
| 17 | UpdateCheckScheduler::UpdateCheckScheduler(UpdateAttempter* update_attempter) |
| 18 | : update_attempter_(update_attempter), |
| 19 | enabled_(false), |
| 20 | scheduled_(false), |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 21 | last_interval_(0), |
| 22 | poll_interval_(0) {} |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 23 | |
| 24 | UpdateCheckScheduler::~UpdateCheckScheduler() {} |
| 25 | |
| 26 | void UpdateCheckScheduler::Run() { |
| 27 | enabled_ = false; |
| 28 | update_attempter_->set_update_check_scheduler(NULL); |
| 29 | |
| 30 | if (!IsOfficialBuild()) { |
| 31 | LOG(WARNING) << "Non-official build: periodic update checks disabled."; |
| 32 | return; |
| 33 | } |
| 34 | if (IsBootDeviceRemovable()) { |
| 35 | LOG(WARNING) << "Removable device boot: periodic update checks disabled."; |
| 36 | return; |
| 37 | } |
| 38 | enabled_ = true; |
| 39 | |
| 40 | // Registers this scheduler with the update attempter so that scheduler can be |
| 41 | // notified of update status changes. |
| 42 | update_attempter_->set_update_check_scheduler(this); |
| 43 | |
| 44 | // Kicks off periodic update checks. The first check is scheduled |
| 45 | // |kTimeoutOnce| seconds from now. Subsequent checks are scheduled by |
| 46 | // ScheduleNextCheck, normally at |kTimeoutPeriodic|-second intervals. |
| 47 | ScheduleCheck(kTimeoutOnce, kTimeoutRegularFuzz); |
| 48 | } |
| 49 | |
| 50 | bool UpdateCheckScheduler::IsBootDeviceRemovable() { |
| 51 | return utils::IsRemovableDevice(utils::RootDevice(utils::BootDevice())); |
| 52 | } |
| 53 | |
Darin Petkov | 2a0e633 | 2010-09-24 14:43:41 -0700 | [diff] [blame] | 54 | bool UpdateCheckScheduler::IsOOBEComplete() { |
| 55 | return utils::IsOOBEComplete(); |
| 56 | } |
| 57 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 58 | bool UpdateCheckScheduler::IsOfficialBuild() { |
| 59 | return utils::IsOfficialBuild(); |
| 60 | } |
| 61 | |
| 62 | guint UpdateCheckScheduler::GTimeoutAddSeconds(guint interval, |
| 63 | GSourceFunc function) { |
| 64 | return g_timeout_add_seconds(interval, function, this); |
| 65 | } |
| 66 | |
| 67 | void UpdateCheckScheduler::ScheduleCheck(int interval, int fuzz) { |
| 68 | if (!CanSchedule()) { |
| 69 | return; |
| 70 | } |
| 71 | last_interval_ = interval; |
| 72 | interval = utils::FuzzInt(interval, fuzz); |
| 73 | if (interval < 0) { |
| 74 | interval = 0; |
| 75 | } |
| 76 | GTimeoutAddSeconds(interval, StaticCheck); |
| 77 | scheduled_ = true; |
| 78 | LOG(INFO) << "Next update check in " << interval << " seconds."; |
| 79 | } |
| 80 | |
| 81 | gboolean UpdateCheckScheduler::StaticCheck(void* scheduler) { |
| 82 | UpdateCheckScheduler* me = reinterpret_cast<UpdateCheckScheduler*>(scheduler); |
| 83 | CHECK(me->scheduled_); |
| 84 | me->scheduled_ = false; |
Darin Petkov | 2a0e633 | 2010-09-24 14:43:41 -0700 | [diff] [blame] | 85 | if (me->IsOOBEComplete()) { |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 86 | // Before updating, we flush any previously generated UMA reports. |
| 87 | CertificateChecker::FlushReport(); |
Andrew de los Reyes | fb2f461 | 2011-06-09 18:21:49 -0700 | [diff] [blame] | 88 | me->update_attempter_->Update("", "", false, false); |
Darin Petkov | 2a0e633 | 2010-09-24 14:43:41 -0700 | [diff] [blame] | 89 | } else { |
| 90 | // Skips all automatic update checks if the OOBE process is not complete and |
| 91 | // schedules a new check as if it is the first one. |
| 92 | LOG(WARNING) << "Skipping update check because OOBE is not complete."; |
| 93 | me->ScheduleCheck(kTimeoutOnce, kTimeoutRegularFuzz); |
| 94 | } |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 95 | // This check ensures that future update checks will be or are already |
| 96 | // scheduled. The check should never fail. A check failure means that there's |
| 97 | // a bug that will most likely prevent further automatic update checks. It |
| 98 | // seems better to crash in such cases and restart the update_engine daemon |
| 99 | // into, hopefully, a known good state. |
| 100 | CHECK(me->update_attempter_->status() != UPDATE_STATUS_IDLE || |
| 101 | !me->CanSchedule()); |
| 102 | return FALSE; // Don't run again. |
| 103 | } |
| 104 | |
| 105 | void UpdateCheckScheduler::ComputeNextIntervalAndFuzz(int* next_interval, |
| 106 | int* next_fuzz) { |
| 107 | int interval = 0; |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 108 | if (poll_interval_ > 0) { |
| 109 | // Server-dictated poll interval. |
| 110 | interval = poll_interval_; |
| 111 | LOG(WARNING) << "Using server-dictated poll interval: " << interval; |
| 112 | } else if (update_attempter_->http_response_code() == 500 || |
| 113 | update_attempter_->http_response_code() == 503) { |
| 114 | // Implements exponential back off on 500 (Internal Server Error) and 503 |
| 115 | // (Service Unavailable) HTTP response codes. |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 116 | interval = 2 * last_interval_; |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 117 | LOG(WARNING) << "Exponential back off due to 500/503 HTTP response code."; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 118 | } |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 119 | if (interval > kTimeoutMaxBackoff) { |
| 120 | interval = kTimeoutMaxBackoff; |
| 121 | } |
| 122 | // Back off and server-dictated poll intervals are fuzzed by +/- |interval|/2. |
| 123 | int fuzz = interval; |
| 124 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 125 | // Ensures that under normal conditions the regular update check interval and |
| 126 | // fuzz are used. Also covers the case where back off is required based on the |
| 127 | // initial update check. |
| 128 | if (interval < kTimeoutPeriodic) { |
| 129 | interval = kTimeoutPeriodic; |
| 130 | fuzz = kTimeoutRegularFuzz; |
| 131 | } |
| 132 | *next_interval = interval; |
| 133 | *next_fuzz = fuzz; |
| 134 | } |
| 135 | |
| 136 | void UpdateCheckScheduler::ScheduleNextCheck() { |
| 137 | int interval, fuzz; |
| 138 | ComputeNextIntervalAndFuzz(&interval, &fuzz); |
| 139 | ScheduleCheck(interval, fuzz); |
| 140 | } |
| 141 | |
| 142 | void UpdateCheckScheduler::SetUpdateStatus(UpdateStatus status) { |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 143 | // We want to schedule the update checks for when we're idle as well as |
| 144 | // after we've successfully applied an update and waiting for the user |
| 145 | // to reboot to ensure our active count is accurate. |
| 146 | if (status == UPDATE_STATUS_IDLE || |
| 147 | status == UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 148 | ScheduleNextCheck(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | } // namespace chromeos_update_engine |