[libvirt-users] Callback re-registration after libvirtd restart

Daniel P. Berrange berrange at redhat.com
Wed May 17 13:24:12 UTC 2017


On Wed, May 17, 2017 at 03:08:23PM +0200, Daniel Kučera wrote:
> Hi all,
> 
> I'm using libvirt-go and I following code to listen for lifecycle events:
> 
> func event_listen() {
>     log.Printf("event_listen %s", conf.Libvirt.LocalUrl)
>     hv, err := libvirt.NewConnect(conf.Libvirt.LocalUrl)
> 
>     lifecycleCallback := func(c *libvirt.Connect, d *libvirt.Domain, event
> *libvirt.DomainEventLifecycle) {
>         event_message(c, d, "lifecycle", event)
>     }
> 
>     _, err = hv.DomainEventLifecycleRegister(nil, lifecycleCallback)
>     if err != nil {
>         log.Printf("unable to register event callback")
>         return
>     }
> 
>     log.Printf("Libvirt event listener started")
> 
>     go func() {
>         for err == nil {
>             err = libvirt.EventRunDefaultImpl()
>             log.Printf("EventRunDefaultImpl err: %+v", err)
>         }
>         time.Sleep(time.Second)
>         event_listen()
>     }()
> 
> }
> 
> It works ok until I restart libvirtd (service libvirtd restart). After
> that, the inner go func waits some time and continues without error. But
> the callback is not working anymore.
> 
> My question is, how can I detect hv reconnect (I guess it's happening in
> background) so I know when to reinitialize callbacks?

There is a separate event you can listen to that notifies when the connection
is closed. See the RegisterCloseCallback() method on the Connect object.

Basically register a callback there, and when it fires, unregister your
existing domain event callbacks, and close your existing Connect object
handle. Then fire a goroutine that loops once every few seconds trying
to open a new Connect object, and when that succeeds register new
domain event callbacks.

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|




More information about the libvirt-users mailing list