Class: SimpleWS::Jobs::Scheduler::Job

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

Overview

{{{ Job

Constant Summary collapse

@@config =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure(name, value) ⇒ Object



218
219
220
# File 'lib/simplews/jobs.rb', line 218

def self.configure(name, value)
  @@config[name] = value
end

.job_info(name) ⇒ Object

Raises:



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/simplews/jobs.rb', line 189

def self.job_info(name)
  info = nil

  retries = 2
  begin
    info = Marshal::load(File.open(File.join(@@savedir,name + '.marshal')))
    raise Exception unless info.is_a?(Hash) && info[:info]
  rescue Exception
    if retries > 0
      retries -= 1
      sleep 1
      retry
    end
    info = nil
  end

  raise JobNotFound, "Job with name '#{ name }' was not found" if info.nil?

  info
end

.path(file, name) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/simplews/jobs.rb', line 175

def self.path(file, name)
  if file =~ /^\/|#{@@workdir}/
    file.gsub(/\{JOB\}/, name)
  else
    File.join(@@workdir, file.gsub(/\{JOB\}/,name))
  end
end

.results(name) ⇒ Object



210
211
212
213
214
215
# File 'lib/simplews/jobs.rb', line 210

def self.results(name)
  job_info(name)[:results].collect{|file|
    code = Scheduler.random_name("res-")
    [code, file]
  }
end

.save(name, state) ⇒ Object



183
184
185
186
187
# File 'lib/simplews/jobs.rb', line 183

def self.save(name, state)
  fout = File.open(File.join(@@savedir,name + '.marshal'),'w')
  fout.write Marshal::dump(state)
  fout.close
end

.taken(name = "") ⇒ Object



172
173
174
# File 'lib/simplews/jobs.rb', line 172

def self.taken(name = "")
  Dir.glob(@@savedir + "/^#{ Regexp.quote name }.marshal").collect{|n| n.match(/^(#{ Regexp.quote name }).marshal/)[1]}
end

.workdir=(workdir) ⇒ Object



165
166
167
168
169
170
# File 'lib/simplews/jobs.rb', line 165

def self.workdir=(workdir)
  @@workdir = workdir
  @@savedir = File.join(@@workdir, '.save')
  FileUtils.mkdir_p @@workdir unless File.exist? @@workdir
  FileUtils.mkdir_p @@savedir unless File.exist? @@savedir
end

Instance Method Details

#abortObject



270
271
272
273
# File 'lib/simplews/jobs.rb', line 270

def abort
  raise SimpleWS::Jobs::Aborted
  save
end

#configObject



226
227
228
# File 'lib/simplews/jobs.rb', line 226

def config
  @@config
end

#error(message = nil) ⇒ Object



255
256
257
258
# File 'lib/simplews/jobs.rb', line 255

def error(message = nil)
  step(:error, message)
  save
end

#info(info) ⇒ Object



260
261
262
263
# File 'lib/simplews/jobs.rb', line 260

def info(info)
  @state[:info].merge!(info)
  save
end

#job_nameObject



275
276
277
# File 'lib/simplews/jobs.rb', line 275

def job_name
  @name
end

#message(message) ⇒ Object



245
246
247
248
# File 'lib/simplews/jobs.rb', line 245

def message(message)
  @state[:messages] << message 
  save
end

#path(file) ⇒ Object



230
231
232
# File 'lib/simplews/jobs.rb', line 230

def path(file)
  Job.path(file,  @name)
end

#results(results) ⇒ Object



265
266
267
268
# File 'lib/simplews/jobs.rb', line 265

def results(results)
  @state[:results] = results.collect{|file| path(file)}
  save
end

#run(task, name, results, *args) ⇒ Object



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
# File 'lib/simplews/jobs.rb', line 279

def run(task, name, results, *args)
  @name = name
  @state = {
    :name => @name, 
    :status => :prepared, 
    :messages => [], 
    :info => {}, 
    :results => results.collect{|file| path(file)},
  }
  save
  @pid = Process.fork do
    begin
      puts "Job #{@name} starting with pid #{Process.pid}"

      trap(:INT) { raise SimpleWS::Jobs::Aborted }
      self.send task, *args
      step :done
      exit(0)
    rescue  SimpleWS::Jobs::Aborted
      step(:aborted, "Job Aborted")
      exit(-1)
    rescue Exception
      if !$!.kind_of? SystemExit
        error($!.message)
        puts "Error in job #{ @name }"
        puts $!.message
        puts $!.backtrace
        exit(-1)
      end
    end
  end

  @pid
end

#saveObject



234
235
236
# File 'lib/simplews/jobs.rb', line 234

def save
  Job.save(@name, @state)
end

#step(status, message = nil) ⇒ Object



249
250
251
252
253
# File 'lib/simplews/jobs.rb', line 249

def step(status, message = nil)
  @state[:status] = status
  @state[:messages] << message if message && message != ""
  save
end

#workdirObject



222
223
224
# File 'lib/simplews/jobs.rb', line 222

def workdir
  @@workdir
end

#write(file, contents) ⇒ Object



238
239
240
241
242
243
# File 'lib/simplews/jobs.rb', line 238

def write(file, contents)
  path = Job.path(file, @name)
  fout = File.open(path,'w')
  fout.write contents
  fout.close
end