Class: Appswarm

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

Constant Summary collapse

VERSION =
'0.0.1'
BASIC_APPS =
[
#    "LocalFileStore",
#    "LocalWebServer",
  "Admin",
#    "Permaspace",
#    "Home"
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClusterFuncs

#allUpAndRunning, #checkRequirements, #createInstanceId, #debug, #getApplicationTypes, #getApps, #getLocalApplications, #getNewInstanceName, #getOrCreate, #log_direct, #log_line, #run, #runAndWait, #waitUntilAllUpAndRunning

Constructor Details

#initialize(options = {}) ⇒ Appswarm

Returns a new instance of Appswarm.



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

def initialize(options={})
  @apps=[]
  @allApps=loadApps(self)

  if options[:trace]
    enableTracing
  end

  @options=options
  @appid=0
  @running=true
  @stopping=false

  @stopMutex=Mutex.new
  trap("INT"){ Tracer::enable ; stop }
end

Instance Attribute Details

#appsObject (readonly)

Returns the value of attribute apps.



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

def apps
  @apps
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#runningObject (readonly)

Returns the value of attribute running.



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

def running
  @running
end

Instance Method Details

#appswarmBaseDirObject



122
123
124
# File 'lib/appswarm.rb', line 122

def appswarmBaseDir
  File.join(ENV["HOME"],".appswarm")
end

#cloneApp(app) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/appswarm.rb', line 59

def cloneApp(app)
  if @apps.member?(app)
    Thread.new {
      log "Starting new app from #{app}"
      options={:appName=>app.class.to_s+@appid.to_s}
      inst=app.class.new(self,options)
      @appid+=1
      #inst.setAppName(app.getAppName.gsub(/[0-9]*$/,"")[email protected]_s)
      #raise 1
      getApp(:logger).log(self,inst.getAppName)
      @apps << inst
      inst.run
    }
  end
end

#getApp(type) ⇒ Object

@apps.p_each{|app|app.stop if app.respond_to?(:stop)}



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

def getApp(type)
  getApps(type)[0]
end

#getAppByName(name) ⇒ Object



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

def getAppByName(name)
  @apps.select{|app|app.getAppName==name}[0]
end

#getGlobalServicesObject



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

def getGlobalServices
  @apps.select{|a|a.is_a?(GlobalService)}.map{|a|a.serviceID}
end

#log(*s) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/appswarm.rb', line 101

def log(*s)
  pp s
  loggers=getApps(:logger)
  if loggers.length>0
    loggers.each{|app|app.log(self,"",*s)}
  else
    a=s.inspect.to_s
    log_direct(caller[0],nil,a)
  end
end

#shutdownObject



117
118
119
120
# File 'lib/appswarm.rb', line 117

def shutdown
  @running=false
  stop
end

#startObject



42
43
44
45
46
47
48
49
50
# File 'lib/appswarm.rb', line 42

def start
  @appClasses=@allApps

  BASIC_APPS.each{|appClass|
    log "STart #{appClass}"
    run(appClass)
  }
  log "All Apps started"
end

#stopObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/appswarm.rb', line 75

def stop
  puts "STOPPP"
  puts @apps.length
  [:quit,:stop,:stop!].each{|f|
    puts "Calling #{f}"
    @apps.p_each{|app|

      if app.respond_to?(f)
        app.send(f)
      else
        puts "#{app} does not support #{f}"
      end

      }
    sleep 0.2
  }
  #@apps.p_each{|app|app.stop if app.respond_to?(:stop)}
end

#stopApp(app) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/appswarm.rb', line 52

def stopApp(app)
  log "Stopping #{app}"
  if @apps.member?(app)
    app.stop
    @apps.delete(app)
  end
end