Class: Sixconfig
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Sixconfig
- Defined in:
- lib/six-updater-web/app/models/sixconfig.rb
Class Method Summary collapse
- .backtrace(e, log) ⇒ Object
- .exec ⇒ Object
- .process_msg(msg, messages, ses, previous_r = nil) ⇒ Object
- .start_updater(cl) ⇒ Object
-
.start_updater_inweb(cli) ⇒ Object
TODO: FIXME, Can’t run this in development atm!.
Instance Method Summary collapse
- #all_mods(srv = self.server) ⇒ Object
- #auto_skip_status(setting_override = false, server_override = nil, force = false) ⇒ Object
- #clear_skip_status(server_override = nil) ⇒ Object
- #exec ⇒ Object
- #execute(action, setting, server, autoskip, inweb) ⇒ Object
- #from_updater_yml(yml) ⇒ Object
- #output_yml(setting = nil, server = nil) ⇒ Object
- #to_updater_yml(setting_override = nil, server_override = nil) ⇒ Object
Methods inherited from ActiveRecord::Base
#associated_valid?, #no_errors_in_associated?, #save_associated, #save_associated!, #save_with_unsaved_flag, #to_label, #unsaved=, #unsaved?
Class Method Details
.backtrace(e, log) ⇒ Object
120 121 122 |
# File 'lib/six-updater-web/app/models/sixconfig.rb', line 120 def self.backtrace(e, log) log.info "ERROR!!! #{e.class}: #{e.} #{e.backtrace.join("\n")}" end |
.exec ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/six-updater-web/app/models/sixconfig.rb', line 124 def self.exec # Dirty workaround for Windows six-updater.rb inside BASE_PATH will take precedence over ruby\bin\six-updater(.bat) case RUBY_PLATFORM when /-mingw32$/, /-mswin32$/ "six-updater.bat" else "six-updater" end end |
.process_msg(msg, messages, ses, previous_r = nil) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/six-updater-web/app/models/sixconfig.rb', line 138 def self.process_msg(msg, , ses, previous_r = nil) # Create new logentry when no previous_r is found entry = previous_r ? previous_r : Log.new(:logsession_id => ses) entry.content = msg #p [msg, entry] # Set new previous_r unless last message had return previous_r = msg[/\n$/] ? nil : entry #&& ! msg[/.+\r\n$/] << entry unless .include?(entry) previous_r end |
.start_updater(cl) ⇒ Object
134 135 136 |
# File 'lib/six-updater-web/app/models/sixconfig.rb', line 134 def self.start_updater(cl) SixUpdaterWeb.run_program(self.exec, SixUpdaterWeb::BASE_PATH, cl) end |
.start_updater_inweb(cli) ⇒ Object
TODO: FIXME, Can’t run this in development atm!
155 156 157 158 159 160 161 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/six-updater-web/app/models/sixconfig.rb', line 155 def self.start_updater_inweb(cli) logger.info "Starting #{exec} with #{cli} from #{SixUpdaterWeb::BASE_PATH}" ActiveRecord::Base.connection_pool.clear_stale_cached_connections! Thread.new(cli, logger) do |cl, log| Dir.chdir SixUpdaterWeb::BASE_PATH do |dir| # Setup session ses = Logsession.new ses.save ses = ses.id # Workaround for some issue with throwing around this record, in development mode. , buff = [], [] shebang = nil found_r, previous_r = nil, nil status = nil cmd = case RUBY_PLATFORM when /-mingw32$/, /-mswin32$/ "ruby \"#{File.join(RAILS_ROOT, "lib", "wrapper.rb")}\" #{exec} #{cl}" else "#{exec} #{cl}" end begin status = IO.popen(cmd) do |io_out| #Open3.popen3("#{exec} #{cl}") do |io_in, io_out, io_err, waitth| previous = Time.now #io_in.close io_out.sync = true io_out.each_byte do |b| begin char = b.chr case RUBY_PLATFORM when /-mingw32$/, /-mswin32$/ if found_r # Found \r found_r = false if ["\n"].include?(char) # Line ending: Add to buffer, then process it buff << char msg = buff.join("") buff = [] if msg =~ /^SIX-SHEBANG: / shebang = msg else previous_r = process_msg(msg, , ses, previous_r) end else # Other char; Process buffer, then add it to a new buffer msg = buff.join("") buff = [] if msg =~ /^SIX-SHEBANG: / shebang = msg else previous_r = process_msg(msg, , ses, previous_r) end found_r = true if ["\r"].include?(char) buff << char end else # Default processing buff << char case char when "\r" found_r = true when "\n" msg = buff.join("") buff = [] if msg =~ /^SIX-SHEBANG: / shebang = msg else previous_r = process_msg(msg, , ses, previous_r) end end end else # Default processing buff << char if ["\r","\n"].include?(char) msg = buff.join("") buff = [] #if msg =~ /^SIX-SHEBANG: / # shebang = msg #else previous_r = process_msg(msg, , ses, previous_r) #end end end rescue => e backtrace(e, log) end unless .empty? # Write the messages buffer to database # only check every 2 seconds if Time.now - previous > 2 previous = Time.now done = [] .uniq! begin .each {|msg| msg.save; done << msg} rescue => e backtrace(e, log) end -= done end end end #status = waitth.value end rescue => e backtrace(e, log) end pid, status = nil, 1 case RUBY_PLATFORM when /-mingw32$/, /-mswin32$/ # Handle last bits in the buffer unless buff.empty? msg = buff.join("") if msg =~ /^SIX-SHEBANG: / shebang = msg else previous_r = process_msg(msg, , ses, previous_r) end end # Handle status and pid if shebang if shebang[/^SIX-SHEBANG: (-?.*), (-?.*)/] pid = $1.to_i if $1 status = $2.to_i if $2 end end else # Handle last bits in the buffer unless buff.empty? msg = buff.join("") previous_r = process_msg(msg, , ses, previous_r) end # Handle status and pid pid, status = $?.pid, $?.exitstatus end if status > 0 << Log.new(:logsession_id => ses, :content => "Abnormal program termination. #{pid}: #{status}") else << Log.new(:logsession_id => ses, :content => "Normal program termination. #{pid}: #{status}") end unless .empty? i = 0; done = false until done || i > 3 i += 1 begin .each {|msg| msg.save} done = true rescue => e backtrace(e, log) sleep 2 end end end end ActiveRecord::Base.connection_pool.clear_stale_cached_connections! end sleep 2 end |
Instance Method Details
#all_mods(srv = self.server) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/six-updater-web/app/models/sixconfig.rb', line 15 def all_mods(srv = self.server) m = [] m += srv.mods if srv self.mods.each { |mod| m << mod unless m.include?(mod) } m.each {|mod| if (mod.new_record? && !mod.exists?(self.appsetting)); mod.disabled = true; end } m end |
#auto_skip_status(setting_override = false, server_override = nil, force = false) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/six-updater-web/app/models/sixconfig.rb', line 60 def auto_skip_status(setting_override = false, server_override = nil, force = false) setting = if setting_override setting_override else self.appsetting end srv = if server_override server_override else self.server end setting = Appsetting.new unless setting self.all_mods(srv).each do |mod| if force mod.skip = false else mod.update_version(setting.real_modpath) mod.update_skip end mod.save end end |
#clear_skip_status(server_override = nil) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/six-updater-web/app/models/sixconfig.rb', line 53 def clear_skip_status(server_override = nil) self.all_mods(server_override).each do |mod| mod.skip = false mod.save end end |
#exec ⇒ Object
23 24 25 |
# File 'lib/six-updater-web/app/models/sixconfig.rb', line 23 def exec "" end |
#execute(action, setting, server, autoskip, inweb) ⇒ Object
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 |
# File 'lib/six-updater-web/app/models/sixconfig.rb', line 27 def execute(action, setting, server, autoskip, inweb) #self.auto_skip_status(setting, server, false) if autoskip h = self.output_yml(setting, server) cl = "" if action action.tasks.each do |task| cl += " --#{task}" unless task == "Default" end end if inweb Sixconfig.start_updater_inweb(cl) return h end case RUBY_PLATFORM when /-mingw32$/, /-mswin32$/ cl += " --wait" end Sixconfig.start_updater(cl) h end |
#from_updater_yml(yml) ⇒ Object
9 10 11 12 13 |
# File 'lib/six-updater-web/app/models/sixconfig.rb', line 9 def from_updater_yml(yml) yml.each_pair do |key, value| end end |
#output_yml(setting = nil, server = nil) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/six-updater-web/app/models/sixconfig.rb', line 85 def output_yml(setting = nil, server = nil) yml = self.to_updater_yml(setting, server) File.open(File.join(SixUpdaterWeb::DATA_PATH, 'six-updater.yml'), 'w') do |file| file.puts yml.to_yaml end yml end |
#to_updater_yml(setting_override = nil, server_override = nil) ⇒ Object
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 |
# File 'lib/six-updater-web/app/models/sixconfig.rb', line 93 def to_updater_yml(setting_override = nil, server_override = nil) setting = if setting_override setting_override else self.appsetting end srv = if server_override server_override else self.server end setting = Appsetting.new unless setting hash = setting.to_updater_yml hash[:folders] = [] self.all_mods(srv).each do |mod| y = mod.to_updater_yml hash[:folders] << y unless hash[:folders].include?(y) end hash[:server] = srv.to_updater_yml if srv hash end |