Sha256: 52db6d5ccf6ed581962a9edd5ceaae4033e6da214e1cc55109ec81143f1f903d

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require 'eventmachine'

module Rancher
  module Api
    module Helpers
      module Model
        class RancherWaitTimeOutError < StandardError; end
        class RancherModelError < StandardError; end
        class RancherActionNotAvailableError < StandardError; end

        TIMEOUT_LIMIT = 900

        def self_url
          links['self']
        end

        def reload
          assign_attributes(self.class.find(id).attributes)
        end

        def run(action, data: {})
          url = actions[action.to_s]
          raise RancherActionNotAvailableError, "Available actions: '#{actions.inspect}'" if url.blank?
          handle_response(self.class.post(url, data))
        end

        def handle_response(response)
          raise RancherModelError, response.inspect if response.type.eql?('error')
        end

        def wait_for_state(desired_state)
          EM.run do
            EM.add_timer(TIMEOUT_LIMIT) do
              raise RancherWaitTimeOutError, "Timeout while waiting for transition to: #{desired_state}"
            end
            EM.tick_loop do
              reload
              current_state = state
              if current_state.eql?(desired_state.to_s)
                Logger.log.info "state changed from: #{current_state} => #{desired_state}"
                EM.stop
              else
                Logger.log.info "waiting for state change: #{current_state} => #{desired_state}"
                sleep(1)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rancher-api-0.5.0 lib/rancher/api/helpers/model.rb