Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wakeGraceTime wait before executing wakeCommand #59

Closed
BTPankow opened this issue Mar 5, 2019 · 6 comments
Closed

wakeGraceTime wait before executing wakeCommand #59

BTPankow opened this issue Mar 5, 2019 · 6 comments
Milestone

Comments

@BTPankow
Copy link

BTPankow commented Mar 5, 2019

Backstory

Based my interpretation of the README, it looks like the wakeCommand should execute after wakeGraceTime number of seconds have elapsed.

However, it currently looks like the wakeCommand is executed immediately after the wol request.

Am I misinterpreting the documentation? (listed below)

Key Description Required
wakeGraceTime Number of seconds to wait after wake-up before checking online status and issuing the wakeCommand, default 45 No

Issue

I have a slightly unusual use-case, since I would like to wake a virtual machine running on a host computer that may or may not be already powered on.

I was hoping the wakeCommand would be executed after the wakeGraceTime so that I could turn on the vm through ssh after powering on the host.

Environment

  • homebridge-wol version: master branch (36cdd0f)

Configuration

My configuration looks like this:

        {
            "accessory": "NetworkDevice",
            "name": "<vm-name>",
            "mac": "<host-mac-address>",
            "ip": "<vm-ip>",
            "pingInterval": 10,
            "wakeGraceTime": 45,
            "wakeCommand": "ssh <host-user>@<host-ip> <start-vm-cmd>",
            "shutdownGraceTime": 15,
            "shutdownCommand": "ssh <host-user>@<host-ip> <shutdown-vm-cmd>"
        }

Log

When I follow these steps:

  1. Run DEBUG=* homebridge -D -P ~/homebridge-wol/
  2. Let it run for one minute
  3. Tap <vm-name> switch

I get the following log (relevant section):

[2019-3-4 21:24:01] [<vm-name>] NetworkDevice awake cycle started for "<vm-name>" (<vm-ip-address>)
[2019-3-4 21:24:01] [<vm-name>] Attempting to wake up "<vm-name>" (<vm-ip-address>)
[2019-3-4 21:24:01] [<vm-name>] NetworkDevice "<vm-name>" (<vm-ip-address>) went from status "Offline" to "Waking Up"
[2019-3-4 21:24:01] [<vm-name>] Stopping pinger
[2019-3-4 21:24:01] [<vm-name>] Attempting to wake up "<vm-name>" (<vm-ip-address>) using "ssh <host-user>@<host-ip> <start-vm-cmd>"
[2019-3-4 21:24:01] [<vm-name>] Stopping pinger
[2019-3-4 21:24:01] [<vm-name>] Starting pinger at an interval of 10000 milli seconds
[2019-3-4 21:24:02] [<vm-name>] An error occured while trying to wake "<vm-name>" (<vm-ip-address>): Error: Command failed: ssh <host-user>@<host-ip> <start-vm-cmd>
ssh: connect to host <host-ip-address> port 22: No route to host

[2019-3-4 21:24:02] [<vm-name>] Stopping pinger
[2019-3-4 21:24:02] [<vm-name>] Starting pinger at an interval of 10000 milli seconds
[2019-3-4 21:24:02] [<vm-name>] Pinger can't see device; setting to Offline.
[2019-3-4 21:24:02] [<vm-name>] NetworkDevice "<vm-name>" (<vm-ip-address>) went from status "Waking Up" to "Offline"

Notes

If there is supposed to be a delay before issuing the wakeCommand, the following change worked for me:

diff --git a/lib/network-device.js b/lib/network-device.js
index 9492e8d..f196ae7 100644
--- a/lib/network-device.js
+++ b/lib/network-device.js
@@ -154,13 +154,16 @@ class NetworkDevice {

     // Wake up using a wake command if available
     if (this.config.wakeCommand) {
-      exec(this.config.wakeCommand, error => {
-        if (error)
-          this.log('An error occured while trying to wake "%s" (%s): %s', this.config.name, this.config.ip || 'unknown ip', error);
+      setTimeout(() => {
+        exec(this.config.wakeCommand, error => {
+          this.log('Attempting to wake up "%s" (%s) using "%s"', this.config.name, this.config.ip || 'unknown ip', this.config.wakeCommand);
+          if (error)
+            this.log('An error occured while trying to wake "%s" (%s): %s', this.config.name, this.config.ip || 'unknown ip', error);

-        if (this.pinger)
-          this.pinger.start();
-      });
+          if (this.pinger)
+            this.pinger.start();
+        })
+      }, this.config.wakeGraceTime);

       woke = true;
     }
@AlexGustafsson
Copy link
Owner

You’re absolute correct. I’m a at fault for not documenting the regression better. This is actually one of the main reason that I’ve not yet pushed 3.3.

The wake command is intended to execute after the wake grace time and it should be done so in any published version.

I’m sorry for any inconvenience this might have caused. I’ll add a note on the current state of the master branch in the README.

If you’ve got the time, you’re free to post a PR and I’ll accept it. Make sure that this.config.wakeGraceTime has a sane default (set in the constructor).

@AlexGustafsson AlexGustafsson added this to the 3.3 milestone Mar 5, 2019
@BTPankow
Copy link
Author

BTPankow commented Mar 6, 2019

No problem, thanks for the quick response! I initially noticed it on the npm version, and it seems to be in the 3.2.4 tagged version, so it made me question if I was misinterpreting things. (https://github.com/AlexGustafsson/homebridge-wol/blob/ver-3.2.4/lib/network-device.js#L156 looks like the wakeCommand is executed right after wol)

I'd be happy to get a PR together for this soon.

One question about wakeCommand I have: is it something we'd want to execute after the system has been seen as "on"? In my case, I'd prefer not to wait for the entire wakeGraceTime if the host computer is already on, but I'm not sure if that is the same for other use cases.

For example, I was thinking that the wakeGraceTime would be useful as a grace time after the machine has first been booted to give it time to set things up (like ssh). If the machine is already on then I'd prefer to not wait for a long wakeGraceTime that incorporates the entire boot time. Plus, I'd have a better chance of timing the wakeCommand correctly in case the machine takes extra long to boot up.

Basically, this is the logic I would be interested in:

  1. Try to wake up machine (if mac specified)
  2. Wait for computer to be "online" (if pingCommand or ip specified)
  3. Wait for wakeGraceTime
  4. Execute wakeCommand

Of course, this would kind of change the definition of the wakeGraceTime, so I could see leaving it be. However, this would be a useful program flow for my purposes and possibly other use cases.

@AlexGustafsson
Copy link
Owner

Hi! Sorry for missing your comment / responding so late.

You're still welcome to open a PR. Implement it the way you'd like, but try to make it additive so that the previous behavior still works. Perhaps a helloCommand or something similar would be in order?

@AlexGustafsson
Copy link
Owner

There's now an untested update to the code where the wake command should be called after the wait grace time.

@AlexGustafsson
Copy link
Owner

I've gone ahead and published 3.2.5 which should contain a fix for this issue.

@AlexGustafsson
Copy link
Owner

Closing as this should work now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants