Class: FlyingSphinx::DelayedDelta

Inherits:
ThinkingSphinx::Deltas::DefaultDelta
  • Object
show all
Defined in:
lib/flying_sphinx/delayed_delta.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.duplicates_exist?(object) ⇒ Boolean

Checks whether a given job already exists in the queue.

Parameters:

  • object (Object)

    The job

Returns:

  • (Boolean)

    True if a duplicate of the job already exists in the queue



31
32
33
34
35
36
37
38
# File 'lib/flying_sphinx/delayed_delta.rb', line 31

def self.duplicates_exist?(object)
  ::Delayed::Job.count(
    :conditions => {
      :handler    => object.to_yaml,
      :locked_at  => nil
    }
  ) > 0
end

.enqueue(object, priority = 0) ⇒ Object

Adds a job to the queue, if it doesn’t already exist. This is to ensure multiple indexing requests for the same delta index don’t get added, as the index only needs to be processed once.

Because indexing jobs are all the same object, they all get serialised to the same YAML value.

Parameters:

  • object (Object)

    The job, which must respond to the #perform method.

  • priority (Integer) (defaults to: 0)

    (0)



12
13
14
15
16
# File 'lib/flying_sphinx/delayed_delta.rb', line 12

def self.enqueue(object, priority = 0)
  return if duplicates_exist? object

  enqueue_without_duplicates_check object, priority
end

.enqueue_without_duplicates_check(object, priority = 0) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/flying_sphinx/delayed_delta.rb', line 18

def self.enqueue_without_duplicates_check(object, priority = 0)
  if defined?(Rails) && Rails.version.to_i <= 2
    ::Delayed::Job.enqueue(object, priority)
  else
    ::Delayed::Job.enqueue(object, :priority => priority)
  end
end

Instance Method Details

#index(model, instance = nil) ⇒ Boolean

Adds a job to the queue for processing the given model’s delta index. A job for hiding the instance in the core index is also created, if an instance is provided.

Neither job will be queued if updates or deltas are disabled, or if the instance (when given) is not toggled to be in the delta index. The first two options are controlled via ThinkingSphinx.updates_enabled? and ThinkingSphinx.deltas_enabled?.

Parameters:

  • model (Class)

    the ActiveRecord model to index.

  • instance (ActiveRecord::Base) (defaults to: nil)

    the instance of the given model that has changed. Optional.

Returns:

  • (Boolean)

    true



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/flying_sphinx/delayed_delta.rb', line 54

def index(model, instance = nil)
  return true if skip? instance

  self.class.enqueue(
    FlyingSphinx::IndexRequest.new(model.delta_index_names),
    delayed_job_priority
  )

  self.class.enqueue_without_duplicates_check(
    FlyingSphinx::FlagAsDeletedJob.new(
      model.core_index_names, instance.sphinx_document_id
    ),
    delayed_job_priority
  ) if instance

  true
end