App initialization lifecycle on hub reboot

I'm having trouble with getting my app to initialize when the hub reboots (typically due to a power failure). I have an external specification I need to reload after reboot. I've defended against the problem by lazy-loading if the specification is missing, but the first call fails and it's annoying.

I've read through what I can find in the community history; it appears a few people have had this problem, but I don't see a concise response of (a) what to do and (b) what actually happens after a reboot (as opposed to installing or modifying the app).

Here's the pertinent code:

// Per some community documentation this may be needed to actually initialize 
// the app on hub reboot, but it doesn't appear to happen and it still don't work
// after reboot
subscribe("location", "systemStart", hubRestartHandler)
def hubRestartHandler(evt) {
    initialize()
}

def installed() {
    logInfo 'installed()'
    state.serverInstalled = true
    initialize()
}

def updated() {
    logInfo 'updated()'
    initialize()
}

def initialize() {
    logInfo 'initialize()'
    loadSpec()
}

def uninstalled() {
    logInfo 'uninstalled()'
}

I have my log-level set to "info" but I don't see any of these methods output; the first evidence of my app is when an event to which it's subscribed happens, and it tells me "No spec was loaded" and then calls loadSpec() directly. What am I missing?

Nothing happens to an app per se when the hub is rebooted. Additionally (since I think this is one of your assumptions), initialize() is not a special method in an app. Many authors use one to share code between updated() and installed(), among other possible reasons, but again, the name (or purpose) is not special. (This is different from drivers but even there is only applicable to ones that specify the capability.)

However, the hub (location object, technically) fires an event when the hub boots. What your app can do is subscribe to this event and run your handler in response, similar to other event subscriptions. The subscription might look like:

subscribe(location, "systemStart", "myHandlerMethod")

1 Like

I had already tried the subscription as you suggested, with the unquoted location object, but when I do that I get the following error when I save:

java.sql.SQLException: NULL not allowed for column "INSTALLED_APP_ID"; SQL statement: INSERT INTO EVENT_SUBSCRIPTION (VERSION, FILTER, HANDLER, NAME, INSTALLED_APP_ID, LOCATION_ID) VALUES (1, 0, ?, ?, ?, ?) [23502-214] Query: INSERT INTO EVENT_SUBSCRIPTION (VERSION, FILTER, HANDLER, NAME, INSTALLED_APP_ID, LOCATION_ID) VALUES (1, 0, ?, ?, ?, ?) Parameters: [hubRestartHandler, systemStart, null, 1] on line 451

Did you hit "Install" in your app to install it first? (Where the "Done" button otherwise is.)

Long since - like I said, the app's been running for a while. We just had a power failure over the weekend and I decided to try to fix this little nuisance.

I am using that exact line (except with a different handler name) in my code. Can you pare your app code down to a minimal non-working example that demonstrates the problem? Something else must be going on, and this could help reveal what that is.