Browse Source

better error handling for AccountInitializer

tags/v0.1.6
Jonathan Cobb 5 years ago
parent
commit
93bb2e304c
2 changed files with 16 additions and 3 deletions
  1. +8
    -2
      bubble-server/src/main/java/bubble/dao/account/AccountInitializer.java
  2. +8
    -1
      bubble-server/src/main/java/bubble/model/account/Account.java

+ 8
- 2
bubble-server/src/main/java/bubble/dao/account/AccountInitializer.java View File

@@ -10,6 +10,7 @@ import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.cobbzilla.util.daemon.ZillaRuntime.die;
@@ -24,7 +25,13 @@ public class AccountInitializer implements Runnable {
private Account account;
private AccountDAO accountDAO;
private AccountMessageDAO messageDAO;

private AtomicBoolean ready = new AtomicBoolean(false);
public boolean ready() { return ready.get(); }

private AtomicReference<Exception> error = new AtomicReference<>();
public Exception getError() { return error.get(); }
public boolean hasError () { return getError() != null; }

public AccountInitializer(Account account, AccountDAO accountDAO, AccountMessageDAO messageDAO) {
this.account = account;
@@ -32,8 +39,6 @@ public class AccountInitializer implements Runnable {
this.messageDAO = messageDAO;
}

public boolean ready() { return ready.get(); }

@Override public void run() {
try {
boolean success = false;
@@ -62,6 +67,7 @@ public class AccountInitializer implements Runnable {
.setAction(AccountAction.welcome)
.setTarget(ActionTarget.account));
} catch (Exception e) {
error.set(e);
// todo: send to errbit
die("error: "+e, e);
}


+ 8
- 1
bubble-server/src/main/java/bubble/model/account/Account.java View File

@@ -32,6 +32,7 @@ import java.util.List;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.cobbzilla.util.daemon.ZillaRuntime.die;
import static org.cobbzilla.util.daemon.ZillaRuntime.now;
import static org.cobbzilla.util.reflect.ReflectionUtil.copy;
import static org.cobbzilla.util.system.Sleep.sleep;
@@ -142,7 +143,13 @@ public class Account extends IdentifiableBase implements TokenPrincipal {
}
final long start = now();
while (!accountInitializer.ready() && now() - start < INIT_WAIT_TIMEOUT) {
sleep(INIT_WAIT_INTERVAL, "postCreate: waiting for AccountInitializer.ready");
sleep(INIT_WAIT_INTERVAL, "waitForAccountInit: waiting for AccountInitializer.ready");
if (accountInitializer.hasError()) break;
}
if (accountInitializer.hasError()) {
final Exception error = accountInitializer.getError();
if (error instanceof RuntimeException) throw (RuntimeException) error;
return die("waitForAccountInit: "+error);
}
if (now() - start > INIT_WAIT_TIMEOUT && !accountInitializer.ready()) {
throw invalidEx("err.accountInit.timeout");


Loading…
Cancel
Save