deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [12/13] git commit: Core: Added .wait_for! method to Instance model
Date Tue, 24 Jul 2012 07:39:48 GMT
Core: Added .wait_for! method to Instance model

This method implement very basic polling mechanism
that can be used to poll different instance states or
conditions (like IP address is ready.)

Signed-off-by: Michal fojtik <mfojtik@redhat.com>


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/6bb0bacd
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/6bb0bacd
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/6bb0bacd

Branch: refs/heads/master
Commit: 6bb0bacd07a84b260b05cc8b832a89847ca2e7fa
Parents: d903f88
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Mon Jul 23 14:34:56 2012 +0200
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Mon Jul 23 20:08:07 2012 +0200

----------------------------------------------------------------------
 server/lib/deltacloud/models/instance.rb |   39 +++++++++++++++++++++++++
 1 files changed, 39 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/6bb0bacd/server/lib/deltacloud/models/instance.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/models/instance.rb b/server/lib/deltacloud/models/instance.rb
index 465451f..9bf9f4a 100644
--- a/server/lib/deltacloud/models/instance.rb
+++ b/server/lib/deltacloud/models/instance.rb
@@ -14,8 +14,12 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
+require 'timeout'
+
 class Instance < BaseModel
 
+  include Timeout
+
   attr_accessor :owner_id
   attr_accessor :image_id
   attr_accessor :name
@@ -73,4 +77,39 @@ class Instance < BaseModel
     return true unless authn_error.nil?
   end
 
+  # This method will pool the instance until condition is true
+  # Will raise 'Timeout' when it reach retry count
+  #
+  # default opts[:retries] => 10
+  # default opts[:time_between_retry] => 10 (seconds)
+  # default opts[:timeout] => 60 (seconds) -> single request timeout
+  #
+  # opts[:before] => Proc -> executed 'before' making each request
+  # opts[:after] => Proc -> executed 'after' making each request
+  #
+  def wait_for!(driver, opts={}, &block)
+    opts[:retries] ||= 10
+    opts[:time_between_retry] ||= 10
+    opts[:timeout] ||= 60
+    opts[:retries].downto(0) do |r|
+      result = begin
+        timeout(opts[:timeout]) do
+          if opts[:before]
+            new_instance = opts[:before].call(r) { driver.instance(:id => self.id) }
+          else
+            new_instance = driver.instance(:id => self.id)
+          end
+          ((yield new_instance) == true) ? new_instance : false
+        end
+      rescue Timeout::Error
+        false
+      ensure
+        opts[:after].call(r) if opts[:after]
+      end
+      return result unless result == false
+      sleep(opts[:time_between_retry])
+    end
+    raise Timeout::Error
+  end
+
 end


Mime
View raw message