<div dir="ltr"><div><div><div><div>Hi Daniel,<br><br></div>I tried that but it doesn't work:<br><br>func libvirt_close_callback(conn *libvirt.Connect, reason libvirt.ConnectCloseReason){<br>    log.Printf("close callback: %+v", reason)<br>}<br><br>func event_listen() {<br>    log.Printf("event_listen %s", conf.Libvirt.LocalUrl)<br>    hv, err := libvirt.NewConnect(conf.Libvirt.LocalUrl)<br><br>    err = hv.RegisterCloseCallback(libvirt_close_callback)<br>    if err != nil {<br>        log.Printf("unable to register close callback")<br>        return<br>    }<br>...<br><br></div>The callback fires only when I kill my app, <br><br>^CGot signal:%!(EXTRA syscall.Signal=interrupt)<br>close callback: 0<br><br>but not when I restart libvirtd. I tried using both local and remote connect URIs: qemu+ssh://<a href="http://10.130.16.101/system">10.130.16.101/system</a>, qemu+ssh://localhost/system, qemu+ssh:///system<br><br></div>Thanks.<br><br></div>D.<br><div><div><div><br></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-05-17 15:24 GMT+02:00 Daniel P. Berrange <span dir="ltr"><<a href="mailto:berrange@redhat.com" target="_blank">berrange@redhat.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Wed, May 17, 2017 at 03:08:23PM +0200, Daniel Kučera wrote:<br>
> Hi all,<br>
><br>
> I'm using libvirt-go and I following code to listen for lifecycle events:<br>
><br>
> func event_listen() {<br>
>     log.Printf("event_listen %s", conf.Libvirt.LocalUrl)<br>
>     hv, err := libvirt.NewConnect(conf.<wbr>Libvirt.LocalUrl)<br>
><br>
>     lifecycleCallback := func(c *libvirt.Connect, d *libvirt.Domain, event<br>
> *libvirt.DomainEventLifecycle) {<br>
>         event_message(c, d, "lifecycle", event)<br>
>     }<br>
><br>
>     _, err = hv.<wbr>DomainEventLifecycleRegister(<wbr>nil, lifecycleCallback)<br>
>     if err != nil {<br>
>         log.Printf("unable to register event callback")<br>
>         return<br>
>     }<br>
><br>
>     log.Printf("Libvirt event listener started")<br>
><br>
>     go func() {<br>
>         for err == nil {<br>
>             err = libvirt.EventRunDefaultImpl()<br>
>             log.Printf("<wbr>EventRunDefaultImpl err: %+v", err)<br>
>         }<br>
>         time.Sleep(time.Second)<br>
>         event_listen()<br>
>     }()<br>
><br>
> }<br>
><br>
> It works ok until I restart libvirtd (service libvirtd restart). After<br>
> that, the inner go func waits some time and continues without error. But<br>
> the callback is not working anymore.<br>
><br>
> My question is, how can I detect hv reconnect (I guess it's happening in<br>
> background) so I know when to reinitialize callbacks?<br>
<br>
</div></div>There is a separate event you can listen to that notifies when the connection<br>
is closed. See the RegisterCloseCallback() method on the Connect object.<br>
<br>
Basically register a callback there, and when it fires, unregister your<br>
existing domain event callbacks, and close your existing Connect object<br>
handle. Then fire a goroutine that loops once every few seconds trying<br>
to open a new Connect object, and when that succeeds register new<br>
domain event callbacks.<br>
<br>
Regards,<br>
Daniel<br>
<span class="HOEnZb"><font color="#888888">--<br>
|: <a href="https://berrange.com" rel="noreferrer" target="_blank">https://berrange.com</a>      -o-    <a href="https://www.flickr.com/photos/dberrange" rel="noreferrer" target="_blank">https://www.flickr.com/photos/<wbr>dberrange</a> :|<br>
|: <a href="https://libvirt.org" rel="noreferrer" target="_blank">https://libvirt.org</a>         -o-            <a href="https://fstop138.berrange.com" rel="noreferrer" target="_blank">https://fstop138.berrange.com</a> :|<br>
|: <a href="https://entangle-photo.org" rel="noreferrer" target="_blank">https://entangle-photo.org</a>    -o-    <a href="https://www.instagram.com/dberrange" rel="noreferrer" target="_blank">https://www.instagram.com/<wbr>dberrange</a> :|<br>
</font></span></blockquote></div><br></div>