更新 Bot 的電腦: 忙碌時按更新會排隊,閒下來自動執行;失敗自動重試(越錯越慢)
This commit is contained in:
parent
1ebfb5ef34
commit
b917480af1
|
|
@ -865,6 +865,7 @@ AnyBot 是一個桌面聊天 App,介面和運作方式照 Grok Bot 重做。
|
||||||
- 還沒有快照時按鈕是灰的。
|
- 還沒有快照時按鈕是灰的。
|
||||||
- **「重設電腦」**(紅色按鈕):全部清空,換一台全新的電腦。步驟:正在拆除舊電腦 → 正在清除資料 → 正在建立。
|
- **「重設電腦」**(紅色按鈕):全部清空,換一台全新的電腦。步驟:正在拆除舊電腦 → 正在清除資料 → 正在建立。
|
||||||
- **同時只能有一個更新**:再按會顯示「已經有一個更新在進行中」。開始後就不能取消。
|
- **同時只能有一個更新**:再按會顯示「已經有一個更新在進行中」。開始後就不能取消。
|
||||||
|
- **忙碌時按更新會排隊**:有 Bot 正在用電腦時按「更新」(或排程更新遇到忙碌),會被擋下並記住這次請求(「已排隊,它一閒下來就自動更新」);電腦沒人用滿一分鐘就自動開始。自動更新失敗會自動重試,每次失敗多等 30 秒(30、60、90 秒…),成功後歸零。
|
||||||
- **中斷了會接續**:更新做到一半 App 被關掉,下次打開時,如果紀錄在 10 分鐘內、而且電腦不正常,就自動接續;超過 10 分鐘就清掉紀錄,電腦正常時也不重做(都取自原版)。
|
- **中斷了會接續**:更新做到一半 App 被關掉,下次打開時,如果紀錄在 10 分鐘內、而且電腦不正常,就自動接續;超過 10 分鐘就清掉紀錄,電腦正常時也不重做(都取自原版)。
|
||||||
- 進度在設定頁用步驟列和進度條顯示。
|
- 進度在設定頁用步驟列和進度條顯示。
|
||||||
- **新版本與排程更新**(原版:「有新映像檔就在指定的當地時間自動更新,錯過有一小時的補執行時間」):
|
- **新版本與排程更新**(原版:「有新映像檔就在指定的當地時間自動更新,錯過有一小時的補執行時間」):
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@
|
||||||
|
|
||||||
## 陷阱
|
## 陷阱
|
||||||
|
|
||||||
|
- 按「更新」時有 Bot 正在用電腦:會被擋下並排隊(訊息說「已排隊,它一閒下來就自動更新」),box 沒人用滿 60 秒就自動開始;自動更新失敗會自己重試,越錯越慢(30、60、90 秒…),成功後歸零。自動更新只在「大腦」那台發生;連別台大腦的 app(手腳)自己的「Bot 的電腦」沒有可更新的東西,是大腦的手腳:要更新請到大腦的介面按。
|
||||||
- Bot 不能自己換電腦:工具指定了別台,但使用者最近三則訊息沒提到那台,這一步會被移回使用者選的那台,結果裡會說。真的要別台先問使用者。
|
- Bot 不能自己換電腦:工具指定了別台,但使用者最近三則訊息沒提到那台,這一步會被移回使用者選的那台,結果裡會說。真的要別台先問使用者。
|
||||||
- 使用者選的那台沒開機、或那台的 AnyBot 沒開沒登入,就不在清單上:Bot 先待在自己的電腦,`list_computers` 會寫那台「現在沒有連線」。
|
- 使用者選的那台沒開機、或那台的 AnyBot 沒開沒登入,就不在清單上:Bot 先待在自己的電腦,`list_computers` 會寫那台「現在沒有連線」。
|
||||||
- 在 Bot 自己的電腦上做事不用問,但它看不到使用者的檔案;檔案工具只碰得到自己的資料夾(大家共用同一台)。
|
- 在 Bot 自己的電腦上做事不用問,但它看不到使用者的檔案;檔案工具只碰得到自己的資料夾(大家共用同一台)。
|
||||||
|
|
|
||||||
|
|
@ -306,6 +306,20 @@ pub fn is_busy() -> bool {
|
||||||
BUSY.load(Ordering::SeqCst) > 0
|
BUSY.load(Ordering::SeqCst) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The person asked for an update while the box was in use (or a rebuild failed): run it at the
|
||||||
|
/// next quiet moment (update_when_idle) instead of dropping the request on the floor.
|
||||||
|
static PENDING_UPDATE: AtomicBool = AtomicBool::new(false);
|
||||||
|
/// The next allowed retry of a failed auto-update (longer waits after each failure).
|
||||||
|
fn next_try() -> &'static std::sync::Mutex<Option<(Instant, u32)>> {
|
||||||
|
static N: std::sync::OnceLock<std::sync::Mutex<Option<(Instant, u32)>>> = std::sync::OnceLock::new();
|
||||||
|
N.get_or_init(|| std::sync::Mutex::new(None))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn pending_update() -> bool {
|
||||||
|
PENDING_UPDATE.load(Ordering::SeqCst)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn touch() {
|
pub fn touch() {
|
||||||
shared().lock().unwrap().last_used = Instant::now();
|
shared().lock().unwrap().last_used = Instant::now();
|
||||||
}
|
}
|
||||||
|
|
@ -1328,22 +1342,43 @@ pub fn start_background() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The brain's box is older than this AnyBot (it lacks parts the bots now use: the numbered
|
/// The brain's box is older than this AnyBot (it lacks parts the bots now use: the numbered
|
||||||
/// controls, the browser launcher): update it by itself once nobody has used it for a minute,
|
/// controls, the browser launcher): update it by itself once nobody has used it for a minute.
|
||||||
/// once per version. Files and logins stay (they live in the home volume).
|
/// The first try per version happens on its own; a request the person made while the box was
|
||||||
|
/// busy (PENDING_UPDATE) keeps asking at every quiet moment, and a rebuild that failed retries
|
||||||
|
/// with longer waits (30 s, 60 s, 90 s…). Files and logins stay (they live in the home volume).
|
||||||
fn update_when_idle(idle_secs: u64) {
|
fn update_when_idle(idle_secs: u64) {
|
||||||
static TRIED: Mutex<String> = Mutex::new(String::new());
|
static TRIED: Mutex<String> = Mutex::new(String::new());
|
||||||
if !IS_BRAIN.load(Ordering::SeqCst) || idle_secs < 60 || is_busy() || HELD_BY_PERSON.load(Ordering::SeqCst) || viewer_open() || !update_available() {
|
if !IS_BRAIN.load(Ordering::SeqCst) || idle_secs < 60 || is_busy() || HELD_BY_PERSON.load(Ordering::SeqCst) || viewer_open() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if !update_available() {
|
||||||
|
PENDING_UPDATE.store(false, Ordering::SeqCst);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// A failed rebuild waits: 30 s, 60 s, 90 s… so a broken update can't spam the log.
|
||||||
|
if let Ok(mut n) = next_try().lock() {
|
||||||
|
if let Some((at, _)) = *n {
|
||||||
|
if Instant::now() < at {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let fails = n.as_ref().map(|(_, f)| *f).unwrap_or(0);
|
||||||
|
*n = Some((Instant::now() + Duration::from_secs(30 * u64::from(fails + 1)), fails + 1));
|
||||||
|
}
|
||||||
let want = wanted_version();
|
let want = wanted_version();
|
||||||
|
let asked = PENDING_UPDATE.load(Ordering::SeqCst);
|
||||||
{
|
{
|
||||||
let mut t = TRIED.lock().unwrap_or_else(|e| e.into_inner());
|
let mut t = TRIED.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
if *t == want {
|
if *t == want && !asked {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Only the first try per version announces itself; retries (a busy box) stay quiet.
|
||||||
|
if *t != want {
|
||||||
*t = want.clone();
|
*t = want.clone();
|
||||||
}
|
|
||||||
log(&format!("Bot 的電腦是舊版,自動更新到第 {want} 版(檔案和登入都會保留)。"));
|
log(&format!("Bot 的電腦是舊版,自動更新到第 {want} 版(檔案和登入都會保留)。"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PENDING_UPDATE.store(false, Ordering::SeqCst);
|
||||||
if let Err(e) = rebuild(Rebuild::Update) {
|
if let Err(e) = rebuild(Rebuild::Update) {
|
||||||
log(&format!("自動更新沒有成功:{e}"));
|
log(&format!("自動更新沒有成功:{e}"));
|
||||||
}
|
}
|
||||||
|
|
@ -1573,7 +1608,8 @@ pub fn rebuild(kind: Rebuild) -> Result<(), String> {
|
||||||
return Err("已經有一個更新在進行中。".into());
|
return Err("已經有一個更新在進行中。".into());
|
||||||
}
|
}
|
||||||
if kind == Rebuild::Update && BUSY.load(Ordering::SeqCst) > 0 {
|
if kind == Rebuild::Update && BUSY.load(Ordering::SeqCst) > 0 {
|
||||||
return Err("有 Bot 正在用這台電腦,等它做完再重建(或先停止它)。".into());
|
PENDING_UPDATE.store(true, Ordering::SeqCst);
|
||||||
|
return Err("有 Bot 正在用這台電腦:已排隊,它一閒下來就自動更新。".into());
|
||||||
}
|
}
|
||||||
if kind == Rebuild::Restore && !snapshot_path().exists() {
|
if kind == Rebuild::Restore && !snapshot_path().exists() {
|
||||||
return Err("還沒有儲存過快照,沒有東西可以還原。".into());
|
return Err("還沒有儲存過快照,沒有東西可以還原。".into());
|
||||||
|
|
@ -1592,6 +1628,10 @@ pub fn rebuild(kind: Rebuild) -> Result<(), String> {
|
||||||
match r {
|
match r {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
log(&format!("{}完成。", kind.title()));
|
log(&format!("{}完成。", kind.title()));
|
||||||
|
PENDING_UPDATE.store(false, Ordering::SeqCst);
|
||||||
|
if let Ok(mut n) = next_try().lock() {
|
||||||
|
*n = None;
|
||||||
|
}
|
||||||
let mut s = shared().lock().unwrap();
|
let mut s = shared().lock().unwrap();
|
||||||
s.rebuilding = false;
|
s.rebuilding = false;
|
||||||
s.run = Run::Running;
|
s.run = Run::Running;
|
||||||
|
|
@ -1599,6 +1639,10 @@ pub fn rebuild(kind: Rebuild) -> Result<(), String> {
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log(&format!("{}失敗:{e}", kind.title()));
|
log(&format!("{}失敗:{e}", kind.title()));
|
||||||
|
// An update that broke half way is worth another go when things are quiet.
|
||||||
|
if kind == Rebuild::Update {
|
||||||
|
PENDING_UPDATE.store(true, Ordering::SeqCst);
|
||||||
|
}
|
||||||
let mut s = shared().lock().unwrap();
|
let mut s = shared().lock().unwrap();
|
||||||
s.rebuilding = false;
|
s.rebuilding = false;
|
||||||
s.run = Run::Failed(e);
|
s.run = Run::Failed(e);
|
||||||
|
|
@ -2871,6 +2915,17 @@ pub fn shutdown() {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
/// 更新 pressed while a bot is working is not dropped: it queues and the idle loop runs it.
|
||||||
|
#[test]
|
||||||
|
fn an_update_asked_while_busy_is_queued() {
|
||||||
|
let busy = Busy::new(true);
|
||||||
|
assert!(is_busy());
|
||||||
|
let e = rebuild(Rebuild::Update).unwrap_err();
|
||||||
|
assert!(e.contains("已排隊") && e.contains("自動更新"), "{e}");
|
||||||
|
assert!(pending_update(), "the request must survive the busy box");
|
||||||
|
drop(busy);
|
||||||
|
}
|
||||||
|
|
||||||
/// 復原 reads the snapshot through before it throws the home away: one cut short, empty or
|
/// 復原 reads the snapshot through before it throws the home away: one cut short, empty or
|
||||||
/// gone is refused while the computer is still whole.
|
/// gone is refused while the computer is still whole.
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue