Module: Scout

Defined in:
lib/scout.rb,
lib/scout/monitor.rb

Constant Summary collapse

SENSIBLE_WRITE_DIRS =
Open.sensible_write_dir.find_all
LOCK_DIRS =
Path.setup('tmp/tsv_open_locks').find_all +
Path.setup('tmp/tsv_locks').find_all +
Path.setup('tmp/persist_locks').find_all +
Path.setup('tmp/sensible_write_locks').find_all +
Path.setup('tmp/produce_locks').find_all +
Path.setup('tmp/step_info_locks').find_all
PERSIST_DIRS =
Path.setup('share').find_all  + Path.setup('var/cache/persistence').find_all
JOB_DIRS =
Path.setup('var/jobs').find_all
MUTEX_FOR_THREAD_EXCLUSIVE =
Mutex.new

Class Method Summary collapse

Class Method Details

.__jobs(dirs = JOB_DIRS) ⇒ Object

REST



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
# File 'lib/scout/monitor.rb', line 239

def self.__jobs(dirs = JOB_DIRS)
  job_files = {}
  dirs.each do |dir|
    workflow_dirs = dir.glob("*").each do |wdir|
      workflow = File.basename(wdir)
      job_files[workflow] = {}
      task_dirs = wdir.glob('*')
      task_dirs.each do |tdir|
        task = File.basename(tdir)
        job_files[workflow][task] = tdir.glob('*')
      end
    end
  end
  jobs = {}
  job_files.each do |workflow,task_jobs|
    jobs[workflow] ||= {}
    task_jobs.each do |task, files|
      jobs[workflow][task] ||= {}
      files.each do |f|
        next if f =~ /\.lock$/
        job = f.sub(/\.(info|files)/,'')

        jobs[workflow][task][job] ||= {}
        if jobs[workflow][task][job][:status].nil?
          status = nil
          status = :done if Open.exists? job
          if status.nil? and f=~/\.info/
            info = begin
                     Step::INFO_SERIALIZER.load(Open.read(f, :mode => 'rb'))
                   rescue
                     {}
                   end
            status = info[:status]
            pid = info[:pid]
          end

          jobs[workflow][task][job][:pid] = pid if pid
          jobs[workflow][task][job][:status] = status if status
        end
      end
    end
  end
  jobs
end

.dump_memory(file, obj = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/scout/monitor.rb', line 20

def self.dump_memory(file, obj = nil)
  Log.info "Dumping #{obj} objects into #{ file }"
  Thread.new do
    while true
      Open.write(file) do |f|
        MUTEX_FOR_THREAD_EXCLUSIVE.synchronize do
          GC.start
          ObjectSpace.each_object(obj) do |o|
            f.puts "---"
            f.puts(String === o ? o : o.inspect)
          end
        end
      end
      FileUtils.cp file, file + '.save'
      sleep 3
    end
  end
end

.file_time(file) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/scout/monitor.rb', line 39

def self.file_time(file)
  info = {}
  begin
    info[:ctime] = File.ctime file
    info[:atime] = File.atime file
    info[:elapsed] = Time.now - info[:ctime]
  rescue Exception
  end
  info[:ctime] = Time.now - 999
  info
end

.job_info(workflows = nil, tasks = nil, dirs = JOB_DIRS) ⇒ Object

PERSISTS



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
# File 'lib/scout/monitor.rb', line 127

def self.job_info(workflows = nil, tasks = nil, dirs = JOB_DIRS)
  require 'rbbt/workflow/step'

  workflows = [workflows] if workflows and not Array === workflows
  workflows = workflows.collect{|w| w.to_s} if workflows

  tasks = [tasks] if tasks and not Array === tasks
  tasks = tasks.collect{|w| w.to_s} if tasks

  jobs = {}
  seen = Set.new
  _files = Set.new
  dirs.collect do |dir|
    next unless Open.exists? dir

    task_dir_workflows = {}
    tasks_dirs = if dir == '.'
                  ["."]
                 else
                   #workflowdirs = if (dir_sub_path = Open.find_repo_dir(workflowdir))
                   #                 repo_dir, sub_path = dir_sub_path
                   #                 Open.list_repo_files(*dir_sub_path).collect{|f| f.split("/").first}.uniq.collect{|f| File.join(repo_dir, f)}.uniq
                   #               else
                   #                 dir.glob("*")
                   #               end

                   workflowdirs = dir.glob("*")

                   workflowdirs.collect do |workflowdir|
                     workflow = File.basename(workflowdir)
                     next if workflows and not workflows.include? workflow

                     #task_dirs = if (dir_sub_path = Open.find_repo_dir(workflowdir))
                     #              repo_dir, sub_path = dir_sub_path
                     #              Open.list_repo_files(*dir_sub_path).collect{|f| f.split("/").first}.uniq.collect{|f| File.join(repo_dir, f)}.uniq
                     #            else
                     #              workflowdir.glob("*")
                     #            end

                     task_dirs = workflowdir.glob("*")

                     task_dirs.each do |tasks_dir|
                       task_dir_workflows[tasks_dir] = workflow
                     end
                   end.compact.flatten
                 end

    tasks_dirs.collect do |taskdir|
      task = File.basename(taskdir)
      next if tasks and not tasks.include? task


      #files = if (dir_sub_path = Open.find_repo_dir(taskdir))
      #          repo_dir, sub_path = dir_sub_path
      #          Open.list_repo_files(*dir_sub_path).reject do |f|
      #            f.include?("/.info/") ||
      #              f.include?(".files/") ||
      #              f.include?(".pid/") ||
      #              File.directory?(f)
      #          end.collect do |f|
      #            File.join(repo_dir, f)
      #          end
      #        else
      #          #cmd = "find -L '#{ taskdir }/'  -not \\( -path \"#{taskdir}/*.files/*\" -prune \\) -not -name '*.pid' -not -name '*.notify' -not -name '\\.*' 2>/dev/null"
      #          cmd = "find -L '#{ taskdir }/' -not \\( -path \"#{taskdir}/.info/*\" -prune \\) -not \\( -path \"#{taskdir}/*.files/*\" -prune \\) -not -name '*.pid' -not -name '*.md5' -not -name '*.notify' -not -name '\\.*' \\( -not -type d -o -name '*.files' \\)  2>/dev/null"

      #          CMD.cmd(cmd, :pipe => true).read.split("\n")
      #        end

      files = begin
                cmd = "find -L '#{ taskdir }/' -not \\( -path \"#{taskdir}/.info/*\" -prune \\) -not \\( -path \"#{taskdir}/*.files/*\" -prune \\) -not -name '*.pid' -not -name '*.md5' -not -name '*.notify' -not -name '\\.*' \\( -not -type d -o -name '*.files' \\)  2>/dev/null"

                CMD.cmd(cmd, :pipe => true).read.split("\n")
              end

      files = files.sort_by{|f| Open.mtime(f) || Time.now}
      workflow = task_dir_workflows[taskdir]
      TSV.traverse files, :type => :array, :into => jobs, :_bar => "Finding jobs in #{ taskdir }" do |file|
        _files << file
        if m = file.match(/(.*)\.(info|pid|files)$/)
          file = m[1]
        end
        next if seen.include? file
        seen << file

        name = file[taskdir.length+1..-1]
        info_file = file + '.info'

        info = {}

        info[:workflow] = workflow
        info[:task] = task
        info[:name] = name

        if Open.exists? file
          info = info.merge(file_time(file))
          info[:done] = true
          info[:info_file] = Open.exist?(info_file) ? info_file : nil
        else
          info = info.merge({:info_file => info_file, :done => false})
        end

        [file, info]
      end

    end.compact.flatten
  end.compact.flatten
  jobs
end

.load_lock(lock) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/scout/monitor.rb', line 284

def self.load_lock(lock)
  begin
    info = Misc.insist 3 do
      Open.yaml(lock)
    end
    info.values_at "pid", "ppid", "time"
  rescue Exception
    time = begin
             File.atime(lock)
           rescue Exception
             Time.now
           end
    [nil, nil, time]
  end
end

.lock_info(dirs = LOCK_DIRS) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/scout/monitor.rb', line 60

def self.lock_info(dirs = LOCK_DIRS)
  lock_info = {}
  locks(dirs).each do |f|
    lock_info[f] = {}
    begin
      lock_info[f].merge!(file_time(f))
      if File.size(f) > 0
        info = Open.open(f) do |s|
          Open.yaml(s)
        end
        IndiferentHash.setup(info)
        lock_info[f][:pid] = info[:pid]
        lock_info[f][:ppid] = info[:ppid]
      end
    rescue Exception
      Log.warn $!.message
    end
  end
  lock_info
end

.locks(dirs = LOCK_DIRS) ⇒ Object

{{{ LOCKS



53
54
55
56
57
58
# File 'lib/scout/monitor.rb', line 53

def self.locks(dirs = LOCK_DIRS)
  dirs.collect do |dir|
    next unless Open.exists? dir
    `find -L "#{ dir }" -name "*.lock" 2>/dev/null`.split "\n"
  end.compact.flatten
end

.persist_info(dirs = PERSIST_DIRS) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/scout/monitor.rb', line 112

def self.persist_info(dirs = PERSIST_DIRS)
  info = {}
  persists(dirs).each do |f|
    begin
      i = file_time(f)
      info[f] = i
    rescue
      Log.exception $!
    end
  end
  info
end

.persists(dirs = PERSIST_DIRS) ⇒ Object

PERSISTS



105
106
107
108
109
110
# File 'lib/scout/monitor.rb', line 105

def self.persists(dirs = PERSIST_DIRS)
  dirs.collect do |dir|
    next unless Open.exists? dir
    `find -L "#{ dir }" -name "*.persist" 2>/dev/null`.split "\n"
  end.compact.flatten
end

.sensiblewrite_info(dirs = SENSIBLE_WRITE_DIRS) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/scout/monitor.rb', line 90

def self.sensiblewrite_info(dirs = SENSIBLE_WRITE_DIRS)
  info = {}
  sensiblewrites(dirs).each do |f|
    begin
      i = file_time(f)
      info[f] = i
    rescue
      Log.exception $!
    end
  end
  info
end

.sensiblewrites(dirs = SENSIBLE_WRITE_DIRS) ⇒ Object

{{{ SENSIBLE WRITES



83
84
85
86
87
88
# File 'lib/scout/monitor.rb', line 83

def self.sensiblewrites(dirs = SENSIBLE_WRITE_DIRS)
  dirs.collect do |dir|
    next unless Open.exists? dir
    `find -L "#{ dir }" -not -name "*.lock" -not -type d 2>/dev/null`.split "\n"
  end.compact.flatten
end

.versionObject



4
5
6
# File 'lib/scout.rb', line 4

def self.version
  Open.read(File.join(__dir__, '../VERSION'))
end