Class: RQ::QDB
Overview
the QDB class is the low level access point to the actual sqlite database. the primary function if performs is to serialize access to the queue db via the locking protocol
Defined Under Namespace
Classes: AbortedTransactionError, RollbackTransactionError
Constant Summary collapse
- FIELDS =
%w( jid priority state submitted started finished elapsed submitter runner stdin stdout stderr data pid exit_status tag restartable command )
- PRAGMAS =
–}}}
<<-sql PRAGMA default_synchronous = FULL; sql
- SCHEMA =
–}}}
<<-sql create table jobs ( jid integer primary key, #{ FIELDS[1..-1].join ",\n " } ); create table attributes ( key, value, primary key (key) ); sql
- DEFAULT_LOGGER =
–}}}
Logger::new(STDERR)
- DEFAULT_SQL_DEBUG =
false
- DEFAULT_TRANSACTION_RETRIES =
4
- DEFAULT_AQUIRE_LOCK_SC =
SleepCycle::new(2, 16, 2)
- DEFAULT_TRANSACTION_RETRIES_SC =
SleepCycle::new(8, 24, 8)
- DEFAULT_ATTEMPT_LOCKD_RECOVERY =
true
- DEFAULT_LOCKD_RECOVER_WAIT =
1 hr
3600
- DEFAULT_AQUIRE_LOCK_LOCKFILE_STALE_AGE =
6 hrs
21600
- DEFAULT_AQUIRE_LOCK_REFRESH_RATE =
30
Constants included from Logging
Logging::DIV0, Logging::DIV1, Logging::DIV2, Logging::DIV3, Logging::EOL, Logging::SEC0, Logging::SEC1, Logging::SEC2, Logging::SEC3
Class Attribute Summary collapse
-
.aquire_lock_lockfile_stale_age ⇒ Object
Returns the value of attribute aquire_lock_lockfile_stale_age.
-
.aquire_lock_refresh_rate ⇒ Object
Returns the value of attribute aquire_lock_refresh_rate.
-
.aquire_lock_sc ⇒ Object
Returns the value of attribute aquire_lock_sc.
-
.attempt_lockd_recovery ⇒ Object
Returns the value of attribute attempt_lockd_recovery.
-
.lockd_recover_wait ⇒ Object
Returns the value of attribute lockd_recover_wait.
-
.sql_debug ⇒ Object
–{{{.
-
.transaction_retries ⇒ Object
Returns the value of attribute transaction_retries.
-
.transaction_retries_sc ⇒ Object
Returns the value of attribute transaction_retries_sc.
Instance Attribute Summary collapse
-
#aquire_lock_lockfile_stale_age ⇒ Object
Returns the value of attribute aquire_lock_lockfile_stale_age.
-
#aquire_lock_refresh_rate ⇒ Object
Returns the value of attribute aquire_lock_refresh_rate.
-
#aquire_lock_sc ⇒ Object
Returns the value of attribute aquire_lock_sc.
-
#attempt_lockd_recovery ⇒ Object
Returns the value of attribute attempt_lockd_recovery.
-
#dirname ⇒ Object
readonly
Returns the value of attribute dirname.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#lockd_recover_wait ⇒ Object
Returns the value of attribute lockd_recover_wait.
-
#lockfile ⇒ Object
readonly
Returns the value of attribute lockfile.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#sql_debug ⇒ Object
Returns the value of attribute sql_debug.
-
#transaction_retries ⇒ Object
Returns the value of attribute transaction_retries.
-
#transaction_retries_sc ⇒ Object
Returns the value of attribute transaction_retries_sc.
Class Method Summary collapse
-
.create(path, opts = {}) ⇒ Object
–}}}.
-
.create_schema(path) ⇒ Object
–}}}.
- .fields ⇒ Object
-
.h2t(h) ⇒ Object
–}}}.
-
.integrity_check(dbpath) ⇒ Object
–}}}.
-
.q(tuple) ⇒ Object
–}}}.
-
.t2h(tuple) ⇒ Object
–}}}.
-
.tuple ⇒ Object
–}}}.
Instance Method Summary collapse
-
#abort_transaction(*a) ⇒ Object
–}}}.
-
#aquire_lock(opts = {}) ⇒ Object
–}}}.
-
#connect ⇒ Object
–}}}.
-
#execute(sql, &block) ⇒ Object
–}}}.
-
#initialize(path, opts = {}) ⇒ QDB
constructor
A new instance of QDB.
-
#integrity_check(path = @path) ⇒ Object
–}}}.
-
#lock(opts = {}) ⇒ Object
(also: #write_lock)
–}}}.
-
#lockd_recover ⇒ Object
–}}}.
- #lockd_recover_wrap(opts = {}) ⇒ Object
- #read_lock(opts = {}, &block) ⇒ Object (also: #rlock)
-
#recover! ⇒ Object
–}}}.
-
#retry_if_locked ⇒ Object
TODO - add sleep cycle if this ends up getting used.
-
#ro_transaction(opts = {}, &block) ⇒ Object
–}}}.
-
#rollback_transaction(*a) ⇒ Object
–}}}.
-
#sillyclean(opts = {}) ⇒ Object
–}}}.
-
#transaction(opts = {}) ⇒ Object
–}}}.
-
#transaction_wrap(opts = {}) ⇒ Object
simply retry.
-
#vacuum ⇒ Object
–}}}.
Methods included from Logging
Methods included from Logging::LogMethods
#debug, #error, #fatal, #info, #logerr, #logger, #logger=, #warn
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
Constructor Details
#initialize(path, opts = {}) ⇒ QDB
Returns a new instance of QDB.
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 247 248 249 250 251 252 253 254 255 |
# File 'lib/rq/qdb.rb', line 185 def initialize path, opts = {} #--{{{ @path = path @opts = opts @logger = Util::getopt('logger', @opts) || klass.logger || DEFAULT_LOGGER @sql_debug = Util::getopt('sql_debug', @opts) || klass.sql_debug || ENV['RQ_SQL_DEBUG'] || DEFAULT_SQL_DEBUG @transaction_retries = Util::getopt('transaction_retries', @opts) || klass.transaction_retries || DEFAULT_TRANSACTION_RETRIES @aquire_lock_sc = Util::getopt('aquire_lock_sc', @opts) || klass.aquire_lock_sc || DEFAULT_AQUIRE_LOCK_SC @transaction_retries_sc = Util::getopt('transaction_retries_sc', @opts) || klass.transaction_retries_sc || DEFAULT_TRANSACTION_RETRIES_SC @attempt_lockd_recovery = Util::getopt('attempt_lockd_recovery', @opts) || klass.attempt_lockd_recovery || DEFAULT_ATTEMPT_LOCKD_RECOVERY @lockd_recover_wait = Util::getopt('lockd_recover_wait', @opts) || klass.lockd_recover_wait || DEFAULT_LOCKD_RECOVER_WAIT @aquire_lock_lockfile_stale_age = Util::getopt('aquire_lock_lockfile_stale_age', @opts) || klass.aquire_lock_lockfile_stale_age || DEFAULT_AQUIRE_LOCK_LOCKFILE_STALE_AGE @aquire_lock_refresh_rate = Util::getopt('aquire_lock_refresh_rate', @opts) || klass.aquire_lock_refresh_rate || DEFAULT_AQUIRE_LOCK_REFRESH_RATE @schema = "#{ @path }.schema" @dirname = File::dirname(path).gsub(%r|/+\s*$|,'') @basename = File::basename(path) @waiting_w = File::join(@dirname, "#{ Util::hostname }.#{ $$ }.waiting.w") @waiting_r = File::join(@dirname, "#{ Util::hostname }.#{ $$ }.waiting.r") @lock_w = File::join(@dirname, "#{ Util::hostname }.#{ $$ }.lock.w") @lock_r = File::join(@dirname, "#{ Util::hostname }.#{ $$ }.lock.r") @lockfile = File::join(@dirname, 'lock') @lockf = Lockfile::new("#{ @path }.lock") @fields = FIELDS @in_transaction = false @in_ro_transaction = false @db = nil @lockd_recover = "#{ @dirname }.lockd_recover" @lockd_recover_lockf = Lockfile::new "#{ @lockd_recover }.lock" @lockd_recovered = false #--}}} end |
Class Attribute Details
.aquire_lock_lockfile_stale_age ⇒ Object
Returns the value of attribute aquire_lock_lockfile_stale_age.
81 82 83 |
# File 'lib/rq/qdb.rb', line 81 def aquire_lock_lockfile_stale_age @aquire_lock_lockfile_stale_age end |
.aquire_lock_refresh_rate ⇒ Object
Returns the value of attribute aquire_lock_refresh_rate.
82 83 84 |
# File 'lib/rq/qdb.rb', line 82 def aquire_lock_refresh_rate @aquire_lock_refresh_rate end |
.aquire_lock_sc ⇒ Object
Returns the value of attribute aquire_lock_sc.
77 78 79 |
# File 'lib/rq/qdb.rb', line 77 def aquire_lock_sc @aquire_lock_sc end |
.attempt_lockd_recovery ⇒ Object
Returns the value of attribute attempt_lockd_recovery.
79 80 81 |
# File 'lib/rq/qdb.rb', line 79 def attempt_lockd_recovery @attempt_lockd_recovery end |
.lockd_recover_wait ⇒ Object
Returns the value of attribute lockd_recover_wait.
80 81 82 |
# File 'lib/rq/qdb.rb', line 80 def lockd_recover_wait @lockd_recover_wait end |
.sql_debug ⇒ Object
–{{{
75 76 77 |
# File 'lib/rq/qdb.rb', line 75 def sql_debug @sql_debug end |
.transaction_retries ⇒ Object
Returns the value of attribute transaction_retries.
76 77 78 |
# File 'lib/rq/qdb.rb', line 76 def transaction_retries @transaction_retries end |
.transaction_retries_sc ⇒ Object
Returns the value of attribute transaction_retries_sc.
78 79 80 |
# File 'lib/rq/qdb.rb', line 78 def transaction_retries_sc @transaction_retries_sc end |
Instance Attribute Details
#aquire_lock_lockfile_stale_age ⇒ Object
Returns the value of attribute aquire_lock_lockfile_stale_age.
181 182 183 |
# File 'lib/rq/qdb.rb', line 181 def aquire_lock_lockfile_stale_age @aquire_lock_lockfile_stale_age end |
#aquire_lock_refresh_rate ⇒ Object
Returns the value of attribute aquire_lock_refresh_rate.
182 183 184 |
# File 'lib/rq/qdb.rb', line 182 def aquire_lock_refresh_rate @aquire_lock_refresh_rate end |
#aquire_lock_sc ⇒ Object
Returns the value of attribute aquire_lock_sc.
177 178 179 |
# File 'lib/rq/qdb.rb', line 177 def aquire_lock_sc @aquire_lock_sc end |
#attempt_lockd_recovery ⇒ Object
Returns the value of attribute attempt_lockd_recovery.
179 180 181 |
# File 'lib/rq/qdb.rb', line 179 def attempt_lockd_recovery @attempt_lockd_recovery end |
#dirname ⇒ Object (readonly)
Returns the value of attribute dirname.
170 171 172 |
# File 'lib/rq/qdb.rb', line 170 def dirname @dirname end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
172 173 174 |
# File 'lib/rq/qdb.rb', line 172 def fields @fields end |
#lockd_recover_wait ⇒ Object
Returns the value of attribute lockd_recover_wait.
180 181 182 |
# File 'lib/rq/qdb.rb', line 180 def lockd_recover_wait @lockd_recover_wait end |
#lockfile ⇒ Object (readonly)
Returns the value of attribute lockfile.
174 175 176 |
# File 'lib/rq/qdb.rb', line 174 def lockfile @lockfile end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
173 174 175 |
# File 'lib/rq/qdb.rb', line 173 def mutex @mutex end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
169 170 171 |
# File 'lib/rq/qdb.rb', line 169 def opts @opts end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
168 169 170 |
# File 'lib/rq/qdb.rb', line 168 def path @path end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
171 172 173 |
# File 'lib/rq/qdb.rb', line 171 def schema @schema end |
#sql_debug ⇒ Object
Returns the value of attribute sql_debug.
175 176 177 |
# File 'lib/rq/qdb.rb', line 175 def sql_debug @sql_debug end |
#transaction_retries ⇒ Object
Returns the value of attribute transaction_retries.
176 177 178 |
# File 'lib/rq/qdb.rb', line 176 def transaction_retries @transaction_retries end |
#transaction_retries_sc ⇒ Object
Returns the value of attribute transaction_retries_sc.
178 179 180 |
# File 'lib/rq/qdb.rb', line 178 def transaction_retries_sc @transaction_retries_sc end |
Class Method Details
.create(path, opts = {}) ⇒ Object
–}}}
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/rq/qdb.rb', line 143 def create path, opts = {} #--{{{ qdb = new path, opts FileUtils::touch qdb.lockfile create_schema qdb.schema qdb.transaction do qdb.execute PRAGMAS qdb.execute SCHEMA end qdb #--}}} end |
.create_schema(path) ⇒ Object
–}}}
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/rq/qdb.rb', line 155 def create_schema path #--{{{ tmp = "#{ path }.tmp" open(tmp,'w') do |f| f.puts PRAGMAS f.puts SCHEMA end FileUtils::mv tmp, path #--}}} end |
.fields ⇒ Object
84 85 86 87 88 |
# File 'lib/rq/qdb.rb', line 84 def fields #--{{{ FIELDS #--}}} end |
.h2t(h) ⇒ Object
–}}}
118 119 120 121 122 123 124 |
# File 'lib/rq/qdb.rb', line 118 def h2t h #--{{{ t = tuple FIELDS.each{|f| t[f] = h[f]} t #--}}} end |
.integrity_check(dbpath) ⇒ Object
–}}}
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rq/qdb.rb', line 89 def integrity_check dbpath #--{{{ ret = false tuple = nil begin db = begin SQLite::Database::new dbpath, 0 rescue SQLite::Database::new dbpath end opened = true db.use_array = true rescue nil tuple = db.execute 'PRAGMA integrity_check;' ret = (tuple and tuple.first and (tuple.first["integrity_check"] =~ /^\s*ok\s*$/io)) ensure db.close if opened db = nil end ret #--}}} end |
.q(tuple) ⇒ Object
–}}}
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rq/qdb.rb', line 132 def q tuple #--{{{ [ tuple ].flatten.map do |f| if f and not f.to_s.empty? "'" << Util.escape(f,"'","'") << "'" else 'NULL' end end #--}}} end |
.t2h(tuple) ⇒ Object
–}}}
111 112 113 114 115 116 117 |
# File 'lib/rq/qdb.rb', line 111 def t2h tuple #--{{{ h = {} FIELDS.each_with_index{|f,i| h[f] = tuple[i]} h #--}}} end |
Instance Method Details
#abort_transaction(*a) ⇒ Object
–}}}
410 411 412 413 414 |
# File 'lib/rq/qdb.rb', line 410 def abort_transaction(*a) #--{{{ raise AbortedTransactionError, *a #--}}} end |
#aquire_lock(opts = {}) ⇒ Object
–}}}
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 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/rq/qdb.rb', line 437 def aquire_lock opts = {} #--{{{ ro = Util::getopt 'read_only', opts ret = nil @aquire_lock_sc.reset waiting, ltype, lfile = if ro [@waiting_r, File::LOCK_SH | File::LOCK_NB, @lock_r] else [@waiting_w, File::LOCK_EX | File::LOCK_NB, @lock_w] end ltype_s = (ltype == File::LOCK_EX ? 'write' : 'read') ltype ||= File::LOCK_NB aquired = false until aquired begin debug{ "aquiring lock" } #@lockf.lock unless ro open(@lockfile, 'a+') do |lf| locked = false refresher = nil sc = nil begin FileUtils::touch waiting # poll 42.times do locked = lf.posixlock(ltype | File::LOCK_NB) break if locked sleep rand end if locked aquired = true refresher = Refresher::new @lockfile, @aquire_lock_refresh_rate debug{ "refresher pid <#{ refresher.pid }> refresh_rate <#{ @aquire_lock_refresh_rate }>" } FileUtils::rm_f waiting rescue nil FileUtils::touch lfile rescue nil debug{ "aquired lock" } ret = yield debug{ "released lock" } else aquired = false stat = File::stat @lockfile mtime = stat.mtime stale = mtime < (Time::now - @aquire_lock_lockfile_stale_age) if stale Util::uncache @lockfile rescue nil stat = File::stat @lockfile mtime = stat.mtime stale = mtime < (Time::now - @aquire_lock_lockfile_stale_age) if stale warn{ "detected stale lockfile of mtime <#{ mtime }>" } lockd_recover if @attempt_lockd_recovery end end sc = @aquire_lock_sc.next debug{ "failed to aquire lock - sleep(#{ sc })" } sleep sc end ensure if locked unlocked = false begin 42.times do unlocked = lf.posixlock(File::LOCK_UN | File::LOCK_NB) break if unlocked sleep rand end ensure lf.posixlock File::LOCK_UN unless unlocked end end refresher.kill if refresher FileUtils::rm_f waiting rescue nil FileUtils::rm_f lfile rescue nil end end ensure #@lockf.unlock rescue nil unless read_only end end ret #--}}} end |
#connect ⇒ Object
–}}}
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 |
# File 'lib/rq/qdb.rb', line 530 def connect #--{{{ ret = nil opened = nil begin raise 'db has no schema' unless test ?e, @schema debug{"connecting to db <#{ @path }>..."} $db = @db = begin SQLite::Database::new(@path, 0) rescue SQLite::Database::new(@path) end debug{"connected."} opened = true @db.use_array = true rescue nil ret = yield @db ensure @db.close if opened $db = @db = nil debug{"disconnected from db <#{ @path }>"} end ret #--}}} end |
#execute(sql, &block) ⇒ Object
–}}}
555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'lib/rq/qdb.rb', line 555 def execute sql, &block #--{{{ raise 'not in transaction' unless @in_transaction if @sql_debug logger << "SQL:\n#{ sql }\n" end #ret = retry_if_locked{ @db.execute sql, &block } ret = @db.execute sql, &block if @sql_debug and ret and Array === ret and ret.first logger << "RESULT:\n#{ ret.first.inspect }\n...\n" end ret #--}}} end |
#integrity_check(path = @path) ⇒ Object
–}}}
702 703 704 705 706 707 |
# File 'lib/rq/qdb.rb', line 702 def integrity_check path = @path #--{{{ debug{ "running integrity_check on <#{ path }>" } klass.integrity_check(path) #--}}} end |
#lock(opts = {}) ⇒ Object Also known as: write_lock
–}}}
708 709 710 711 712 713 714 715 716 717 718 |
# File 'lib/rq/qdb.rb', line 708 def lock opts = {} #--{{{ ret = nil lockd_recover_wrap do aquire_lock(opts) do ret = yield end end ret #--}}} end |
#lockd_recover ⇒ Object
–}}}
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 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 |
# File 'lib/rq/qdb.rb', line 611 def lockd_recover #--{{{ return nil unless @attempt_lockd_recovery warn{ "attempting lockd recovery" } time = Time::now ret = nil @lockd_recover_lockf.lock do Util::uncache @dirname rescue nil Util::uncache @path rescue nil Util::uncache @lockfile rescue nil Util::uncache @lockd_recover rescue nil mtime = File::stat(@lockd_recover).mtime rescue time if mtime > time warn{ "skipping lockd recovery (another node has already recovered)" } ret = true else moved = false begin FileUtils::touch @lockd_recover @lockd_recovered = false begin report = <<-msg hostname : #{ Util::hostname } pid : #{ Process.pid } time : #{ Time::now } q : path : #{ @dirname } stat : #{ File::stat(@dirname).inspect } db : path : #{ @path } stat : #{ File::stat(@path).inspect } lockfile : path : #{ @lockfile } stat : #{ File::stat(@lockfile).inspect } msg info{ "LOCKD RECOVERY REPORT" } logger << report cmd = "mail -s LOCKD_RECOVERY [email protected] <<eof\n#{ report }\neof" Util::system cmd rescue nil end warn{ "sleeping #{ @lockd_recover_wait }s before continuing..." } sleep @lockd_recover_wait tmp = "#{ @dirname }.tmp" FileUtils::rm_rf tmp FileUtils::mv @dirname, tmp moved = true rfiles = [@path, @lockfile].map{|f| File::join(tmp,File::basename(f))} rfiles.each do |f| ftmp = "#{ f }.tmp" FileUtils::rm_rf ftmp FileUtils::cp f, ftmp FileUtils::rm f FileUtils::mv ftmp, f end dbtmp = File::join(tmp,File::basename(@path)) if integrity_check(dbtmp) FileUtils::mv tmp, @dirname FileUtils::cp @lockd_recover_lockf.path, @lockd_recover @lockd_recovered = true Util::uncache @dirname rescue nil Util::uncache @path rescue nil Util::uncache @lockfile rescue nil Util::uncache @lockd_recover rescue nil warn{ "lockd recovery complete" } else FileUtils::mv tmp, @dirname @lockd_recovered = false error{ "lockd recovery failed" } end ret = @lockd_recovered ensure if moved and not @lockd_recovered and tmp and test(?d, tmp) FileUtils::mv tmp, @dirname end end end end ret #--}}} end |
#lockd_recover_wrap(opts = {}) ⇒ Object
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 |
# File 'lib/rq/qdb.rb', line 327 def lockd_recover_wrap opts = {} #--{{{ ret = nil try_again = false begin begin @lockd_recovered = false old_mtime = begin Util::uncache @lockd_recover rescue nil File::stat(@lockd_recover).mtime rescue Time::now end ret = yield ensure new_mtime = begin Util::uncache @lockd_recover rescue nil File::stat(@lockd_recover).mtime rescue old_mtime end if new_mtime and old_mtime and new_mtime > old_mtime and not @lockd_recovered try_again = true end end rescue if try_again warn{ "a remote lockd recovery has invalidated this transaction!" } warn{ "retrying..."} sleep 120 retry else raise end end ret #--}}} end |
#read_lock(opts = {}, &block) ⇒ Object Also known as: rlock
721 722 723 724 725 726 |
# File 'lib/rq/qdb.rb', line 721 def read_lock(opts = {}, &block) #--{{{ opts['read_only'] = true lock opts, &block #--}}} end |
#recover! ⇒ Object
–}}}
597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'lib/rq/qdb.rb', line 597 def recover! #--{{{ raise 'nested transaction' if @in_transaction begin @in_transaction = true connect{ execute 'vacuum' } require 'timeout' Timeout::timeout(60){ system "sqlite #{ @path } .tables >/dev/null 2>&1" } ensure @in_transaction = false end integrity_check #--}}} end |
#retry_if_locked ⇒ Object
TODO - add sleep cycle if this ends up getting used
572 573 574 575 576 577 578 579 580 581 582 583 584 |
# File 'lib/rq/qdb.rb', line 572 def retry_if_locked #--{{{ ret = nil begin ret = yield rescue SQLite::BusyException warn{ "database locked - waiting(1.0) and retrying" } sleep 1.0 retry end ret #--}}} end |
#ro_transaction(opts = {}, &block) ⇒ Object
–}}}
256 257 258 259 260 261 |
# File 'lib/rq/qdb.rb', line 256 def ro_transaction(opts = {}, &block) #--{{{ opts['read_only'] = true transaction(opts, &block) #--}}} end |
#rollback_transaction(*a) ⇒ Object
–}}}
415 416 417 418 419 |
# File 'lib/rq/qdb.rb', line 415 def rollback_transaction(*a) #--{{{ raise RollbackTransactionError, *a #--}}} end |
#sillyclean(opts = {}) ⇒ Object
–}}}
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/rq/qdb.rb', line 420 def sillyclean opts = {} #--{{{ ro = Util::getopt 'read_only', opts ret = nil if ro ret = yield else glob = File::join @dirname,'.nfs*' orgsilly = Dir[glob] ret = yield newsilly = Dir[glob] silly = newsilly - orgsilly silly.each{|path| FileUtils::rm_rf path} end ret #--}}} end |
#transaction(opts = {}) ⇒ Object
–}}}
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/qdb.rb', line 262 def transaction opts = {} #--{{{ raise 'nested transaction' if @in_transaction ro = Util::getopt 'read_only', opts ret = nil begin @in_transaction = true lockd_recover_wrap(opts) do transaction_wrap(opts) do aquire_lock(opts) do #sillyclean(opts) do connect do execute 'begin' unless ro ret = yield execute 'commit' unless ro end #end end end end ensure @in_transaction = false end ret #--}}} end |
#transaction_wrap(opts = {}) ⇒ Object
simply retry.
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 |
# File 'lib/rq/qdb.rb', line 373 def transaction_wrap opts = {} #--{{{ ro = Util::getopt 'read_only', opts ret = nil if ro ret = yield else errors = [] @transaction_retries_sc.reset begin ret = yield rescue => e #rescue SQLite::DatabaseException, SQLite::SQLException, SystemCallError => e case e when AbortedTransactionError raise when RollbackTransactionError raise else if @transaction_retries == 0 raise elsif errors.size >= @transaction_retries error{ "MAXIMUM TRANSACTION RETRIES SURPASSED" } raise else warn{ e } if(errors.empty? or not Util::erreq(errors[-1], e)) errors << e warn{ "retry <#{ errors.size }>..." } end sleep @transaction_retries_sc.next retry end end end ret #--}}} end |
#vacuum ⇒ Object
–}}}
585 586 587 588 589 590 591 592 593 594 595 596 |
# File 'lib/rq/qdb.rb', line 585 def vacuum #--{{{ raise 'nested transaction' if @in_transaction begin @in_transaction = true connect{ execute 'vacuum' } ensure @in_transaction = false end self #--}}} end |