Imps and Agents .. revisited

I thought I had a handle on imp/impee/agent associations and behaviors, but some recent observations have me scratching my head again … I’m hoping someone can square me away. Here’s my situation …

I’ve got agent code for one of my models that contains the following …

device.onconnect(function() {
// some Twilio notification stuff
server.log(“device now online”);
});

Everything (server logs / notifications) works as expected with an impee running that particular model.

I now move the imp from that device over to another impee, which is associated with an entirely different code model (including its agent code). Further, I’ve got that imp/impee combination currently running and in deep-sleep state, (confirmed by its server logs).

Now, here’s the thing I don’t get … the agent associated with the first (i.e. now non-operating, imp-less) impee/model continues to occasionally send out server logs and notifications. I don’t understand why.

(Am I just doing the wrong thing here? Should I just retain common agent code for models that might be used with impees that I might swap an imp between … rather than have different agent code for the different models?)

I’ve got a few follow up questions, and am happy to provide some more details if helpful, but I think maybe I’ll just leave it at this for moment to get the discussion started.

Thanks,
Larry

Right now, agents continue to run even if their imp moves to another impee. This will change soon.

The impee ID is what the agent is bound to, not the imp.

Thanks Hugo.

Logically, I was thinking it made sense for the agent (like imp) code to be bound to the impee, but then got thrown off when I saw the agent for the non-operational impee continuing to run.

Thanks for clearing that up, and indicating a change is in the works.

This is a slightly different follow-up topic, more along the lines of “Agent Restarts”, but I’ll just keep it in this Agent thread I previously started.

From before - I use the Agent code snippet …
device.onconnect(function() { // some Twilio notification stuff server.log("device now online"); });
… to see when a device comes online.

I get unexpected notifications however, indicating that the Agent is perhaps re-starting, unrelated to my device actually coming online or restarting.

a.) is there a mechanism to find out at what times the Agent restarts (due to server upgrades, etc.)? I believe I recall there was a page somewhere indicating server status, but I can’t find it again.

b.) is there a way around this … i.e. to only send out my logging/notifications with actual device coming online events, rather than Agent restarts unrelated to the actual device?

Thanks,
Larry

Right now, agents on the development server restart quite often (they are still in beta). In time we’re adding pre-shutdown notifications for schedule maintenance - ie you will be able to register a handler that gets given a small amount of time to save state - but that is not there yet.

You can save state whenever you fancy with server.save, and I’d recommend you do this if you have state that needs preserving.

Do you see any imp online-offline notifications in the log? Generally the agent notifications are send when these are logged.

In my case, I don’t need to preserve state … I’m just trying to receive notifications when a device goes offline, then back on. The more important thing is the offline notification (which works fine) … I was just observing spurious online notifications (with the device already in that state), and trying to understand if these were correlated to Agent restarts. (I do appreciate that there are frequent restarts on the development server. I was just trying to understand if there is a log of when these occurred that I could compare to my observations).

I didn’t happen to record the log snippet when it last occurred earlier this afternoon, but am fairly certain in this particular instance that I did not see an associated online-offline notification. I’ll be sure to look for this if/when I get another unexpected device.onconnect() event.

Thanks for the response.

I don’t actually think you should get an online notification on an agent restart, so that’s a bit confusing. If you log at the top of your agent code, you’ll be able to see when the agent restarts?

Ok, I think what you’re saying then is that just because an agent restarts, that shouldn’t cause the device.onconnect() function (and my enclosed notification/logging statements) to be executed? I do note however that anytime I restart code that the agent restarts, and the device.onconnect() function does indeed execute. How is this different from an Electric Imp induced server/agent restart?

Good idea to just stick a server.log(“Agent starting”) statement at the beginning of the agent code. (Simple … I shoulda thought of that!) Thanks.

I’ll keep an eye on it and try to figure out what causes/d the spurious notification … maybe something with my server, something with Twilio, etc.? Not too big of a deal, just something interesting I’m trying to track down.

Thanks again,
Larry

When you hit restart in the IDE, you restart both the device and the agent, which is different from an agent restart when the device is just sitting there happily… but still, a bit confusing. Will dig more.

Got it. Only thing I wanted to be clear on was, is device.onconnect() also called for a server-only restart, or just when I restart both imp/agent code with the IDE ? (I would think only the latter (IDE) case, but just wanted to be certain).

We can probably suspend the discussion for the moment as I haven’t observed any other unexpected notifications since the other day. I’ve got the add’l agent restart logging in now, and might pick things up again if I note any add’l unexplained behavior.

Thanks again.

Me again … (feeling perhaps like I’m beating a dead horse).

Well, encountered a new twist this morning that I hadn’t seen before. Now, I’ve got a device that’s offline … but the agent device.onconnect( ) function got called with an apparent server side restart this morning (around 10:45a PDT ?)

I’m still apparently missing something quite fundamental here … why should the .onconnect( ) function execute when the agent’s device is not even online?

It shouldn’t, that really sounds like a bug.

Thanks. Yea, I’ve got a device that’s been offline for a bit, but agent restarts (I just did it via IDE now) definitely seem to execute the device.onconnect( ) function I have in there. I’ll PM you the MAC if you want to have a look. (BTW, nice addition to have the device/agent status shown in the IDE window title bars … hadn’t noticed this until seeing your post on the other thread).