Skip to content

Commit

Permalink
Merge pull request #15 from sford/master
Browse files Browse the repository at this point in the history
improve error handling when libvirt is being used by multiple processes at the same time
  • Loading branch information
plribeiro3000 committed Mar 1, 2016
2 parents 31a6b1d + 6e7d4c2 commit 505675f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
43 changes: 25 additions & 18 deletions lib/fog/libvirt/requests/compute/list_domains.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def list_domains(filter = { })
client.list_defined_domains.each { |name| data << client.lookup_domain_by_name(name) } unless filter[:defined] == false
client.list_domains.each { |id| data << client.lookup_domain_by_id(id) } unless filter[:active] == false
end
data.compact.map { |d| domain_to_attributes d }
data.compact.map { |d| domain_to_attributes d }.compact
end
end

Expand Down Expand Up @@ -51,23 +51,30 @@ def domain_interfaces xml

def domain_to_attributes(dom)
states= %w(nostate running blocked paused shutting-down shutoff crashed)
{
:id => dom.uuid,
:uuid => dom.uuid,
:name => dom.name,
:max_memory_size => dom.info.max_mem,
:cputime => dom.info.cpu_time,
:memory_size => dom.info.memory,
:cpus => dom.info.nr_virt_cpu,
:autostart => dom.autostart?,
:os_type => dom.os_type,
:active => dom.active?,
:display => domain_display(dom.xml_desc),
:boot_order => boot_order(dom.xml_desc),
:nics => domain_interfaces(dom.xml_desc),
:volumes_path => domain_volumes(dom.xml_desc),
:state => states[dom.info.state]
}

begin
{
:id => dom.uuid,
:uuid => dom.uuid,
:name => dom.name,
:max_memory_size => dom.info.max_mem,
:cputime => dom.info.cpu_time,
:memory_size => dom.info.memory,
:cpus => dom.info.nr_virt_cpu,
:autostart => dom.autostart?,
:os_type => dom.os_type,
:active => dom.active?,
:display => domain_display(dom.xml_desc),
:boot_order => boot_order(dom.xml_desc),
:nics => domain_interfaces(dom.xml_desc),
:volumes_path => domain_volumes(dom.xml_desc),
:state => states[dom.info.state]
}
rescue ::Libvirt::RetrieveError, ::Libvirt::Error
# Catch libvirt exceptions to avoid race conditions involving
# concurrent libvirt operations (like from another process)
return nil
end
end
end

Expand Down
10 changes: 8 additions & 2 deletions lib/fog/libvirt/requests/compute/list_volumes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ def list_volumes(filter = { })
if filter.keys.empty?
raw_volumes do |pool|
pool.list_volumes.each do |volume_name|
data << volume_to_attributes(pool.lookup_volume_by_name(volume_name))
begin
data << volume_to_attributes(pool.lookup_volume_by_name(volume_name))
rescue ::Libvirt::RetrieveError
# Catch libvirt exceptions to avoid race conditions involving
# concurrent libvirt operations (like from another process)
next
end
end
end
else
Expand All @@ -33,7 +39,7 @@ def volume_to_attributes(vol)
:allocation => bytes_to_gb(vol.info.allocation),
:capacity => bytes_to_gb(vol.info.capacity),
}
rescue Libvirt::RetrieveError
rescue ::Libvirt::RetrieveError, ::Libvirt::Error
return nil # If there are issues during stat of volume file
end
end
Expand Down

0 comments on commit 505675f

Please sign in to comment.