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

Events not firing #75

Open
Hibryda opened this issue Aug 26, 2016 · 6 comments
Open

Events not firing #75

Hibryda opened this issue Aug 26, 2016 · 6 comments

Comments

@Hibryda
Copy link

Hibryda commented Aug 26, 2016

I have a piece of code:

vapp.hyperv = new virt.Hypervisor(vapp.hyperv_string)
  vapp.hyperv.connectAsync()
    .then(() =>
      vapp.domain = vapp.hyperv.lookupDomainByNameAsync(name)
    )
    .then((domain) => {
      vapp.domain = domain
      vapp.ev_lifecycle = domain.registerEvent(
        {evtype:virt.VIR_DOMAIN_EVENT_ID_LIFECYCLE},
        function (hyp, dom, data) {
            console.log(hyp, dom, data)
      })
    return vapp.domain.getInfoAsync()
    })
...

After running the above I get the following error:
libvirt: error : internal error: could not initialize domain event timer

Why is it so?

@atoy40
Copy link
Contributor

atoy40 commented Aug 27, 2016

Hello,

you've to call virt.startEventLoop() first to register libvirt event manager with the nodejs event loop.

Anthony.

@Hibryda
Copy link
Author

Hibryda commented Aug 27, 2016

Thanks. Will try.

@Hibryda
Copy link
Author

Hibryda commented Aug 28, 2016

Ok, tried with calling virt.startEventLoop() . It actually works somehow, as it now reacts to events. But not the one that is attached. Just for quick try I attached reboot event and rebooted the vm form virt-manager. The output from console of my app shows GENERIC CALLBACK CALLEDGENERIC CALLBACK CALLED. But not the callback I attached. More, the callback gets executed immediately when registration performs. I've checked in the source and arguments for event registration should be [object, function]. So doing properly. No idea what am I doing wrong now.

@Hibryda
Copy link
Author

Hibryda commented Aug 28, 2016

Dug a little further and see that generic is attached to reboot. More, generic emits no event, just prints the above.
The above changes actually nothing as I still have no clue how to register event without calling callback immediately.

@atoy40
Copy link
Contributor

atoy40 commented Sep 1, 2016

@Hibryda

First, use registerEventAsync (the promised version of the function) instead of registerEvent. Then, the libvirt domain acts as a EventEmitter, so you can use something looking like :

.then((domain) => {
    vapp.domain = domain;
    return domain.registerEventAsync({ evtype: virt.VIR_DOMAIN_EVENT_ID_LIFECYCLE })
})
.then((callbackId) => {
    vapp.domain.on('lifecycleEvent', (data) => {
        console.log("lifecycleEvent fired : "+JSON.stringify(data));
    });
    return vapp.domain.getInfoAsync();
})
.then((info)  => {
    ....
}

You can use callbackId to unregister the event handling :

domain.unregisterEventAsync(callbackId).then(.....);

Anthony.

@Hibryda
Copy link
Author

Hibryda commented Sep 1, 2016

I just thought that there is a way to register an event and attach function in one step (like addEventListener). Apparently must do it in two steps.
Thanks for the answer.

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