Class: Wakame::Actions::StartService

Inherits:
Wakame::Action show all
Defined in:
lib/wakame/actions/start_service.rb

Constant Summary

Constants included from AttributeHelper

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

Instance Attribute Summary

Attributes inherited from Wakame::Action

#action_manager

Instance Method Summary collapse

Methods inherited from Wakame::Action

#acquire_lock, #actor_request, #agent_monitor, #all_subactions_complete?, #flush_subactions, #master, #notes, #notify, #on_canceled, #service_cluster, #status=, #subactions, #sync_actor_request, #trigger_action, #walk_subactions

Methods included from ThreadImmutable

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

Methods included from AttributeHelper

#dump_attrs, #retrieve_attr_attribute

Constructor Details

#initialize(service_instance) ⇒ StartService

Returns a new instance of StartService.



5
6
7
# File 'lib/wakame/actions/start_service.rb', line 5

def initialize(service_instance)
  @service_instance = service_instance
end

Instance Method Details

#on_failedObject



84
85
86
87
88
# File 'lib/wakame/actions/start_service.rb', line 84

def on_failed
  StatusDB.barrier {
    @service_instance.update_status(Service::STATUS_FAIL)
  }
end

#runObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wakame/actions/start_service.rb', line 9

def run
  # Skip to act when the service is having below status.
  if @service_instance.status == Service::STATUS_STARTING || @service_instance.status == Service::STATUS_ONLINE
    Wakame.log.info("Ignore to start the service as is being or already Online: #{@service_instance.resource.class}")
    return
  end

  acquire_lock { |lst|
    lst << @service_instance.resource.class.to_s
  }

  if @service_instance.resource.require_agent
    raise "The service is not bound cloud host object: #{@service_instance.id}" if @service_instance.cloud_host_id.nil?

    unless @service_instance.cloud_host.mapped?
      acquire_lock { |lst|
        lst << Service::AgentPool.class.to_s
      }
      
      # Try to arrange agent from existing agent pool.
      StatusDB.barrier {
        next if Service::AgentPool.instance.group_active.empty?
        agent2host = cluster.agents.invert
        
        Service::AgentPool.instance.group_active.keys.each { |agent_id|
          agent = Service::Agent.find(agent_id)
          if !agent.has_resource_type?(@service_instance.resource) &&
              agent2host[agent_id].nil? && # This agent is not mapped to any cloud hosts.
              @service_instance.cloud_host.vm_spec.satisfy?(agent.vm_attr)
            
            @service_instance.cloud_host.map_agent(agent)
            break
          end
        }
      }
      
      # Start new VM when the target agent is still nil.
      unless @service_instance.cloud_host.mapped?
        inst_id_key = "new_inst_id_" + Wakame::Util.gen_id
        trigger_action(LaunchVM.new(inst_id_key, @service_instance.cloud_host.vm_spec))
        flush_subactions
        
        StatusDB.barrier {
          agent = Service::Agent.find(notes[inst_id_key])
          raise "Cound not find the specified VM instance \"#{notes[inst_id_key]}\"" if agent.nil?
          @service_instance.cloud_host.map_agent(agent)
        }
      end
      
      raise "Could not find the agent to be assigned to : #{@service_instance.resource.class}" unless @service_instance.cloud_host.mapped?

      @service_instance.resource.on_enter_agent(@service_instance, self)
    end
    
    raise "The assigned agent \"#{@service_instance.cloud_host.agent_id}\" for the service instance #{@service_instance.id} is not online."  unless @service_instance.cloud_host.status == Service::Agent::STATUS_ONLINE

  end
  

  StatusDB.barrier {
    @service_instance.update_status(Service::STATUS_STARTING)
  }
  
  if @service_instance.resource.require_agent
    trigger_action(DeployConfig.new(@service_instance))
    flush_subactions
  end

  @service_instance.resource.start(@service_instance, self)
  
  trigger_action(NotifyParentChanged.new(@service_instance))
  flush_subactions

end