Class: RQ::JobQueue
- Inherits:
-
Object
- Object
- RQ::JobQueue
- Defined in:
- lib/rq/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.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#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
–}}}.
-
#data4(jid) ⇒ Object
–}}}.
-
#data_4(jid) ⇒ 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.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rq/jobqueue.rb', line 57 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' @data = File::join @path, 'data' @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.
48 49 50 |
# File 'lib/rq/jobqueue.rb', line 48 def bin @bin end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
52 53 54 |
# File 'lib/rq/jobqueue.rb', line 52 def data @data end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
53 54 55 |
# File 'lib/rq/jobqueue.rb', line 53 def opts @opts end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
47 48 49 |
# File 'lib/rq/jobqueue.rb', line 47 def path @path end |
#qdb ⇒ Object (readonly) Also known as: db
Returns the value of attribute qdb.
54 55 56 |
# File 'lib/rq/jobqueue.rb', line 54 def qdb @qdb end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
51 52 53 |
# File 'lib/rq/jobqueue.rb', line 51 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
49 50 51 |
# File 'lib/rq/jobqueue.rb', line 49 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
50 51 52 |
# File 'lib/rq/jobqueue.rb', line 50 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 43 |
# File 'lib/rq/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 FileUtils::mkdir_p q.data q #--}}} end |
Instance Method Details
#[]=(key, value) ⇒ Object
–}}}
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 |
# File 'lib/rq/jobqueue.rb', line 923 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
–}}}
851 852 853 854 855 |
# File 'lib/rq/jobqueue.rb', line 851 def abort_transaction(*a,&b) #--{{{ @qdb.abort_transaction(*a,&b) #--}}} end |
#attributes ⇒ Object
–}}}
941 942 943 944 945 946 947 948 |
# File 'lib/rq/jobqueue.rb', line 941 def attributes #--{{{ h = {} tuples = @qdb.execute "select * from attributes;" tuples.map!{|t| h[t['key']] = t['value']} h #--}}} end |
#data4(jid) ⇒ Object
–}}}
106 107 108 109 110 |
# File 'lib/rq/jobqueue.rb', line 106 def data4 jid #--{{{ "data/#{ jid }" #--}}} end |
#data_4(jid) ⇒ Object
–}}}
111 112 113 114 115 |
# File 'lib/rq/jobqueue.rb', line 111 def data_4 jid #--{{{ File::(File::join(path, data4(jid))) #--}}} end |
#delete(*args, &block) ⇒ Object
–}}}
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 572 573 574 575 576 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 |
# File 'lib/rq/jobqueue.rb', line 538 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), data_4(jid)].each do |path| FileUtils::rm_rf 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
–}}}
831 832 833 834 835 |
# File 'lib/rq/jobqueue.rb', line 831 def execute(*args, &block) #--{{{ @qdb.execute(*args, &block) #--}}} end |
#getdeadjobs(started, &block) ⇒ Object
–}}}
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
# File 'lib/rq/jobqueue.rb', line 769 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
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 |
# File 'lib/rq/jobqueue.rb', line 724 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
–}}}
836 837 838 839 840 |
# File 'lib/rq/jobqueue.rb', line 836 def integrity_check(*args, &block) #--{{{ @qdb.integrity_check(*args, &block) #--}}} end |
#jobisdead(job) ⇒ Object
–}}}
787 788 789 790 791 792 793 794 795 796 |
# File 'lib/rq/jobqueue.rb', line 787 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
–}}}
755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
# File 'lib/rq/jobqueue.rb', line 755 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
–}}}
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 |
# File 'lib/rq/jobqueue.rb', line 739 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
–}}}
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 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 |
# File 'lib/rq/jobqueue.rb', line 288 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
–}}}
846 847 848 849 850 |
# File 'lib/rq/jobqueue.rb', line 846 def lock(*args, &block) #--{{{ @qdb.lock(*args, &block) #--}}} end |
#mtime ⇒ Object
TODO - use mtime to optimize checks by feeder??
918 919 920 921 922 |
# File 'lib/rq/jobqueue.rb', line 918 def mtime #--{{{ File::stat(@path).mtime #--}}} end |
#query(where_clause = nil, &block) ⇒ Object
–}}}
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 |
# File 'lib/rq/jobqueue.rb', line 508 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
–}}}
841 842 843 844 845 |
# File 'lib/rq/jobqueue.rb', line 841 def recover!(*args, &block) #--{{{ @qdb.recover!(*args, &block) #--}}} end |
#resubmit(*jobs, &block) ⇒ Object
–}}}
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 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/rq/jobqueue.rb', line 182 def resubmit(*jobs, &block) #--{{{ now = Util:: Time::now transaction do jobs.each do |job| jid = Integer job['jid'] command = job['command'] stdin = job['stdin'] data = job['data'] 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 tuple['data'] = data4 jid 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 data_4(jid) =begin p :job => job p :ts => ts p :pwd => Dir.pwd p :data => data p :data_4 => data_4(jid) p :standard_in_4 => standard_in_4(jid) exit!(1) =end FileUtils::cp ts.path, standard_in_4(jid) if ts if data FileUtils::cp data, data_4(jid) else FileUtils::mkdir_p data_4(jid) end 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
–}}}
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 |
# File 'lib/rq/jobqueue.rb', line 815 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
–}}}
856 857 858 859 860 |
# File 'lib/rq/jobqueue.rb', line 856 def rollback_transaction(*a,&b) #--{{{ @qdb.rollback_transaction(*a,&b) #--}}} end |
#snapshot(qtmp = "#{ @basename }.snapshot", retries = nil) ⇒ Object
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 |
# File 'lib/rq/jobqueue.rb', line 862 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
–}}}
101 102 103 104 105 |
# File 'lib/rq/jobqueue.rb', line 101 def standard_err_4 jid #--{{{ File::(File::join(path, stderr4(jid))) #--}}} end |
#standard_in_4(jid) ⇒ Object
–}}}
81 82 83 84 85 |
# File 'lib/rq/jobqueue.rb', line 81 def standard_in_4 jid #--{{{ File::(File::join(path, stdin4(jid))) #--}}} end |
#standard_out_4(jid) ⇒ Object
–}}}
91 92 93 94 95 |
# File 'lib/rq/jobqueue.rb', line 91 def standard_out_4 jid #--{{{ File::(File::join(path, stdout4(jid))) #--}}} end |
#status(options = {}) ⇒ Object
–}}}
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 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 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/rq/jobqueue.rb', line 344 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
–}}}
96 97 98 99 100 |
# File 'lib/rq/jobqueue.rb', line 96 def stderr4 jid #--{{{ "stderr/#{ jid }" #--}}} end |
#stdin4(jid) ⇒ Object
–}}}
76 77 78 79 80 |
# File 'lib/rq/jobqueue.rb', line 76 def stdin4 jid #--{{{ "stdin/#{ jid }" #--}}} end |
#stdout4(jid) ⇒ Object
–}}}
86 87 88 89 90 |
# File 'lib/rq/jobqueue.rb', line 86 def stdout4 jid #--{{{ "stdout/#{ jid }" #--}}} end |
#submit(*jobs, &block) ⇒ Object
–}}}
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 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 |
# File 'lib/rq/jobqueue.rb', line 116 def submit(*jobs, &block) #--{{{ if jobs.size == 1 and jobs.first.is_a?(String) jobs = [ { "command" => jobs.to_s } ] end 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'] data = job['data'] 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 tuple['data'] = data4 jid 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::rm_rf data_4(jid) FileUtils::cp ts.path, standard_in_4(jid) if ts if data FileUtils::cp_r data, data_4(jid) else FileUtils::mkdir_p data_4(jid) end 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
–}}}
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 |
# File 'lib/rq/jobqueue.rb', line 247 def tmp_stdin stdin = nil #--{{{ #stdin = nil if stdin.to_s.empty? if stdin.to_s.empty? return(block_given? ? yield(nil) : nil) end 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
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 |
# File 'lib/rq/jobqueue.rb', line 798 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
–}}}
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 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 |
# File 'lib/rq/jobqueue.rb', line 620 def update(kvs, *jids, &block) #--{{{ ret = nil # # yank out stdin - which we allow as a key # stdin = kvs.delete 'stdin' data = kvs.delete 'data' # # 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 do |job| FileUtils::cp ts.path, standard_in_4(job['jid']) if ts true end clobber_data = lambda do |job| if data FileUtils::rm_rf data_4(job['jid']) FileUtils::cp_r data, data_4(job['jid']) end true end tuples = [] = if block lambda{|job| clobber_stdin[job] and clobber_data[job] and block[job]} else lambda{|job| clobber_stdin[job] and clobber_data[job] and 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
–}}}
615 616 617 618 619 |
# File 'lib/rq/jobqueue.rb', line 615 def vacuum #--{{{ @qdb.vacuum #--}}} end |