Class: Kithe::Indexable::ThreadSettings

Inherits:
Object
  • Object
show all
Defined in:
app/indexing/kithe/indexable/thread_settings.rb

Overview

An object that is stored in Thread.current to represent current indexing/writing settings, used to implement Kithe::Indexable.index_with

The public API is that Kithe::Indexable.index_with calls:

  • ThreadSettings.push(settings) to register current settings

  • ThreadSettings.current.pop at end of block to un-register them

Then code in Kithe::Indexable can check ‘ThreadSettings.current` to get the current ThreadSettings. It returns a “null object” representing no settings if there are none, so calling code can do things like:

ThreadSettings.current.disabled_callbacks?

and

ThreadSettings.current.writer

without worrying about if there are current settings.

Defined Under Namespace

Classes: NullSettings

Constant Summary collapse

THREAD_CURRENT_KEY =
:kithe_indexable_current_writer_settings

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(batching:, disable_callbacks:, original_settings:, writer:, on_finish:) ⇒ ThreadSettings

Ordinarily you will not use this directly, it’s called by .push. But param definitions are here.

Parameters:

  • batching (Boolean)

    if true, set up a batching writer. Incompatible with other writer-related settings.

  • disable_callbacks (Boolean)

    if true, automatic after_commit callbacks are currently disabled.

  • original_settings (ThreadSettings)

    when .push passes this in, so it can be restored on .pop

  • writer (Traject::Writer)

    a writer to be used as current default for indexing. May be set up with unusual settings, or even be an unusual writer class.

  • on_finish (Proc)

    proc object which will be called on .pop (normally at end of index_with block). It will be passed the operative Traject::Writer for that block.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/indexing/kithe/indexable/thread_settings.rb', line 57

def initialize(batching:, disable_callbacks:, original_settings:,
  writer:, on_finish:)
  @original_settings = original_settings
  @batching = batching
  @disable_callbacks = disable_callbacks
  @on_finish = on_finish

  @writer = writer

  if @batching && @writer
    raise ArgumentError.new("either `batching:true` convenience, or `writer:` specified, you can't do both")
  end

  @local_writer = false
end

Class Method Details

.currentObject

Returns a ThreadSettings currently stored in Thread.current, or else A Null object (Null Object Pattern) representings no settings.



36
37
38
# File 'app/indexing/kithe/indexable/thread_settings.rb', line 36

def self.current
  Thread.current[THREAD_CURRENT_KEY] || NullSettings.new
end

.push(**kwargs) ⇒ Object

Parameters:

  • batching (Boolean)

    if true, set up a batching writer. Incompatible with other writer-related settings.

  • disable_callbacks (Boolean)

    if true, automatic after_commit callbacks are currently disabled.

  • original_settings (ThreadSettings)

    when .push passes this in, so it can be restored on .pop

  • writer (Traject::Writer)

    a writer to be used as current default for indexing. May be set up with unusual settings, or even be an unusual writer class.

  • on_finish (Proc)

    proc object which will be called on .pop (normally at end of index_with block). It will be passed the operative Traject::Writer for that block.



26
27
28
29
30
31
32
# File 'app/indexing/kithe/indexable/thread_settings.rb', line 26

def self.push(**kwargs)
  original = Thread.current[THREAD_CURRENT_KEY]
  instance = new(**kwargs.merge(original_settings: original))
  Thread.current[THREAD_CURRENT_KEY] = instance

  instance
end

Instance Method Details

#disabled_callbacks?Boolean

Are automatic after_commit callbacks currently disabled?

Returns:

  • (Boolean)


91
92
93
# File 'app/indexing/kithe/indexable/thread_settings.rb', line 91

def disabled_callbacks?
  @disable_callbacks
end

#popObject

Remove this object from Thread.current, replacing with any previous current settings.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/indexing/kithe/indexable/thread_settings.rb', line 97

def pop
  # only call on-finish if we have a writer, batch writers are lazily
  # created and maybe we never created one
  if @writer
    # if we created the writer ourselves locally and nobody
    # specified an on_finish, close our locally-created writer.
    on_finish = if @local_writer && @on_finish.nil?
      proc {|writer| writer.close }
    else
      @on_finish
    end
    on_finish.call(@writer) if on_finish
  end

  Thread.current[THREAD_CURRENT_KEY] = @original_settings
end

#writerObject

Is there a writer configured for current settings? If so, return it. May return nil.

In case of ‘batching:true`, the batching writer will be lazily created on first time #writer is asked for.



80
81
82
83
84
85
86
87
88
# File 'app/indexing/kithe/indexable/thread_settings.rb', line 80

def writer
  @writer ||= begin
    if @batching
      batch_size = (@batching == true) ? Kithe.indexable_settings.batching_mode_batch_size : @batching
      @local_writer = true
      Kithe.indexable_settings.writer_instance!("solr_writer.batch_size" => batch_size)
    end
  end
end