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