blob: 16f1929c2d65423cd68e3d3ca89195f5f7a1ad87 [file] [log] [blame]
Darin Petkov1023a602010-08-30 13:47:51 -07001// 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
9namespace chromeos_update_engine {
10
11const int UpdateCheckScheduler::kTimeoutOnce = 7 * 60; // at 7 minutes
12const int UpdateCheckScheduler::kTimeoutPeriodic = 45 * 60; // every 45 minutes
13const int UpdateCheckScheduler::kTimeoutRegularFuzz = 10 * 60; // +/- 5 minutes
14const int UpdateCheckScheduler::kTimeoutMaxBackoff = 4 * 60 * 60; // 4 hours
15
16UpdateCheckScheduler::UpdateCheckScheduler(UpdateAttempter* update_attempter)
17 : update_attempter_(update_attempter),
18 enabled_(false),
19 scheduled_(false),
Darin Petkov85ced132010-09-01 10:20:56 -070020 last_interval_(0),
21 poll_interval_(0) {}
Darin Petkov1023a602010-08-30 13:47:51 -070022
23UpdateCheckScheduler::~UpdateCheckScheduler() {}
24
25void 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
49bool UpdateCheckScheduler::IsBootDeviceRemovable() {
50 return utils::IsRemovableDevice(utils::RootDevice(utils::BootDevice()));
51}
52
Darin Petkov2a0e6332010-09-24 14:43:41 -070053bool UpdateCheckScheduler::IsOOBEComplete() {
54 return utils::IsOOBEComplete();
55}
56
Darin Petkov1023a602010-08-30 13:47:51 -070057bool UpdateCheckScheduler::IsOfficialBuild() {
58 return utils::IsOfficialBuild();
59}
60
61guint UpdateCheckScheduler::GTimeoutAddSeconds(guint interval,
62 GSourceFunc function) {
63 return g_timeout_add_seconds(interval, function, this);
64}
65
66void 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
80gboolean UpdateCheckScheduler::StaticCheck(void* scheduler) {
81 UpdateCheckScheduler* me = reinterpret_cast<UpdateCheckScheduler*>(scheduler);
82 CHECK(me->scheduled_);
83 me->scheduled_ = false;
Darin Petkov2a0e6332010-09-24 14:43:41 -070084 if (me->IsOOBEComplete()) {
Andrew de los Reyes45168102010-11-22 11:13:50 -080085 me->update_attempter_->Update("", "", false);
Darin Petkov2a0e6332010-09-24 14:43:41 -070086 } 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 Petkov1023a602010-08-30 13:47:51 -070092 // 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
102void UpdateCheckScheduler::ComputeNextIntervalAndFuzz(int* next_interval,
103 int* next_fuzz) {
104 int interval = 0;
Darin Petkov85ced132010-09-01 10:20:56 -0700105 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 Petkov1023a602010-08-30 13:47:51 -0700113 interval = 2 * last_interval_;
Darin Petkov85ced132010-09-01 10:20:56 -0700114 LOG(WARNING) << "Exponential back off due to 500/503 HTTP response code.";
Darin Petkov1023a602010-08-30 13:47:51 -0700115 }
Darin Petkov85ced132010-09-01 10:20:56 -0700116 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 Petkov1023a602010-08-30 13:47:51 -0700122 // 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
133void UpdateCheckScheduler::ScheduleNextCheck() {
134 int interval, fuzz;
135 ComputeNextIntervalAndFuzz(&interval, &fuzz);
136 ScheduleCheck(interval, fuzz);
137}
138
139void UpdateCheckScheduler::SetUpdateStatus(UpdateStatus status) {
140 if (status == UPDATE_STATUS_IDLE) {
141 ScheduleNextCheck();
142 }
143}
144
145} // namespace chromeos_update_engine