Module: ClusterFuncs

Included in:
Appswarm, TestBed
Defined in:
lib/appswarm/cluster_funcs.rb

Instance Method Summary collapse

Instance Method Details

#allUpAndRunningObject



52
53
54
# File 'lib/appswarm/cluster_funcs.rb', line 52

def allUpAndRunning
  @apps.select{|a|a.running==false}.length==0
end

#checkRequirements(appClass) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/appswarm/cluster_funcs.rb', line 8

def checkRequirements(appClass)
  reqs=appClass.requirements
  reqs.each{|req|
    if getApp(req).nil?
      f=@appClasses.select{|klass|klass.providing.member?(req)}
      if f.length==0
        raise "PROBLEM: #{req} not found!"
      else
        debug "STARTING REQUIRED #{f[0]}"
        run(f[0].to_s.gsub(/App$/,""))
      end
      
    end
  }
end

#createInstanceIdObject



153
154
155
# File 'lib/appswarm/cluster_funcs.rb', line 153

def createInstanceId
  Crypt::hexdigest(rand.to_s+Time.new.to_s)
end

#debug(*p) ⇒ Object



138
139
140
# File 'lib/appswarm/cluster_funcs.rb', line 138

def debug(*p)
  log_direct(caller[0],nil,p.map{|x|x.to_s}.join(" "))
end

#getApplicationTypesObject



42
43
44
# File 'lib/appswarm/cluster_funcs.rb', line 42

def getApplicationTypes
  @appClasses
end

#getApps(type) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/appswarm/cluster_funcs.rb', line 27

def getApps(type)
  if type.is_a?(Class)
    return @apps.select{|app|app.is_a?(type)}
  end

  if type.is_a?(String)
    type+="App" unless type=~/App$/
    return @apps.select{|app|app.class.to_s==type}
  end

  @apps.select{|app|
    app.provides?(type)
  }
end

#getLocalApplicationsObject



24
25
26
# File 'lib/appswarm/cluster_funcs.rb', line 24

def getLocalApplications
  @apps
end

#getNewInstanceName(klass) ⇒ Object



3
4
5
6
7
# File 'lib/appswarm/cluster_funcs.rb', line 3

def getNewInstanceName(klass)
  @instanceId||=0
  @instanceId+=1
  klass.to_s+@instanceId.to_s
end

#getOrCreate(appName) ⇒ Object



46
47
48
49
50
# File 'lib/appswarm/cluster_funcs.rb', line 46

def getOrCreate(appName)
  a=getApp(appName)
  a=run(appName) unless a
  a
end

#log_direct(place, instance, what) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/appswarm/cluster_funcs.rb', line 141

def log_direct(place,instance,what)
  place.gsub!(/\/.*\//,'/..../')
  i=""
  i="[#{instance}]" if instance

  t=Time.new.strftime("%Y-%m-%d %H:%M:%S")
  what.split("\n").each{|line|
    log_line "[#{t}][#{place}]#{i} "+line
  }

end

#log_line(line) ⇒ Object



135
136
137
# File 'lib/appswarm/cluster_funcs.rb', line 135

def log_line(line)
  puts line
end

#run(appName, *params) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/appswarm/cluster_funcs.rb', line 70

def run(appName,*params)
  return nil if @stopping
  log "Starting #{appName}"
  appClass=@appClasses.select{|appClass|
    appName.to_s+"App"==appClass.to_s
  }[0]
  raise "Class #{appName} not Found" unless appClass
  checkRequirements(appClass)


  log "NAME:",appName,params
  a=nil

  @stopMutex.synchronize {
    return if @stopped
    if params.is_a?(Array)
      params=params[0]
    end
    if params.is_a?(Hash)
      return if @apps.select{|app|app.getAppId==params[:appId]}.length>0

      # FIXME: support multiple apps of same type running
      return if @apps.select{|app|
        app.class==appClass
      }.length>0
    end

    #return if getAppById()
    log "Running Testbed #{self}"
    options=@options
    if params
    if params.length>0
      if params[-1].is_a?(Hash)
        options=params[-1]
      end
    end
    end
    a=appClass.new(self,options)
    i=a.object_id
    y=Thread.new(a) {|a|
      Thread.current[:appName2]=appName
      begin
        @apps<<a
        log "TRYING TO START #{a}"

        a.startup
        a.running=true
        a.run
        debug "FINISHED #{a}"
      rescue Exception=>e
        log e,e.backtrace
      end
    }
    loop  do
      sleep 0.01
      break if @apps.member?(a)
    end
    y[:appName]=appName
    @tbedThreads||={}
    @tbedThreads[i]=y
    a
  }
  a
end

#runAndWait(appName, *params) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/appswarm/cluster_funcs.rb', line 62

def runAndWait(appName,*params)
  app=run(appName,*params)
  while app.running==false
    sleep 0.1
    log "WAIT for #{app} to startup"
  end
end

#waitUntilAllUpAndRunningObject



56
57
58
59
60
# File 'lib/appswarm/cluster_funcs.rb', line 56

def waitUntilAllUpAndRunning
  while not allUpAndRunning
    sleep 0.1
  end
end