Class: Wakame::Service::ServiceInstance

Inherits:
Wakame::StatusDB::Model show all
Includes:
ThreadImmutable
Defined in:
lib/wakame/service.rb

Overview

The data model represents a service instance. Status transition: STATUS_INIT -> [STATUS_OFFLINE|STATUS_ONLINE|STATUS_FAIL] -> STATUS_END Progress status: STATUS_NONE, STATUS_MIGRATING, STATUS_PROPAGATING,

Constant Summary

Constants included from AttributeHelper

AttributeHelper::CLASS_TYPE_KEY, AttributeHelper::CONVERT_CLASSES, AttributeHelper::PRIMITIVE_CLASSES

Instance Method Summary collapse

Methods included from ThreadImmutable

#bind_thread, included, #target_thread, #target_thread?, #thread_check

Methods inherited from Wakame::StatusDB::Model

#delete, #dirty?, #id, inherited, #new_record?, #on_after_delete, #on_after_load, #on_before_delete, #on_before_load, #reload, #save

Methods included from AttributeHelper

#dump_attrs, #retrieve_attr_attribute

Instance Method Details

#bind_cloud_host(new_cloud_host) ⇒ Object



824
825
826
827
828
829
830
831
832
833
834
# File 'lib/wakame/service.rb', line 824

def bind_cloud_host(new_cloud_host)
  return if !self.resource.require_agent || self.cloud_host_id == new_cloud_host.id
  raise "The host (#{new_cloud_host.id}) was assigned same service already: #{resource.class}" if new_cloud_host.has_resource_type?(resource)
  
  unbind_cloud_host

  self.cloud_host_id = new_cloud_host.id
  self.save
  
  ED.fire_event(Event::ServiceBoundCloudHost.new(self.id, new_cloud_host.id))
end

#bind_cluster(cluster) ⇒ Object



884
885
886
887
888
889
890
891
892
# File 'lib/wakame/service.rb', line 884

def bind_cluster(cluster)
  return if self.cluster_id == cluster.id

  unbind_cluster
  self.cluster_id = cluster.id
  self.save

  ED.fire_event(Event::ServiceBoundCluster.new(self, cluster))
end

#bind_resource(resource) ⇒ Object



855
856
857
858
859
860
861
862
863
# File 'lib/wakame/service.rb', line 855

def bind_resource(resource)
  return if self.resource_id == resource.id

  unbind_resource
  self.resource_id = resource.id
  self.save

  #ED.fire_event(Event::ServiceBoundCluster.new(self, cluster))
end

#child_instancesObject



921
922
923
924
925
926
927
928
929
# File 'lib/wakame/service.rb', line 921

def child_instances
  ary = []
  self.cluster.dg.children(resource.class).each { |r|
    self.cluster.each_instance(r.class){ |i|
      ary << i
    }
  }
  ary.flatten
end

#cloud_hostObject



818
819
820
821
822
# File 'lib/wakame/service.rb', line 818

def cloud_host
  raise "The associated resource is not needed to have agent." unless self.resource.require_agent
    
  self.cloud_host_id.nil? ? nil : CloudHost.find(self.cloud_host_id)
end

#clusterObject



879
880
881
882
# File 'lib/wakame/service.rb', line 879

def cluster
  return nil if self.cluster_id.nil?
  ServiceCluster.find(self.cluster_id)
end

#export_bindingObject



907
908
909
# File 'lib/wakame/service.rb', line 907

def export_binding
  binding
end

#parent_instancesObject



911
912
913
914
915
916
917
918
919
# File 'lib/wakame/service.rb', line 911

def parent_instances
  ary = []
  self.cluster.dg.parents(resource.class).each { |r|
    self.cluster.each_instance(r.class){ |i|
      ary << i
    }
  }
  ary.flatten
end

#resourceObject



850
851
852
853
# File 'lib/wakame/service.rb', line 850

def resource
  return nil if self.resource_id.nil?
  Resource.find(self.resource_id)
end

#unbind_cloud_hostObject



837
838
839
840
841
842
843
844
845
846
847
# File 'lib/wakame/service.rb', line 837

def unbind_cloud_host
  return if self.cloud_host_id.nil?

  old_item = self.cloud_host
  self.cloud_host_id = nil
  
  self.save

  ED.fire_event(Event::ServiceUnboundCloudHost.new(self.id, old_item.id))
  old_item
end

#unbind_clusterObject



895
896
897
898
899
900
901
902
903
904
# File 'lib/wakame/service.rb', line 895

def unbind_cluster
  return if self.cluster_id.nil?
  old_item = self.cluster_id

  self.cluster_id = nil
  self.save

  ED.fire_event(Event::ServiceUnboundCluster.new(self, old_item))
  old_item
end

#unbind_resourceObject



866
867
868
869
870
871
872
873
874
875
# File 'lib/wakame/service.rb', line 866

def unbind_resource
  return if self.resource_id.nil?
  old_item = self.resource_id

  self.resource_id = nil
  self.save

  #ED.fire_event(Event::ServiceUnboundCluster.new(self, old_item))
  old_item
end

#update_monitor_status(new_status, changed_at = Time.now, fail_message = nil) ⇒ Object



786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/wakame/service.rb', line 786

def update_monitor_status(new_status, changed_at=Time.now, fail_message=nil)
  raise "Invalid status has been assigned: #{new_status}" unless [STATUS_UNKNOWN, STATUS_ONLINE, STATUS_OFFLINE, STATUS_FAIL].member?(new_status)

  if @monitor_status != new_status

    Wakame.log.debug("#{self.class}: #{self.id} update_monitor_status #{new_status}, #{@monitor_status}")
    prev_status = @monitor_status
    @monitor_status = new_status
    @monitor_status_changed_at = changed_at
    
    self.save

    event = Event::ServiceMonitorStatusChanged.new(self.id, new_status, prev_status)
    event.time = changed_at.dup
    ED.fire_event(event)

    tmp_event = nil
    if prev_status != Service::STATUS_ONLINE && new_status == Service::STATUS_ONLINE
      tmp_event = Event::ServiceOnline.new(self.id)
      tmp_event.time = @status_changed_at.dup
    elsif prev_status != Service::STATUS_OFFLINE && new_status == Service::STATUS_OFFLINE
      tmp_event = Event::ServiceOffline.new(self.id)
      tmp_event.time = @status_changed_at.dup
    elsif prev_status != Service::STATUS_FAIL && new_status == Service::STATUS_FAIL
      tmp_event = Event::ServiceFailed.new(self.id, fail_message)
      tmp_event.time = @status_changed_at.dup
    end
    ED.fire_event(tmp_event) if tmp_event

  end
end

#update_status(new_status) ⇒ Object



774
775
776
777
778
779
780
781
782
783
784
# File 'lib/wakame/service.rb', line 774

def update_status(new_status)
  if @status != new_status
    prev_status = @status
    @status = new_status
    @status_changed_at = Time.now
    
    self.save

    ED.fire_event(Event::ServiceStatusChanged.new(self.id, new_status, prev_status))
  end
end