Class: RQ::JobQueue
- Inherits:
-
Object
- Object
- RQ::JobQueue
- Defined in:
- lib/rq-3.0.0/jobqueue.rb
Overview
the JobQueue class is responsible for high level access to the job queue
Defined Under Namespace
Classes: Error
Constant Summary collapse
- MAX_JID =
2 ** 20
Constants included from Logging
Logging::DIV0, Logging::DIV1, Logging::DIV2, Logging::DIV3, Logging::EOL, Logging::SEC0, Logging::SEC1, Logging::SEC2, Logging::SEC3
Instance Attribute Summary collapse
-
#bin ⇒ Object
readonly
Returns the value of attribute bin.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#qdb ⇒ Object
(also: #db)
readonly
Returns the value of attribute qdb.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
–}}}.
-
#abort_transaction(*a, &b) ⇒ Object
–}}}.
-
#attributes ⇒ Object
–}}}.
-
#delete(*args, &block) ⇒ Object
–}}}.
-
#execute(*args, &block) ⇒ Object
–}}}.
-
#getdeadjobs(started, &block) ⇒ Object
–}}}.
- #getjob ⇒ Object
-
#initialize(path, opts = {}) ⇒ JobQueue
constructor
A new instance of JobQueue.
-
#integrity_check(*args, &block) ⇒ Object
–}}}.
-
#jobisdead(job) ⇒ Object
–}}}.
-
#jobisdone(job) ⇒ Object
–}}}.
-
#jobisrunning(job) ⇒ Object
–}}}.
-
#list(*whats, &block) ⇒ Object
–}}}.
-
#lock(*args, &block) ⇒ Object
–}}}.
-
#mtime ⇒ Object
TODO - use mtime to optimize checks by feeder??.
-
#query(where_clause = nil, &block) ⇒ Object
–}}}.
-
#recover!(*args, &block) ⇒ Object
–}}}.
-
#resubmit(*jobs, &block) ⇒ Object
–}}}.
-
#ro_transaction(*args) ⇒ Object
–}}}.
-
#rollback_transaction(*a, &b) ⇒ Object
–}}}.
- #snapshot(qtmp = "#{ @basename }.snapshot", retries = nil) ⇒ Object
-
#standard_err_4(jid) ⇒ Object
–}}}.
-
#standard_in_4(jid) ⇒ Object
–}}}.
-
#standard_out_4(jid) ⇒ Object
–}}}.
-
#status(options = {}) ⇒ Object
–}}}.
-
#stderr4(jid) ⇒ Object
–}}}.
-
#stdin4(jid) ⇒ Object
–}}}.
-
#stdout4(jid) ⇒ Object
–}}}.
-
#submit(*jobs, &block) ⇒ Object
–}}}.
-
#tmp_stdin(stdin = nil) ⇒ Object
–}}}.
- #transaction(*args) ⇒ Object
-
#update(kvs, *jids, &block) ⇒ Object
–}}}.
-
#vacuum ⇒ Object
–}}}.
Methods included from Util
#alive?, append_features, #btrace, #columnize, #defval, #emsg, #erreq, #errmsg, #escape, #escape!, #exec, export, #fork, #getopt, #hashify, #hms, #host, #hostname, #klass, #maim, #mcp, #realpath, #stamptime, #system, #timestamp, #tmpnam, #uncache, #which_ruby
Methods included from Logging
Methods included from Logging::LogMethods
#debug, #error, #fatal, #info, #logerr, #logger, #logger=, #warn
Constructor Details
#initialize(path, opts = {}) ⇒ JobQueue
Returns a new instance of JobQueue.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 55 def initialize path, opts = {} #--{{{ @path = path # do NOT expand this or it'll be fubar from misc nfs mounts!! @bin = File::join @path, 'bin' @stdin = File::join @path, 'stdin' @stdout = File::join @path, 'stdout' @stderr = File::join @path, 'stderr' @opts = opts raise "q <#{ @path }> does not exist" unless test ?e, @path raise "q <#{ @path }> is not a directory" unless test ?d, @path @basename = File::basename(@path) @dirname = File::dirname(@path) @logger = getopt('logger', opts) || Logger::new(STDERR) @qdb = getopt('qdb', opts) || QDB::new(File::join(@path, 'db'), 'logger' => @logger) @in_transaction = false @in_ro_transaction = false #--}}} end |
Instance Attribute Details
#bin ⇒ Object (readonly)
Returns the value of attribute bin.
47 48 49 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 47 def bin @bin end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
51 52 53 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 51 def opts @opts end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
46 47 48 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 46 def path @path end |
#qdb ⇒ Object (readonly) Also known as: db
Returns the value of attribute qdb.
52 53 54 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 52 def qdb @qdb end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
50 51 52 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 50 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
48 49 50 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 48 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
49 50 51 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 49 def stdout @stdout end |
Class Method Details
.create(path, opts = {}) ⇒ Object
–{{{
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 28 def create path, opts = {} #--{{{ FileUtils::rm_rf path FileUtils::mkdir_p path db = File::join path, 'db' qdb = QDB.create db, opts opts['qdb'] = qdb q = new path, opts FileUtils::mkdir_p q.bin FileUtils::mkdir_p q.stdin FileUtils::mkdir_p q.stdout FileUtils::mkdir_p q.stderr q #--}}} end |
Instance Method Details
#[]=(key, value) ⇒ Object
–}}}
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 868 def []= key, value #--{{{ sql = "select count(*) from attributes where key='#{ key }';" tuples = @qdb.execute sql tuple = tuples.first count = Integer tuple['count(*)'] case count when 0 sql = "insert into attributes values('#{ key }','#{ value }');" @qdb.execute sql when 1 sql = "update attributes set key='#{ key }', value='#{ value }' where key='#{ key }';" @qdb.execute sql else raise "key <#{ key }> has become corrupt!" end #--}}} end |
#abort_transaction(*a, &b) ⇒ Object
–}}}
796 797 798 799 800 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 796 def abort_transaction(*a,&b) #--{{{ @qdb.abort_transaction(*a,&b) #--}}} end |
#attributes ⇒ Object
–}}}
886 887 888 889 890 891 892 893 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 886 def attributes #--{{{ h = {} tuples = @qdb.execute "select * from attributes;" tuples.map!{|t| h[t['key']] = t['value']} h #--}}} end |
#delete(*args, &block) ⇒ Object
–}}}
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 495 def delete(*args, &block) #--{{{ whats, optargs = args.partition{|arg| not Hash === arg} opts = {} optargs.each{|oa| opts.update oa} force = Util::getopt 'force', opts delete_sql, select_sql = '', '' whats << 'all' if whats.empty? whats.each do |what| case "#{ what }" when %r/^\s*\d+\s*$/io # number delete_sql << "delete from jobs where jid=#{ what } and state!='running';\n" select_sql << "select * from jobs where jid=#{ what } and state!='running';\n" when %r/^\s*p/io # pending delete_sql << "delete from jobs where state='pending';\n" select_sql << "select * from jobs where state='pending';\n" when %r/^\s*h/io # holding delete_sql << "delete from jobs where state='holding';\n" select_sql << "select * from jobs where state='holding';\n" when %r/^\s*r/io # running delete_sql << "delete from jobs where state='running';\n" if force select_sql << "select * from jobs where state='running';\n" if force when %r/^\s*f/io # finished delete_sql << "delete from jobs where state='finished';\n" select_sql << "select * from jobs where state='finished';\n" when %r/^\s*d/io # dead delete_sql << "delete from jobs where state='dead';\n" select_sql << "select * from jobs where state='dead';\n" when %r/^\s*a/io # all delete_sql << "delete from jobs where state!='running';\n" select_sql << "select * from jobs where state!='running';\n" else raise ArgumentError, "cannot delete <#{ what.inspect }>" end end scrub = lambda do |jid| [standard_in_4(jid), standard_out_4(jid), standard_err_4(jid)].each do |path| FileUtils::rm_f path end end tuples = [] = if block lambda do |tuple| jid = tuple['jid'] block[tuple] scrub[jid] end else lambda do |tuple| jid = tuple['jid'] scrub[jid] tuples << tuple end end # TODO - make file deletion transactional too transaction do execute(select_sql, &) execute(delete_sql){} end delete_sql = nil select_sql = nil block ? nil : tuples #--}}} end |
#execute(*args, &block) ⇒ Object
–}}}
776 777 778 779 780 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 776 def execute(*args, &block) #--{{{ @qdb.execute(*args, &block) #--}}} end |
#getdeadjobs(started, &block) ⇒ Object
–}}}
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 714 def getdeadjobs(started, &block) #--{{{ ret = nil sql = <<-sql select * from jobs where state = 'running' and runner='#{ Util::hostname }' and started<='#{ started }' sql if block execute(sql, &block) else ret = execute(sql) end ret #--}}} end |
#getjob ⇒ Object
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 669 def getjob #--{{{ sql = <<-sql select * from jobs where (state='pending' or (state='dead' and (not restartable isnull))) and (runner like '%#{ Util::host }%' or runner isnull) order by priority desc, submitted asc, jid asc limit 1; sql tuples = execute sql job = tuples.first job #--}}} end |
#integrity_check(*args, &block) ⇒ Object
–}}}
781 782 783 784 785 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 781 def integrity_check(*args, &block) #--{{{ @qdb.integrity_check(*args, &block) #--}}} end |
#jobisdead(job) ⇒ Object
–}}}
732 733 734 735 736 737 738 739 740 741 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 732 def jobisdead job #--{{{ jid = job['jid'] if jid sql = "update jobs set state='dead' where jid='#{ jid }'" execute(sql){} end job #--}}} end |
#jobisdone(job) ⇒ Object
–}}}
700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 700 def jobisdone job #--{{{ sql = <<-sql update jobs set state = '#{ job['state'] }', exit_status = '#{ job['exit_status'] }', finished = '#{ job['finished'] }', elapsed = '#{ job['elapsed'] }' where jid = #{ job['jid'] }; sql execute sql #--}}} end |
#jobisrunning(job) ⇒ Object
–}}}
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 684 def jobisrunning job #--{{{ sql = <<-sql update jobs set pid='#{ job['pid'] }', state='#{ job['state'] }', started='#{ job['started'] }', runner='#{ job['runner'] }', stdout='#{ job['stdout'] }', stderr='#{ job['stderr'] }' where jid=#{ job['jid'] }; sql execute sql #--}}} end |
#list(*whats, &block) ⇒ Object
–}}}
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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 245 def list(*whats, &block) #--{{{ ret = nil whats.replace(%w( pending running finished dead )) if whats.empty? or whats.include?('all') whats.map! do |what| case what when %r/^\s*p/io 'pending' when %r/^\s*h/io 'holding' when %r/^\s*r/io 'running' when %r/^\s*f/io 'finished' when %r/^\s*d/io 'dead' else what end end where_clauses = [] whats.each do |what| case what when Numeric where_clauses << "jid=#{ what }\n" else what = "#{ what }" if what.to_s =~ %r/^\s*\d+\s*$/o where_clauses << "jid=#{ QDB::q what }\n" else where_clauses << "state=#{ QDB::q what }\n" end end end where_clause = where_clauses.join(" or \n") sql = <<-sql select * from jobs where #{ where_clause } sql if block ro_transaction{ execute(sql, &block) } else ret = ro_transaction{ execute(sql) } end ret #--}}} end |
#lock(*args, &block) ⇒ Object
–}}}
791 792 793 794 795 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 791 def lock(*args, &block) #--{{{ @qdb.lock(*args, &block) #--}}} end |
#mtime ⇒ Object
TODO - use mtime to optimize checks by feeder??
863 864 865 866 867 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 863 def mtime #--{{{ File::stat(@path).mtime #--}}} end |
#query(where_clause = nil, &block) ⇒ Object
–}}}
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 465 def query(where_clause = nil, &block) #--{{{ ret = nil sql = if where_clause # # turn =~ into like clauses # #where_clause.gsub!(/(=~\s*([^\s')(=]+))/om){q = $2.gsub(%r/'+|\s+/o,''); "like '%#{ q }%'"} # # quote everything on the rhs of an '=' sign - helps with shell problems... # #where_clause.gsub!(/(==?\s*([^\s')(=]+))/om){q = $2.gsub(%r/'+|\s+/o,''); "='#{ q }'"} "select * from jobs where #{ where_clause };" else "select * from jobs;" end if block ro_transaction{ execute(sql, &block) } else ret = ro_transaction{ execute(sql) } end ret #--}}} end |
#recover!(*args, &block) ⇒ Object
–}}}
786 787 788 789 790 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 786 def recover!(*args, &block) #--{{{ @qdb.recover!(*args, &block) #--}}} end |
#resubmit(*jobs, &block) ⇒ Object
–}}}
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 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 157 def resubmit(*jobs, &block) #--{{{ now = Util:: Time::now transaction do jobs.each do |job| jid = Integer job['jid'] command = job['command'] stdin = job['stdin'] raise "no jid for job <#{ job.inspect }>" unless jid raise "no command for job <#{ job.inspect }>" unless command tmp_stdin(stdin) do |ts| tuple = QDB::tuple tuple['jid'] = jid tuple['command'] = command tuple['priority'] = job['priority'] || 0 tuple['tag'] = job['tag'] tuple['runner'] = job['runner'] tuple['restartable'] = job['restartable'] tuple['state'] = 'pending' tuple['submitted'] = now tuple['submitter'] = Util::hostname tuple['stdin'] = stdin4 jid tuple['stdout'] = nil tuple['stderr'] = nil kvs = tuple.fields[1..-1].map{|f| "#{ f }=#{ QDB::q(tuple[ f ]) }"} sql = "update jobs set #{ kvs.join ',' } where jid=#{ jid };\n" execute(sql){} FileUtils::rm_rf standard_in_4(jid) FileUtils::rm_rf standard_out_4(jid) FileUtils::rm_rf standard_err_4(jid) FileUtils::cp ts.path, standard_in_4(jid) if ts if block sql = "select * from jobs where jid = '#{ jid }'" execute(sql, &block) end end # tmp_stdin end # jobs.each end # transaction self #--}}} end |
#ro_transaction(*args) ⇒ Object
–}}}
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 760 def ro_transaction(*args) #--{{{ ret = nil if @in_ro_transaction || @in_transaction ret = yield else begin @in_ro_transaction = true @qdb.ro_transaction(*args){ ret = yield } ensure @in_ro_transaction = false end end ret #--}}} end |
#rollback_transaction(*a, &b) ⇒ Object
–}}}
801 802 803 804 805 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 801 def rollback_transaction(*a,&b) #--{{{ @qdb.rollback_transaction(*a,&b) #--}}} end |
#snapshot(qtmp = "#{ @basename }.snapshot", retries = nil) ⇒ Object
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 807 def snapshot qtmp = "#{ @basename }.snapshot", retries = nil #--{{{ qtmp ||= "#{ @basename }.snapshot" debug{ "snapshot <#{ @path }> -> <#{ qtmp }>" } retries = Integer(retries || 16) debug{ "retries <#{ retries }>" } qss = nil loopno = 0 take_snapshot = lambda do FileUtils::rm_rf qtmp FileUtils::mkdir_p qtmp %w(db db.schema lock).each do |base| src, dest = File::join(@path, base), File::join(qtmp, base) debug{ "cp <#{ src }> -> <#{ dest }>" } FileUtils::cp(src, dest) end ss = klass::new qtmp, @opts if ss.integrity_check ss else begin; recover! unless integrity_check; rescue; nil; end ss.recover! end end loop do break if loopno >= retries if((ss = take_snapshot.call)) debug{ "snapshot <#{ qtmp }> created" } qss = ss break else debug{ "failure <#{ loopno + 1}> of <#{ retries }> attempts to create snapshot <#{ qtmp }> - retrying..." } end loopno += 1 end unless qss debug{ "locking <#{ @path }> as last resort" } @qdb.write_lock do if((ss = take_snapshot.call)) debug{ "snapshot <#{ qtmp }> created" } qss = ss else raise "failed <#{ loopno }> times to create snapshot <#{ qtmp }>" end end end qss #--}}} end |
#standard_err_4(jid) ⇒ Object
–}}}
98 99 100 101 102 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 98 def standard_err_4 jid #--{{{ File::(File::join(path, stderr4(jid))) #--}}} end |
#standard_in_4(jid) ⇒ Object
–}}}
78 79 80 81 82 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 78 def standard_in_4 jid #--{{{ File::(File::join(path, stdin4(jid))) #--}}} end |
#standard_out_4(jid) ⇒ Object
–}}}
88 89 90 91 92 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 88 def standard_out_4 jid #--{{{ File::(File::join(path, stdout4(jid))) #--}}} end |
#status(options = {}) ⇒ Object
–}}}
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 301 def status = {} #--{{{ stats = OrderedAutoHash::new now = Time::now hms = lambda do |t| elapsed = begin Float t rescue now - Util::stamptime(t, 'local' => true) end sh, sm, ss = Util::hms elapsed.to_f s = "#{ '%2.2d' % sh }h#{ '%2.2d' % sm }m#{ '%05.2f' % ss }s" end exit_code_map = [:exit_code_map] || ['exit_code_map'] || {} ro_transaction do # # jobs stats # total = 0 %w( pending holding running finished dead ).each do |state| sql = <<-sql select count(*) from jobs where state='#{ state }' sql tuples = execute sql tuple = tuples.first count = (tuple ? Integer(tuple.first || 0) : 0) stats['jobs'][state] = count total += count end stats['jobs']['total'] = total # # temporal stats # metrics = OrderedAutoHash::new metrics['pending'] = 'submitted' metrics['holding'] = 'submitted' metrics['running'] = 'started' metrics['finished'] = 'elapsed' metrics['dead'] = 'elapsed' metrics.each do |state, metric| sql = unless metric == 'elapsed' <<-sql select min(#{ metric }) as max, max(#{ metric }) as min from jobs where state='#{ state }' sql else <<-sql select min(#{ metric }) as min, max(#{ metric }) as max from jobs where state='#{ state }' sql end tuple = execute(sql).first next unless tuple %w( min max ).each do |time| oh = nil t = tuple[time] if t sql = <<-sql select jid from jobs where #{ metric }='#{ t }' and state='#{ state }' sql which = execute(sql).first jid = (which and which['jid']).to_i if jid oh = OrderedAutoHash::new oh[jid] = hms[t] oh.yaml_inline = true end stats['temporal'][state][time] = oh end end #stats['temporal'][state] ||= nil end stats['temporal'] ||= nil # # generate performance stats # sql = <<-sql select avg(elapsed) from jobs where state='finished' sql tuples = execute sql tuple = tuples.first avg = (tuple ? Float(tuple.first || 0) : 0) stats['performance']['avg_time_per_job'] = hms[avg] list = [] 0.step(5){|i| list << (2 ** i)} list << 24 list.sort! list = 1, 12, 24 list.each do |n| ago = now - (n * 3600) ago = Util:: ago sql = <<-sql select count(*) from jobs where state = 'finished' and finished > '#{ ago }' sql tuples = execute sql tuple = tuples.first count = (tuple ? Integer(tuple.first || 0) : 0) #stats['performance']["n_jobs_in_last_#{ n }_hrs"] = count stats['performance']["n_jobs_in_last_hrs"][n] = count end # # generate exit_status stats # #stats['exit_status'] = {} sql = <<-sql select count(*) from jobs where state='finished' and exit_status=0 sql tuples = execute sql tuple = tuples.first successes = (tuple ? Integer(tuple.first || 0) : 0) stats['exit_status']['successes'] = successes sql = <<-sql select count(*) from jobs where (state='finished' and exit_status!=0) or state='dead' sql tuples = execute sql tuple = tuples.first failures = (tuple ? Integer(tuple.first || 0) : 0) stats['exit_status']['failures'] = failures exit_code_map.each do |which, codes| exit_status_clause = codes.map{|code| "exit_status=#{ code }"}.join(' or ') sql = <<-sql select count(*) from jobs where (state='finished' and (#{ exit_status_clause })) sql tuples = execute sql tuple = tuples.first n = (tuple ? Integer(tuple.first || 0) : 0) stats['exit_status'][which] = n end end stats #--}}} end |
#stderr4(jid) ⇒ Object
–}}}
93 94 95 96 97 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 93 def stderr4 jid #--{{{ "stderr/#{ jid }" #--}}} end |
#stdin4(jid) ⇒ Object
–}}}
73 74 75 76 77 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 73 def stdin4 jid #--{{{ "stdin/#{ jid }" #--}}} end |
#stdout4(jid) ⇒ Object
–}}}
83 84 85 86 87 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 83 def stdout4 jid #--{{{ "stdout/#{ jid }" #--}}} end |
#submit(*jobs, &block) ⇒ Object
–}}}
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 103 def submit(*jobs, &block) #--{{{ now = Util:: Time::now transaction do sql = "select max(jid) from jobs" tuple = execute(sql).first jid = tuple.first || 0 jid = Integer(jid) + 1 jobs.each do |job| command = job['command'] stdin = job['stdin'] raise "no command for job <#{ job.inspect }>" unless command tmp_stdin(stdin) do |ts| tuple = QDB::tuple tuple['command'] = command tuple['priority'] = job['priority'] || 0 tuple['tag'] = job['tag'] tuple['runner'] = job['runner'] tuple['restartable'] = job['restartable'] tuple['state'] = 'pending' tuple['submitted'] = now tuple['submitter'] = Util::hostname tuple['stdin'] = stdin4 jid tuple['stdout'] = nil tuple['stderr'] = nil values = QDB::q tuple sql = "insert into jobs values (#{ values.join ',' });\n" execute(sql){} FileUtils::rm_rf standard_in_4(jid) FileUtils::rm_rf standard_out_4(jid) FileUtils::rm_rf standard_err_4(jid) FileUtils::cp ts.path, standard_in_4(jid) if ts if block sql = "select * from jobs where jid = '#{ jid }'" execute(sql, &block) end end jid += 1 end end self #--}}} end |
#tmp_stdin(stdin = nil) ⇒ Object
–}}}
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 236 237 238 239 240 241 242 243 244 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 207 def tmp_stdin stdin = nil #--{{{ stdin = nil if stdin.to_s.empty? stdin = STDIN if stdin == '-' was_opened = false begin unless stdin.respond_to?('read') or stdin.nil? stdin = stdin.to_s # relative to queue if stdin =~ %r|^@?stdin/\d+$| stdin.gsub! %r|^@|, '' stdin = File::join(path, stdin) end stdin = File. stdin stdin = open stdin was_opened = true end tmp = Tempfile::new "#{ Process::pid }_#{ rand }" while((buf = stdin.read(8192))); tmp.write buf; end if stdin tmp.close if block_given? begin yield tmp ensure tmp.close! end else return tmp end ensure stdin.close if was_opened rescue nil end #--}}} end |
#transaction(*args) ⇒ Object
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 743 def transaction(*args) #--{{{ raise "cannot upgrade ro_transaction" if @in_ro_transaction ret = nil if @in_transaction ret = yield else begin @in_transaction = true @qdb.transaction(*args){ ret = yield } ensure @in_transaction = false end end ret #--}}} end |
#update(kvs, *jids, &block) ⇒ Object
–}}}
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 577 def update(kvs, *jids, &block) #--{{{ ret = nil # # yank out stdin - which we allow as a key # stdin = kvs.delete 'stdin' # # validate/munge state value iff present # if((state = kvs['state'])) case state when %r/^p/io kvs['state'] = 'pending' when %r/^h/io kvs['state'] = 'holding' else raise "update of <state> = <#{ state }> not allowed (try pending or holding)" end end # # validate kvs pairs # allowed = %w( priority command tag runner restartable ) kvs.each do |key, val| raise "update of <#{ key }> = <#{ val }> not allowed" unless (allowed.include?(key)) or (key == 'state' and %w( pending holding ).include?(val)) end # # ensure there are acutally some jobs to update # raise "no jobs to update" if jids.empty? # # generates sql to update jids with kvs and sql to show updated tuples # build_sql = lambda do |kvs, jids| if(jids.delete('pending')) execute("select jid from jobs where state='pending'") do |tuple| jids << tuple['jid'] end end if(jids.delete('holding')) execute("select jid from jobs where state='holding'") do |tuple| jids << tuple['jid'] end end rollback_transaction "no jobs to update" if jids.empty? update_clause = kvs.map{|k,v| v ? "#{ k }='#{ v }'" : "#{ k }=NULL" }.join(",\n") where_clause = jids.map{|jid| "jid=#{ jid }"}.join(" or\n") update_sql = "update jobs\n" << "set\n#{ update_clause }\n" << "where\n(state='pending' or state='holding') and\n(#{ where_clause })" select_sql = "select * from jobs where (state='pending' or state='holding') and\n(#{ where_clause })" if kvs.empty? [ nil, select_sql ] else [ update_sql, select_sql ] end end # # setup stdin # tmp_stdin(stdin) do |ts| clobber_stdin = lambda{|job| FileUtils::cp ts.path, standard_in_4(job['jid']) if ts} tuples = [] = if block lambda{|job| clobber_stdin[job]; block[job]} else lambda{|job| clobber_stdin[job]; tuples << job} end transaction do update_sql, select_sql = build_sql[kvs, jids] break unless select_sql execute(update_sql){} if update_sql execute(select_sql, &) end block ? nil : tuples end #--}}} end |
#vacuum ⇒ Object
–}}}
572 573 574 575 576 |
# File 'lib/rq-3.0.0/jobqueue.rb', line 572 def vacuum #--{{{ @qdb.vacuum #--}}} end |