Class: ThinkingSphinx::Deltas::ResqueDelta

Inherits:
DefaultDelta
  • Object
show all
Defined in:
lib/thinking_sphinx/deltas/resque_delta.rb,
lib/thinking_sphinx/deltas/resque_delta/flag_as_deleted_set.rb

Overview

Delayed Deltas for Thinking Sphinx, using Resque.

This documentation is aimed at those reading the code. If you’re looking for a guide to Thinking Sphinx and/or deltas, I recommend you start with the Thinking Sphinx site instead - or the README for this library at the very least.

See Also:

Author:

  • Patrick Allan

Direct Known Subclasses

FlyingSphinx::ResqueDelta

Defined Under Namespace

Modules: FlagAsDeletedSet, IndexUtils Classes: CoreIndex, DeltaJob, Railtie

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear!Object

Clear both the resque queues and any other state maintained in redis



40
41
42
43
44
# File 'lib/thinking_sphinx/deltas/resque_delta.rb', line 40

def self.clear!
  self.clear_thinking_sphinx_queues

  FlagAsDeletedSet.clear_all!
end

.clear_thinking_sphinx_queuesObject

LTRIM + LPOP deletes all items from the Resque queue without loading it into client memory (unlike Resque.dequeue). WARNING: This will clear ALL jobs in any queue used by a ResqueDelta job. If you’re sharing a queue with other jobs they’ll be deleted!



32
33
34
35
36
37
# File 'lib/thinking_sphinx/deltas/resque_delta.rb', line 32

def self.clear_thinking_sphinx_queues
  job_types.collect { |c| c.instance_variable_get(:@queue) }.uniq.each do |q|
    Resque.redis.ltrim("queue:#{q}", 0, 0)
    Resque.redis.lpop("queue:#{q}")
  end
end

.job_prefixObject



24
25
26
# File 'lib/thinking_sphinx/deltas/resque_delta.rb', line 24

def self.job_prefix
  'ts-delta'
end

.job_typesObject



18
19
20
21
22
# File 'lib/thinking_sphinx/deltas/resque_delta.rb', line 18

def self.job_types
  [
    ThinkingSphinx::Deltas::ResqueDelta::DeltaJob
  ]
end

.lock(index_name) ⇒ Object

Use simplistic locking. We’re assuming that the user won’t run more than one ‘rake ts:si` or `rake ts:in` task at a time.



48
49
50
# File 'lib/thinking_sphinx/deltas/resque_delta.rb', line 48

def self.lock(index_name)
  Resque.redis.set("#{job_prefix}:index:#{index_name}:locked", 'true')
end

.locked?(index_name) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/thinking_sphinx/deltas/resque_delta.rb', line 56

def self.locked?(index_name)
  Resque.redis.get("#{job_prefix}:index:#{index_name}:locked") == 'true'
end

.prepare_for_core_index(index_name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/thinking_sphinx/deltas/resque_delta.rb', line 60

def self.prepare_for_core_index(index_name)
  core = "#{index_name}_core"
  delta = "#{index_name}_delta"

  FlagAsDeletedSet.clear!(core)

  #clear delta jobs
  # dequeue is fast for jobs with arguments
  Resque.dequeue(ThinkingSphinx::Deltas::ResqueDelta::DeltaJob, delta)
end

.unlock(index_name) ⇒ Object



52
53
54
# File 'lib/thinking_sphinx/deltas/resque_delta.rb', line 52

def self.unlock(index_name)
  Resque.redis.del("#{job_prefix}:index:#{index_name}:locked")
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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/thinking_sphinx/deltas/resque_delta.rb', line 85

def index(model, instance = nil)
  return true if skip?(instance)
  model.delta_index_names.each do |delta|
    next if self.class.locked?(delta)
    Resque.enqueue(
      ThinkingSphinx::Deltas::ResqueDelta::DeltaJob,
      delta
    )
  end
  if instance
    model.core_index_names.each do |core|
      FlagAsDeletedSet.add(core, instance.sphinx_document_id)
    end
  end
  true
end