OLD | NEW |
(Empty) | |
| 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), |
| 20 last_interval_(0) {} |
| 21 |
| 22 UpdateCheckScheduler::~UpdateCheckScheduler() {} |
| 23 |
| 24 void UpdateCheckScheduler::Run() { |
| 25 enabled_ = false; |
| 26 update_attempter_->set_update_check_scheduler(NULL); |
| 27 |
| 28 if (!IsOfficialBuild()) { |
| 29 LOG(WARNING) << "Non-official build: periodic update checks disabled."; |
| 30 return; |
| 31 } |
| 32 if (IsBootDeviceRemovable()) { |
| 33 LOG(WARNING) << "Removable device boot: periodic update checks disabled."; |
| 34 return; |
| 35 } |
| 36 enabled_ = true; |
| 37 |
| 38 // Registers this scheduler with the update attempter so that scheduler can be |
| 39 // notified of update status changes. |
| 40 update_attempter_->set_update_check_scheduler(this); |
| 41 |
| 42 // Kicks off periodic update checks. The first check is scheduled |
| 43 // |kTimeoutOnce| seconds from now. Subsequent checks are scheduled by |
| 44 // ScheduleNextCheck, normally at |kTimeoutPeriodic|-second intervals. |
| 45 ScheduleCheck(kTimeoutOnce, kTimeoutRegularFuzz); |
| 46 } |
| 47 |
| 48 bool UpdateCheckScheduler::IsBootDeviceRemovable() { |
| 49 return utils::IsRemovableDevice(utils::RootDevice(utils::BootDevice())); |
| 50 } |
| 51 |
| 52 bool UpdateCheckScheduler::IsOfficialBuild() { |
| 53 return utils::IsOfficialBuild(); |
| 54 } |
| 55 |
| 56 guint UpdateCheckScheduler::GTimeoutAddSeconds(guint interval, |
| 57 GSourceFunc function) { |
| 58 return g_timeout_add_seconds(interval, function, this); |
| 59 } |
| 60 |
| 61 void UpdateCheckScheduler::ScheduleCheck(int interval, int fuzz) { |
| 62 if (!CanSchedule()) { |
| 63 return; |
| 64 } |
| 65 last_interval_ = interval; |
| 66 interval = utils::FuzzInt(interval, fuzz); |
| 67 if (interval < 0) { |
| 68 interval = 0; |
| 69 } |
| 70 GTimeoutAddSeconds(interval, StaticCheck); |
| 71 scheduled_ = true; |
| 72 LOG(INFO) << "Next update check in " << interval << " seconds."; |
| 73 } |
| 74 |
| 75 gboolean UpdateCheckScheduler::StaticCheck(void* scheduler) { |
| 76 UpdateCheckScheduler* me = reinterpret_cast<UpdateCheckScheduler*>(scheduler); |
| 77 CHECK(me->scheduled_); |
| 78 me->scheduled_ = false; |
| 79 me->update_attempter_->Update("", ""); |
| 80 // This check ensures that future update checks will be or are already |
| 81 // scheduled. The check should never fail. A check failure means that there's |
| 82 // a bug that will most likely prevent further automatic update checks. It |
| 83 // seems better to crash in such cases and restart the update_engine daemon |
| 84 // into, hopefully, a known good state. |
| 85 CHECK(me->update_attempter_->status() != UPDATE_STATUS_IDLE || |
| 86 !me->CanSchedule()); |
| 87 return FALSE; // Don't run again. |
| 88 } |
| 89 |
| 90 void UpdateCheckScheduler::ComputeNextIntervalAndFuzz(int* next_interval, |
| 91 int* next_fuzz) { |
| 92 int interval = 0; |
| 93 int fuzz = 0; |
| 94 // Implements exponential back off on 500 (Internal Server Error) and 503 |
| 95 // (Service Unavailable) HTTP response codes. |
| 96 if (update_attempter_->http_response_code() == 500 || |
| 97 update_attempter_->http_response_code() == 503) { |
| 98 interval = 2 * last_interval_; |
| 99 if (interval > kTimeoutMaxBackoff) { |
| 100 interval = kTimeoutMaxBackoff; |
| 101 } |
| 102 // Exponential back off is fuzzed by +/- |interval|/2. |
| 103 fuzz = interval; |
| 104 } |
| 105 // Ensures that under normal conditions the regular update check interval and |
| 106 // fuzz are used. Also covers the case where back off is required based on the |
| 107 // initial update check. |
| 108 if (interval < kTimeoutPeriodic) { |
| 109 interval = kTimeoutPeriodic; |
| 110 fuzz = kTimeoutRegularFuzz; |
| 111 } |
| 112 *next_interval = interval; |
| 113 *next_fuzz = fuzz; |
| 114 } |
| 115 |
| 116 void UpdateCheckScheduler::ScheduleNextCheck() { |
| 117 int interval, fuzz; |
| 118 ComputeNextIntervalAndFuzz(&interval, &fuzz); |
| 119 ScheduleCheck(interval, fuzz); |
| 120 } |
| 121 |
| 122 void UpdateCheckScheduler::SetUpdateStatus(UpdateStatus status) { |
| 123 if (status == UPDATE_STATUS_IDLE) { |
| 124 ScheduleNextCheck(); |
| 125 } |
| 126 } |
| 127 |
| 128 } // namespace chromeos_update_engine |
OLD | NEW |