Class: Application

Inherits:
Object
  • Object
show all
Includes:
DRbUndumped
Defined in:
lib/appswarm/application.rb

Overview

Application is the basic class for all applications and services within appswarm.

Direct Known Subclasses

GlobalService, HTTPApp, NetService, Service

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster, options) ⇒ Application

Returns a new instance of Application.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/appswarm/application.rb', line 15

def initialize(cluster,options)
  @running=false
  @threads=[]
  @cluster=cluster
  @options=options
  @provides={}
  @appName=shortName.downcase

  @appId=@options[:appId]
  @appId||=createAppId
  if @options[:appName]
    @appName=@options[:appName]
  elsif @options[@appName]
    @appName=@options[@appName]
  end
  
  
  @quit=false
  @quitMutex=Mutex.new
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



13
14
15
# File 'lib/appswarm/application.rb', line 13

def cluster
  @cluster
end

#runningObject

Returns the value of attribute running.



14
15
16
# File 'lib/appswarm/application.rb', line 14

def running
  @running
end

Class Method Details

.getAppDir(name) ⇒ Object

gets the (absolute) application-directory for the given class(-name)



151
152
153
# File 'lib/appswarm/application.rb', line 151

def self.getAppDir(name)
  File.expand_path("../../../apps/#{name.to_s.gsub(/App$/,'').dirName}",__FILE__)
end

.protect(funcName) ⇒ Object



247
248
249
250
251
252
253
254
255
256
# File 'lib/appswarm/application.rb', line 247

def self.protect(funcName)
  newName="_"+funcName.to_s
  self.send(:alias_method,newName,funcName)
  self.send(:define_method,funcName){|*args|
    @quitMutex.synchronize {
      raise "#{self} has quit!" if quit?
      self.send(newName,*args)
    }
  }
end

.provides(*x) ⇒ Object



241
242
243
244
245
# File 'lib/appswarm/application.rb', line 241

def self.provides(*x)
  @@providings||={}
  @@providings[self]||=[]
  @@providings[self]+=x
end

.providingObject



237
238
239
# File 'lib/appswarm/application.rb', line 237

def self.providing
  @@providings[self]||[]
end

.requirementsObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/appswarm/application.rb', line 222

def self.requirements
  @@requires||={}
  rs=ancestors.map{|a|@@requires[a]}.flatten.select{|x|x}

  #pp "RS",rs
  return rs
  a=ancestors[1]
  if a.ancestors.member?(Application)
    p=a.requirements
  else
    p=[]
  end
  
  ((@@requires[self]||[])+p).flatten
end

.requires(*x) ⇒ Object



217
218
219
220
221
# File 'lib/appswarm/application.rb', line 217

def self.requires(*x)
  @@requires||={}
  @@requires[self]||=[]
  @@requires[self]<<x
end

Instance Method Details

#appswarmBaseDirObject



101
102
103
# File 'lib/appswarm/application.rb', line 101

def appswarmBaseDir
  cluster.appswarmBaseDir
end

#configObject



64
65
66
# File 'lib/appswarm/application.rb', line 64

def config
  
end

#getApp(*s) ⇒ Object



175
176
177
178
# File 'lib/appswarm/application.rb', line 175

def getApp(*s)
  return cluster.getApp(*s) if cluster
  nil
end

#getAppDataPathObject



115
116
117
118
119
120
121
122
# File 'lib/appswarm/application.rb', line 115

def getAppDataPath
  dir=File.expand_path("../../data/#{self.class.to_s}",__FILE__)
  
  unless File.exist?(dir)
    return []
  end
  dir
end

#getAppDataPathsObject



111
112
113
# File 'lib/appswarm/application.rb', line 111

def getAppDataPaths
  [getAppDataWritePath,getAppDataPath].flatten
end

#getAppDataWritePathObject



105
106
107
108
109
# File 'lib/appswarm/application.rb', line 105

def getAppDataWritePath
  path=File.join(appswarmBaseDir,getAppName)
  Dir.mkdir_save(path)
  path
end

#getAppDirObject



146
147
148
# File 'lib/appswarm/application.rb', line 146

def getAppDir
  Application.getAppDir(self.class.to_s)
end

#getAppIdObject



93
94
95
# File 'lib/appswarm/application.rb', line 93

def getAppId
  @appId
end

#getAppNameObject

instanceName



89
90
91
# File 'lib/appswarm/application.rb', line 89

def getAppName
  @appName
end

#getService(name) ⇒ Object



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

def getService(name)
  @cluster.getApp(name)
end

#infoObject



97
98
99
# File 'lib/appswarm/application.rb', line 97

def info
  {}
end

#log(*s) ⇒ Object

logs any objects to the logger-app or if such does not exist to the console



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/appswarm/application.rb', line 160

def log(*s)
  logger=getApp(:logger)
  if logger
    #      puts "[#{Time.new}][#{self}][#{place}] "+s.map{|v|v.to_s}.join(" ")
    logger.log(self.class,getAppName,*s) if logger
  else
    place=caller[0]
    if cluster
      cluster.log_direct(place,nil,s.to_s)
    else
      pp s
    end
  end
end

#monitor(&b) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/appswarm/application.rb', line 180

def monitor(&b)
  log "start monitor"
  begin
    b.call
  rescue Object=>e
    log "Monitor error #{e}"
    log e.to_s+"\n"+e.backtrace.join("\n")
    #rescue  Error=>e
    #  log e.toString+"\n"+e.backtrace.join("\n")
  end
end

#provides?(xy) ⇒ Boolean

Returns:

  • (Boolean)


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

def provides?(xy)
  return true if self.class.providing.member?(xy)

  @provides[xy]
end

#providingAPIsObject



213
214
215
# File 'lib/appswarm/application.rb', line 213

def providingAPIs
  @provides.keys
end

#quitObject



40
41
42
43
44
# File 'lib/appswarm/application.rb', line 40

def quit
  @quitMutex.synchronize {
    @quit=true
  }
end

#quit?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/appswarm/application.rb', line 36

def quit?
  @quit
end

#retrieveConfigObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/appswarm/application.rb', line 192

def retrieveConfig
  app=nil
  trials=0
  loop do
    app=getApp(:localFileStore)
    if app.nil?
      cluster.run("LocalFileStore")
    else
      break
    end
    trials+=1
    log "First trial failed to get LocalFileStore"
    if trials>2
      puts "ERROR:  NO LocalFileStore found!"
      log "ERROR:  NO LocalFileStore found!"
      return nil
    end
  end 
  app.config(self)
end

#runObject



60
61
62
# File 'lib/appswarm/application.rb', line 60

def run
  
end

#shortNameObject

get a shorter name for the current application.



84
85
86
# File 'lib/appswarm/application.rb', line 84

def shortName
  self.class.to_s.gsub(/App$/,"")
end

#startupObject



56
57
58
# File 'lib/appswarm/application.rb', line 56

def startup
  
end

#stopObject



72
73
74
75
76
77
78
79
80
# File 'lib/appswarm/application.rb', line 72

def stop
  @threads.each{|thread|thread.kill}
  @threads.each{|thread|
    log "KILL THREAD #{thread}"
    thread.kill!

  }
  @threads=[]
end

#warning(*x) ⇒ Object



155
156
157
# File 'lib/appswarm/application.rb', line 155

def warning(*x)
  log "WARNING",*x
end

#withApp(name, &block) ⇒ Object



124
125
126
127
128
129
# File 'lib/appswarm/application.rb', line 124

def withApp(name,&block)
  app=@cluster.getApp(name)
  if app
    app.instance_eval{block.call}
  end
end

#withAppFork(name, &block) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/appswarm/application.rb', line 130

def withAppFork(name,&block)
  @threads << Thread.new(name){|name|
    loop do
      a=@cluster.getApp(name)
      if a
        puts name,a
        #exit
        a.instance_eval(&block)
        break
      end
      sleep 5
    end
  }
end