Skip to content

Commit

Permalink
Fix issue with restarting and crashing bot
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Apr 21, 2020
1 parent 075a0c8 commit c0178f8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 46 deletions.
2 changes: 1 addition & 1 deletion bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ winapi = { version = "0.3.8", features = ["shellapi", "impl-default"] }

[features]
default = ["windows"]
windows = []
windows = []
2 changes: 1 addition & 1 deletion bot/src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<T> Bus<T> {
}
}

for i in remove {
for i in remove.into_iter().rev() {
inner.subs.swap_remove(i);
}
}
Expand Down
50 changes: 24 additions & 26 deletions bot/src/irc/currency_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,35 +166,33 @@ impl command::Handler for Handler {
let user = ctx.user.clone();
let currency = currency.clone();

ctx.spawn(async move {
let result = currency
.balance_add(user.channel(), &boosted_user, amount)
.await;
let result = currency
.balance_add(user.channel(), &boosted_user, amount)
.await;

match result {
Ok(()) => {
if amount >= 0 {
user.respond(format!(
"Gave {user} {amount} {currency}!",
user = boosted_user,
amount = amount,
currency = currency.name
));
} else {
user.respond(format!(
"Took away {amount} {currency} from {user}!",
user = boosted_user,
amount = -amount,
currency = currency.name
));
}
}
Err(e) => {
user.respond("failed to boost user, sorry :(");
log_err!(e, "failed to modify currency");
match result {
Ok(()) => {
if amount >= 0 {
user.respond(format!(
"Gave {user} {amount} {currency}!",
user = boosted_user,
amount = amount,
currency = currency.name
));
} else {
user.respond(format!(
"Took away {amount} {currency} from {user}!",
user = boosted_user,
amount = -amount,
currency = currency.name
));
}
}
});
Err(e) => {
user.respond("failed to boost user, sorry :(");
log_err!(e, "failed to modify currency");
}
}
}
Some("windfall") => {
ctx.check_scope(Scope::CurrencyWindfall)?;
Expand Down
26 changes: 13 additions & 13 deletions bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ fn main() -> Result<(), Error> {
let mut error_backoff = backoff::ExponentialBackoff::default();
error_backoff.current_interval = time::Duration::from_secs(30);
error_backoff.initial_interval = time::Duration::from_secs(30);
error_backoff.max_elapsed_time = None;

let mut current_backoff;
let mut errored = false;
Expand Down Expand Up @@ -326,25 +327,24 @@ fn main() -> Result<(), Error> {
break;
}

if let Some(current_backoff) = current_backoff.as_ref() {
if let Some(current_backoff) = current_backoff.clone() {
log::info!(
"Restarting in {}...",
utils::compact_duration(current_backoff)
utils::compact_duration(&current_backoff)
);

let system = system.clone();

let system_interrupt = async move {
future::select(
system.wait_for_shutdown().boxed(),
system.wait_for_restart().boxed(),
)
.await;
};

let delay = tokio::time::delay_for(*current_backoff);

let _ = runtime.block_on(future::select(system_interrupt.boxed(), delay));
runtime.block_on(async move {
tokio::select! {
_ = system.wait_for_shutdown() => {
}
_ = system.wait_for_restart() => {
}
_ = tokio::time::delay_for(current_backoff) => {
}
}
});
}

if !errored {
Expand Down
8 changes: 3 additions & 5 deletions bot/src/module/promotions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,9 @@ impl super::Module for Module {
let promotions = promotions.clone();
let sender = sender.clone();

tokio::spawn(async move {
if let Err(e) = promote(promotions, sender) {
log::error!("failed to send promotion: {}", e);
}
});
if let Err(e) = promote(promotions, sender) {
log::error!("failed to send promotion: {}", e);
}
}
}
}
Expand Down

0 comments on commit c0178f8

Please sign in to comment.