Module: SQLite3::ForkSafety

Defined in:
lib/sqlite3/fork_safety.rb

Overview

based on Rails’s active_support/fork_tracker.rb

Defined Under Namespace

Modules: CoreExt

Class Method Summary collapse

Class Method Details

.discardObject

:nodoc:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sqlite3/fork_safety.rb', line 33

def discard # :nodoc:
  warned = @suppress
  @databases.each do |db|
    next unless db.weakref_alive?

    unless db.closed? || db.readonly?
      unless warned
        # If you are here, you may want to read
        # https://github.com/sparklemotion/sqlite3-ruby/pull/558
        warn("Writable sqlite database connection(s) were inherited from a forked process. " \
             "This is unsafe and the connections are being closed to prevent possible data " \
             "corruption. Please close writable sqlite database connections before forking.",
          uplevel: 0)
        warned = true
      end
      db.close
    end
  end
  @databases.clear
end

.hook!Object

:nodoc:



23
24
25
# File 'lib/sqlite3/fork_safety.rb', line 23

def hook! # :nodoc:
  ::Process.singleton_class.prepend(CoreExt)
end

.suppress_warnings!Object

Call to suppress the fork-related warnings.



55
56
57
# File 'lib/sqlite3/fork_safety.rb', line 55

def suppress_warnings!
  @suppress = true
end

.track(database) ⇒ Object

:nodoc:



27
28
29
30
31
# File 'lib/sqlite3/fork_safety.rb', line 27

def track(database) # :nodoc:
  @mutex.synchronize do
    @databases << WeakRef.new(database)
  end
end