# encoding: utf-8 =begin TODO: All process initialization and management through this plugin? TODO: Server/Game/Generic app process manager Find open processes of all registered apps able to kill them or restart them, so record the various details somewhere..able to start them. Rpt/log tailer... also handy for updater log etc ;-) Will require user authorization at some point :P (Or no such thing, but ssh-recommended?) =end case RUBY_PLATFORM when /-mingw32$/, /-mswin32$/ require 'win32/process' # required already in six-updater-web.rb #require 'win32/registry' #require 'win32ole' end module Six module Bla # Prepared class << self attr_accessor :process def process begin @process rescue nil end end end end module Appmanager FOLDER = /(.*)\/(.*)/ module_function def logger ActionController::Base.logger end def wmi_init if @wmi.nil? WIN32OLE.codepage = WIN32OLE::CP_UTF8 #swbemloc = WIN32OLE.new("WbemScripting.SWbemLocator") #@wmi = swbemloc.ConnectServer#(@address.to_s, namespace, @user, @password) @wmi = WIN32OLE.connect('winmgmts://') end end def kill(process) Process.kill(9, process).each do |kill| #.ProcessId logger.debug "Killed: #{kill}" # if kill == process.ProcessId # logger.debug "Succesfully killed!" # else # logger.debug "Not succesfully killed, forcing!" # sleep 3 # Process.kill(9, process.ProcessId) # end end end # TODO: Kill by ... multiple properties. gamefolder, etc. def kill_by_name(name) find_process(name).each { |process| kill(process) } end def find_process(name) pids = [] begin case RUBY_PLATFORM when /-mingw32$/, /-mswin32$/ out = %x[tasklist] out.split("\n").each do |line| if line =~ /\A#{name}[\t| ]*([0-9]*)/ pid = $1 if pid.size > 0 pid = pid.to_i pids << pid end end end else %x[ps -A | grep #{name}].split("\n").each do |line| unless line[/grep/] line[/^\s*([0-9]*)/] pids << $1 end end end rescue => e logger.warn "Failed checking tasklist for #{name}" logger.debug "#{e.class}: #{e.message} #{e.backtrace.join("\n")}" end pids =begin wmi_init ar = [] processes = @wmi.ExecQuery("select * from win32_process") processes.each do |process| if process.Name == name logger.debug "Found m! #{process.ProcessId}" ar << process end end ar =end end def tail(file, interval=1) raise "Illegal interval #{interval}" if interval < 0 # Read to a table and display with ajax??? # RE: http://onlamp.com/pub/a/onlamp/2005/06/09/rails_ajax.html File.open(file) do |io| loop do while ( line = io.gets ) puts line end # uncomment next to watch what is happening # puts "-" sleep interval end end end def read_logfile(file) begin File.open('C:/Users/SB/Test.txt') do |file| file.read end rescue 'Error while reading logfile!' end end module Appsetting # TODO: More generic? Instead of arma specific? def path_by_registry SixUpdaterWeb::ARMA_PATH end def real_exe if exe.nil? "arma2.exe" else exe end end def real_params if params.nil? "-noSplash -noFilePatching -showScriptErrors" else params end end def real_logpath if logpath.nil? case RUBY_PLATFORM when /-mingw32$/, /-mswin32$/ "#{File.join(ENV['USERPROFILE'], 'appdata', 'local', 'arma 2')}" else nil end else logpath end end def real_path if path.nil? path_by_registry else self.path end end def real_modpath if self.modpath.nil? self.real_path else self.modpath end end def process_name if exe if exe[FOLDER] $2 else exe end else 'arma2.exe' end end def processes Six::Appmanager.find_process(process_name) end def read_logfile Six::Appmanager.read_logfile logfile unless logfile.nil? end def kill! Six::Appmanager.kill_by_name(process_name) end def rpt process_name[/(.*)\./] "#{$1}.rpt" end def logfile if FileTest.directory? real_logpath File.join(real_logpath, rpt) if FileTest.exist?(File.join(real_logpath, rpt)) else nil end end end end end =begin processes = @wmi.ExecQuery("select * from win32_process") for process in @processes do for property in process.Properties_ do logger.debug property.Name end break end for process in @processes do logger.debug "Name: #{process.Name}" logger.debug "CommandLine: #{process.CommandLine}" logger.debug "CreationDate: #{process.CreationDate}" logger.debug "WorkingSetSize: #{process.WorkingSetSize}" logger.debug end =end