Class: Xxeo::FileQ

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

Constant Summary collapse

@@config_path =
nil
@@config =
nil
@@dir =
nil
@@fname_rgx =
/(\d+.\d\d\d):(\d+):(\d+)\.fq/

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ FileQ

Returns a new instance of FileQ.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fileq.rb', line 39

def initialize(name, options = {})
  options[:env]      ||= 'development'
  @err = '' 
  
  if not @dir
    if options[:dir]
      @dir = options[:dir]
    elsif 
      @@config_path = options[:config_path] || ('./config/fileq.yml')
      @@config = YAML.load_file(@@config_path)
      # USe 

      path = eval('"' + @@config[name][options[:env]]['pathname'] + '"')

      # TODO

      # If it is an expression, evaluate the env var

      @dir = path
    else
      return nil
    end
  end

  @lock = nil

end

Instance Method Details

#all_lengthsObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fileq.rb', line 115

def all_lengths
  a = %w(_tmp que run pause done _err)
  h = Hash.new(0)
  a.map { 
    |e|
    a = Dir.glob(@dir + "/#{e}/*")
    l = a.length
    h[e.to_sym] = l if l > 0
  }
  return h
end

#create_queue_dirsObject



324
325
326
327
328
329
330
331
332
# File 'lib/fileq.rb', line 324

def create_queue_dirs
  files_for_store.map {
    | e |
    path = @dir + '/' + e[0]
    next if File.exists? path
    FileUtils.mkdir(path) if e[1] == 'd'
    FileUtils.touch(path) if e[1] == 'w'
  }
end

#done_que_pathObject



312
313
314
# File 'lib/fileq.rb', line 312

def done_que_path
  return @dir + '/done/'
end

#error_que_pathObject



316
317
318
# File 'lib/fileq.rb', line 316

def error_que_path
  return @dir + '/_err/'
end

#files_for_storeObject



289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/fileq.rb', line 289

def files_for_store
  d = [
    ['', 'd'],
    ['_lock', 'w'],
    ['_log', 'w'],
    ['_tmp', 'd'],
    ['que', 'd'],
    ['run', 'd'],
    ['pause', 'd'],
    ['done', 'd'],
    ['_err', 'd'],
  ]
  return d
end

#find_job(job_name = nil) ⇒ Object



206
207
208
209
210
211
# File 'lib/fileq.rb', line 206

def find_job(job_name = nil)
  lock
  job,status = internal_find_job(job_name)
  unlock
  return job
end

#generate_nameObject

Names will be in format of YYYYMMDD.HHMM.SS



71
72
73
74
75
76
# File 'lib/fileq.rb', line 71

def generate_name
  z = Time.now.getutc
  name = z.strftime("%Y%m%d.%H%M.%S.") + sprintf("%03d", (z.tv_usec / 1000))
  return name
  # Process.pid kddkd

end

#insert_data(data, opts = {}) ⇒ Object



105
106
107
108
109
# File 'lib/fileq.rb', line 105

def insert_data(data, opts = {})
  opts[:XX_type] = 'data'
  opts[:XX_data] = data
  insert(opts)
end

#insert_file(fname, opts = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/fileq.rb', line 95

def insert_file(fname, opts = {})
  unless FileTest.writable?(fname)
    log("Supplied file: #{fname} is not writable. Failed ot insert. (see log)")
    return nil
  end
  opts[:XX_type] = 'file'
  opts[:XX_data] = fname
  insert(opts)
end

#internal_find_job(job_name = nil) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/fileq.rb', line 183

def internal_find_job(job_name = nil)
  result = [nil, nil]
  if job_name
    [['que', ST_QUEUED], ['run', ST_RUN], ['done', ST_DONE], ['pause', ST_PAUSED], ['_err', ST_ERROR]].map {
      | dir |
      path = @dir + '/' + dir[0] + '/' + job_name
      if FileTest.directory? path
        result = [Job.create(self, job_name, path, dir[1]), dir[1]]
        break
      end
    }
  else
    # Find the oldest job in the queue

    jobs = Dir.glob(que_path + '*')
    min = jobs.min
    if min
      name = File.basename(min)
      result = [Job.create(self, name, que_path + name, ST_QUEUED), ST_QUEUED]
    end
  end
  return result
end

#internal_job_exists?(q_name, job_name) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
# File 'lib/fileq.rb', line 127

def internal_job_exists?(q_name, job_name)
  throw ArgumentError unless ['que', 'run', 'done', 'pause', '_err'].include?(q_name)
  throw ArgumentError unless job_name
  result = nil
  path = @dir + '/' + q_name + '/' + job_name
  return FileTest.directory?(path)
end

#last_errorObject



64
65
66
# File 'lib/fileq.rb', line 64

def last_error
  return @err
end

#lengthObject



111
112
113
# File 'lib/fileq.rb', line 111

def length
  Dir.glob(@dir + '/que/*').length
end

#log(msg) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/fileq.rb', line 78

def log(msg)
  @err = msg
  File.open(@dir + '/_log', "a") do
    |f|
    f.write(Time.now.to_s + " == " + msg + "\n")
  end
end

#mark_job_done(job = nil) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/fileq.rb', line 233

def mark_job_done(job = nil)
  throw ArgumentError unless job
  throw ArgumentError unless job.own?
  lock
  if internal_job_exists?('run', job.name)
    # Move to run

    job.disown
    FileUtils.mv(@dir + '/run/' + job.name, @dir + '/done/' + job.name)
    job.set_status(@dir + '/done/' + job.name, ST_DONE)
  else
    log('attemped to mark invalid job as done: ' + job.name)
    job = nil
  end
  unlock
  return job
end

#mark_job_error(job = nil) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/fileq.rb', line 250

def mark_job_error(job = nil)
  throw ArgumentError unless job
  throw ArgumentError unless job.own?
  lock
  if internal_job_exists?('run', job.name)
    # Move to run

    job.disown
    FileUtils.mv(@dir + '/run/' + job.name, @dir + '/_err/' + job.name)
    job.set_status(@dir + '/_err/' + job.name, ST_ERROR)
  else
    log('attemped to mark invalid job as error: ' + job.name)
    job = nil
  end
  unlock
  return job
end

#meta_for_job(job_name) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fileq.rb', line 151

def meta_for_job(job_name)
  lock
  data = nil
  [['que', ST_QUEUED], ['run', ST_RUN], ['done', ST_DONE], ['pause', ST_PAUSED], ['_err', ST_ERROR]].map {
    | dir |
    path = @dir + '/' + dir[0] + '/' + job_name
    if FileTest.directory? path
      data = YAML.load_file(path + '/meta.yml')
      break
    end
  }
  unlock
  return data
end

#pause_que_pathObject



320
321
322
# File 'lib/fileq.rb', line 320

def pause_que_path
  return @dir + '/pause/'
end

#pull_job(job_name = nil) ⇒ Object

Currently we can only move items from the que to the run queu TODO: allow paused or error jobs to be reset, (maybe even done jobs) definitely log history in those jobs



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/fileq.rb', line 216

def pull_job(job_name = nil)
  lock
  job,status = internal_find_job(job_name)
  if job && status == ST_QUEUED
    # Move to run, notice use of job.name rather than job_name

    # .. if we are pulling a new job, it could be nil

    FileUtils.mv(@dir + '/que/' + job.name, @dir + '/run/' + job.name)
    job.set_as_active
  elsif job
    # We cannot pull a job that isn't queued

    log("cannot pull job that isn't queued: " + job_name)
    job = nil
  end
  unlock
  return job
end

#que_pathObject



304
305
306
# File 'lib/fileq.rb', line 304

def que_path
  return @dir + '/que/'
end

#read_logObject



86
87
88
89
90
91
92
93
# File 'lib/fileq.rb', line 86

def read_log
  data = ''
  File.open(@dir + '/_log', "r") do
    |f|
    data = f.read
  end
  return data
end

#reinsert_job(job = nil) ⇒ Object

The assumption here is that this is for testing or that this process couldn’t handle the job and it has been changed so another script could handle the job. Otherwise, an infinite loop is created with this method



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/fileq.rb', line 271

def reinsert_job(job = nil)
  throw ArgumentError unless job
  throw ArgumentError unless job.own?
  lock
  # TODO: handle other job types later

  if internal_job_exists?('run', job.name)
    # Move to que

    job.disown
    FileUtils.mv(@dir + '/run/' + job.name, @dir + '/que/' + job.name)
    job.set_status(@dir + '/que/' + job.name, ST_QUEUED)
  else
    log('attemped to reinsert job that could not be found: ' + job.name)
    job = nil
  end
  unlock
  return job
end

#run_que_pathObject



308
309
310
# File 'lib/fileq.rb', line 308

def run_que_path
  return @dir + '/run/'
end

#status(job_name = nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fileq.rb', line 135

def status(job_name = nil)
  throw ArgumentError unless job_name
  lock
  result = ST_UNKNOWN
  [['que', ST_QUEUED], ['run', ST_RUN], ['done', ST_DONE], ['pause', ST_PAUSED], ['_err', ST_ERROR]].map {
    | dir |
    path = @dir + '/' + dir[0] + '/' + job_name
    if FileTest.directory? path
      result = dir[1]
      break
    end
  }
  unlock
  return result
end

#status_mesg_for_job(job_name) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/fileq.rb', line 166

def status_mesg_for_job(job_name)
  lock
  data = ''
  [['que', ST_QUEUED], ['run', ST_RUN], ['done', ST_DONE], ['pause', ST_PAUSED], ['_err', ST_ERROR]].map {
    | dir |
    path = @dir + '/' + dir[0] + '/' + job_name
    if FileTest.directory? path
      if FileTest.readable? path + '/status'
        File.open(path + '/status') { |f| data = f.read }
      end
      break
    end
  }
  unlock
  return data
end

#verify_storeObject



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/fileq.rb', line 334

def verify_store

  files_for_store.map {
    | e |
    if e[1] == 'w'
      unless FileTest.exists?( @dir + '/' + e[0])
        log "bad queue dir: file '#{e[0]}' does not exist"
        return false
      end
      unless FileTest.writable?( @dir + '/' + e[0])
        log "bad queue dir: file '#{e[0]}' not writable"
        return false
      end
    elsif e[1] == 'd'
      unless FileTest.directory?( @dir + '/' + e[0])
        log "bad queue dir: '#{e[0]}' not a directory"
        return false
      end
    else
      log "Bad code in verify store"
      return false
    end
  }

  @err = ''
  return true

end