Class: Wakame::Initializer

Inherits:
Object
  • Object
show all
Defined in:
lib/wakame/initializer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Initializer

Returns a new instance of Initializer.



24
25
26
# File 'lib/wakame/initializer.rb', line 24

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



22
23
24
# File 'lib/wakame/initializer.rb', line 22

def configuration
  @configuration
end

Class Method Details

.instanceObject



13
14
15
# File 'lib/wakame/initializer.rb', line 13

def instance
  @instance
end

.loaded_classesObject



17
18
19
# File 'lib/wakame/initializer.rb', line 17

def loaded_classes
  @loaded_classes ||= []
end

.run(command, configuration = Configuration.new) ⇒ Object



8
9
10
11
# File 'lib/wakame/initializer.rb', line 8

def run(command, configuration=Configuration.new)
  @instance ||= new(configuration)
  @instance.send(command)
end

Instance Method Details

#load_actorsObject



130
131
132
133
134
135
136
137
138
# File 'lib/wakame/initializer.rb', line 130

def load_actors
  load_framework('wakame/actor/**/*.rb', lambda{ |f|
                   Wakame.log.debug("Loading Core Actor: #{f}")
                 })

  load_project('cluster/actors/*.rb', lambda{ |f|
                 Wakame.log.debug("Loading Project Actor: #{f}")
               })
end

#load_core_actionsObject



156
157
158
159
160
# File 'lib/wakame/initializer.rb', line 156

def load_core_actions
  load_framework("wakame/actions/**/*.rb", lambda{ |f|
                   Wakame.log.debug("Loading Core Actions: #{f}")
                 })
end

#load_core_commandsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/wakame/initializer.rb', line 85

def load_core_commands
#       %w( cluster/commands ).each { |load_path|
#         load_path = File.expand_path(load_path, configuration.root_path)
#         matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
#         Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
#           require file.sub(matcher, '\1')
#         end
#       }

  load_framework('wakame/command/**/*.rb', lambda{ |f|
                   Wakame.log.debug("Loading Core Commands: #{f}")
                 })
end

#load_core_triggersObject



150
151
152
153
154
# File 'lib/wakame/initializer.rb', line 150

def load_core_triggers
  load_framework("wakame/triggers/**/*.rb", lambda{ |f|
                   Wakame.log.debug("Loading Core triggers: #{f}")
                 })
end

#load_environmentObject



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/wakame/initializer.rb', line 100

def load_environment
  config = configuration
  constants = self.class.constants

  [:common, config.environment].each { |key|
    eval(IO.read(config.environment_path(key)), binding, config.environment_path(key))
  }

  (self.class.constants - constants).each do |const|
    Object.const_set(const, self.class.const_get(const))
  end
end

#load_framework(glob_pat, pre_hook = nil, post_hook = nil) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/wakame/initializer.rb', line 209

def load_framework(glob_pat, pre_hook=nil, post_hook=nil)
  if Wakame::Bootstrap.boot_type == Wakame::Bootstrap::VendorBoot
    rbfiles = Dir.glob(File.expand_path(glob_pat, File.join(configuration.framework_root_path, 'lib')))  
  else
    rbfiles = Gem.find_files(glob_pat)
  end
  rbfiles.sort.each{ |f|
    pre_hook.call(f) if pre_hook
    load f
    post_hook.call(f) if post_hook
  }
end

#load_monitorsObject



140
141
142
143
144
145
146
147
148
# File 'lib/wakame/initializer.rb', line 140

def load_monitors
  load_framework('wakame/monitor/**/*.rb', lambda{ |f|
                   Wakame.log.debug("Loading Core Monitor: #{f}")
                 })

  #load_project('cluster/monitors/*.rb', lambda{ |f|
  #               Wakame.log.debug("Loading Project Monitor: #{f}")
  #             })
end

#load_project(glob_pat, pre_hook = nil, post_hook = nil) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/wakame/initializer.rb', line 222

def load_project(glob_pat, pre_hook=nil, post_hook=nil)
  Dir.glob(File.expand_path(glob_pat, configuration.root_path)).sort.each{ |f|
    (pre_hook.call(f) || next) if pre_hook
    load f
    post_hook.call(f) if post_hook
  }
end

#load_resourcesObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/wakame/initializer.rb', line 113

def load_resources
  load_path = File.expand_path('cluster/resources/markers', configuration.root_path)
  Dir.glob("#{load_path}/*.rb").sort.each do |file|
    Wakame.log.debug("Loading resource marker: #{file}")
    load file
  end
  
  load_path = File.expand_path('cluster/resources', configuration.root_path)
  Dir.glob("#{load_path}/*/*.rb").sort.each do |file|
    if file =~ %r{\A#{Regexp.escape(load_path)}/([^/]+)/([^/]+)\.rb\Z} && $1 == $2
      Wakame.log.debug("Loading resource definition: #{file}")
      load file
    end
    #require file.sub(matcher, '\1')
  end
end

#load_system_monitorsObject



82
83
# File 'lib/wakame/initializer.rb', line 82

def load_system_monitors
end

#processObject



28
29
30
31
32
# File 'lib/wakame/initializer.rb', line 28

def process
  setup_load_paths
  setup_logger
  load_environment
end

#process_agentObject



43
44
45
46
47
# File 'lib/wakame/initializer.rb', line 43

def process_agent
  require 'wakame/agent'
  load_actors
  load_monitors
end

#process_cliObject



49
50
51
# File 'lib/wakame/initializer.rb', line 49

def process_cli
  process
end

#process_masterObject



34
35
36
37
38
39
40
41
# File 'lib/wakame/initializer.rb', line 34

def process_master
  require 'wakame/master'
  setup_database
  load_resources
  load_core_commands
  load_core_actions
  load_core_triggers
end

#setup_databaseObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/wakame/initializer.rb', line 162

def setup_database
  require 'sequel'
  
  #db = Sequel.connect(Wakame.config.status_db_dsn, {:logger=>Wakame.log})
  #db = Sequel.connect(Wakame.config.status_db_dsn, {:timeout=>15000})
  db = Sequel.connect(Wakame.config.status_db_dsn)
  if db.uri  =~ /\Asqlite:/
    orig_proc = db.pool.connection_proc
    db.pool.connection_proc = proc { |svr|
      con = orig_proc.call(svr)
      con.busy_handler {|data, retries|
        if retries > 5
          Wakame.log.fatal("Detected the SQLite's busy lock: #{Thread.current}, data=#{data}, retries=#{retries}")
          exit 100
        end
        sleep 8
        true
      }
      con
    }
  end

  load_framework('wakame/models/*.rb', 
                 lambda {|path| self.class.loaded_classes.clear},
                 lambda {|path|
                           
                   self.class.loaded_classes.each { |model_class|
                     next unless model_class.is_a?(Class) && model_class < Sequel::Model
                     
                     model_class.create_table?
                   }

                 })


  if db.table_exists?(:metadata)
  else
    db.create_table? :metadata do
      primary_key :id
      column :version, :string
      column :created_at, :datetime
    end
    db[:metadata].insert(:version=>'0.4', :created_at=>Time.now)
  end

end

#setup_load_pathsObject



53
54
55
56
57
58
59
# File 'lib/wakame/initializer.rb', line 53

def setup_load_paths
  load_paths = configuration.load_paths + configuration.project_paths + configuration.framework_paths
  load_paths.reverse_each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) }
  $LOAD_PATH.uniq!

  require 'wakame'
end

#setup_loggerObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/wakame/initializer.rb', line 61

def setup_logger
  require 'log4r'
  Logger.log = begin
                 #log = Logger.new((Wakame.root||Dir.pwd) / "log.log")
                 out = ::Log4r::StdoutOutputter.new('stdout',
                                                    :formatter => Log4r::PatternFormatter.new(
                                                                                              :depth => 9999, # stack trace depth
                                                                                              :pattern => "%d %C [%l]: %M",
                                                                                              :date_format => "%Y/%m/%d %H:%M:%S"
                                                                                              )
                                                    )
                 log = ::Log4r::Logger.new(File.basename($0.to_s))
                 log.add(out)
                 log
               end
end

#setup_system_actorsObject



79
80
# File 'lib/wakame/initializer.rb', line 79

def setup_system_actors
end