Module: Six::Appmanager

Defined in:
lib/six-updater-web/vendor/plugins/six-app_manager/lib/six/appmanager.rb

Class Method Summary collapse

Class Method Details

.find_process(name, path) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/six-updater-web/vendor/plugins/six-app_manager/lib/six/appmanager.rb', line 58

def find_process(name, path)
  pids = []
  logger.info "Checking for #{name} - #{path}"

  begin
    case RUBY_PLATFORM
      when /-mingw32$/, /-mswin32$/
        %x[WMIC PROCESS get Caption,ExecutablePath,ProcessId].each_line do |l| #Commandline
            line = l.clone
            line.chomp!
            line.strip!
            reg = /([0-9]*)$/
            line[reg]
            pid = $1
            next if pid.nil?
            line.sub!(reg, "")
            line.strip!
            line[/^([\w ]*\.\w*)[ ]*(.*)/]
            image, full = $1, $2
            next if image.nil?

            case image.downcase
              when name.downcase
                pid = pid.to_i
                next unless pid > 0
                if path.nil? || full.nil?
                  pids << pid
                  next
                end
                logger.debug "Full: #{full.downcase}"
                if File.join(path, name).gsub("/", "\\").downcase == full.downcase
                  pids << pid
                end
            end
        end
=begin
        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
=end
      else
        # TODO: Include exe/cmdnline check;
        # /proc/PID/exe
        # /proc/PID/cmdline
        %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

.kill(process) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/six-updater-web/vendor/plugins/six-app_manager/lib/six/appmanager.rb', line 36

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

.kill_by_name(name, path = nil) ⇒ Object

TODO: Kill by … multiple properties. gamefolder, etc.



51
52
53
54
55
56
# File 'lib/six-updater-web/vendor/plugins/six-app_manager/lib/six/appmanager.rb', line 51

def kill_by_name(name, path = nil)
  procs = find_process(name, path)
  logger.debug "Procs: #{procs}"
  procs.each { |process| kill(process) }
  procs
end

.loggerObject



23
24
25
# File 'lib/six-updater-web/vendor/plugins/six-app_manager/lib/six/appmanager.rb', line 23

def logger
  ActionController::Base.logger
end

.read_logfile(file) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/six-updater-web/vendor/plugins/six-app_manager/lib/six/appmanager.rb', line 153

def read_logfile(file)
  begin
    File.open('C:/Users/SB/Test.txt') do |file|
      file.read
    end
  rescue
    'Error while reading logfile!'
  end
end

.tail(file, interval = 1) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/six-updater-web/vendor/plugins/six-app_manager/lib/six/appmanager.rb', line 135

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

.wmi_initObject



27
28
29
30
31
32
33
34
# File 'lib/six-updater-web/vendor/plugins/six-app_manager/lib/six/appmanager.rb', line 27

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