ts-resque-delta 1.2.4 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -1
  3. data/.travis.yml +14 -14
  4. data/Appraisals +4 -6
  5. data/Gemfile +1 -1
  6. data/README.markdown +31 -39
  7. data/Rakefile +6 -13
  8. data/gemfiles/binary.gemfile +7 -0
  9. data/gemfiles/sphinxql.gemfile +7 -0
  10. data/lib/thinking_sphinx/deltas/resque_delta.rb +66 -76
  11. data/lib/thinking_sphinx/deltas/resque_delta/delta_job.rb +1 -113
  12. data/lib/thinking_sphinx/deltas/resque_delta/flag_as_deleted_job.rb +7 -0
  13. data/lib/ts-resque-delta.rb +0 -2
  14. data/spec/acceptance/resque_deltas_spec.rb +50 -0
  15. data/spec/acceptance/spec_helper.rb +4 -0
  16. data/spec/acceptance/support/database_cleaner.rb +11 -0
  17. data/spec/acceptance/support/sphinx_controller.rb +61 -0
  18. data/spec/acceptance/support/sphinx_helpers.rb +36 -0
  19. data/spec/internal/.gitignore +2 -0
  20. data/spec/internal/app/indices/book_index.rb +3 -0
  21. data/spec/internal/app/models/book.rb +7 -0
  22. data/spec/internal/config/database.yml +5 -0
  23. data/spec/internal/db/schema.rb +10 -0
  24. data/spec/internal/log/.gitignore +1 -0
  25. data/spec/spec_helper.rb +16 -12
  26. data/spec/thinking_sphinx/deltas/resque_delta/delta_job_spec.rb +11 -155
  27. data/spec/thinking_sphinx/deltas/resque_delta_spec.rb +3 -166
  28. data/ts-resque-delta.gemspec +19 -28
  29. metadata +90 -230
  30. data/Guardfile +0 -17
  31. data/features/resque_deltas.feature +0 -62
  32. data/features/smart_indexing.feature +0 -43
  33. data/features/step_definitions/common_steps.rb +0 -76
  34. data/features/step_definitions/resque_delta_steps.rb +0 -33
  35. data/features/step_definitions/smart_indexing_steps.rb +0 -3
  36. data/features/support/env.rb +0 -41
  37. data/features/thinking_sphinx/database.example.yml +0 -4
  38. data/features/thinking_sphinx/db/migrations/create_delayed_betas.rb +0 -4
  39. data/features/thinking_sphinx/models/delayed_beta.rb +0 -6
  40. data/gemfiles/activerecord2.gemfile +0 -8
  41. data/gemfiles/activerecord2.gemfile.lock +0 -114
  42. data/gemfiles/activerecord3.gemfile +0 -8
  43. data/gemfiles/activerecord3.gemfile.lock +0 -123
  44. data/lib/flying_sphinx/resque_delta.rb +0 -35
  45. data/lib/flying_sphinx/resque_delta/delta_job.rb +0 -14
  46. data/lib/flying_sphinx/resque_delta/flag_as_deleted_job.rb +0 -7
  47. data/lib/thinking_sphinx/deltas/resque_delta/core_index.rb +0 -116
  48. data/lib/thinking_sphinx/deltas/resque_delta/flag_as_deleted_set.rb +0 -59
  49. data/lib/thinking_sphinx/deltas/resque_delta/index_utils.rb +0 -47
  50. data/lib/thinking_sphinx/deltas/resque_delta/railtie.rb +0 -8
  51. data/lib/thinking_sphinx/deltas/resque_delta/tasks.rb +0 -38
  52. data/lib/thinking_sphinx/deltas/resque_delta/version.rb +0 -7
  53. data/spec/flying_sphinx/resque_delta/delta_job_spec.rb +0 -32
  54. data/spec/flying_sphinx/resque_delta/flag_as_deleted_job_spec.rb +0 -23
  55. data/spec/flying_sphinx/resque_delta_spec.rb +0 -130
  56. data/spec/thinking_sphinx/deltas/resque_delta/core_index_spec.rb +0 -210
  57. data/spec/thinking_sphinx/deltas/resque_delta/flag_as_deleted_set_spec.rb +0 -126
  58. data/spec/thinking_sphinx/deltas/resque_delta/index_utils_spec.rb +0 -67
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2584321ee1b15d855877ef9fb3dc0b4d0719c21e
4
+ data.tar.gz: fc74627b7a3265a8cafe125ab59585c8de2e2a0c
5
+ SHA512:
6
+ metadata.gz: 7380a6e12204a7e6ea243c6c2f6726f637e7125b37b1d09fb88ab93169e78820127b1d28dc07bcff23e0b4199f117a1ef0a147664c2fc64188c026884d1c0dff
7
+ data.tar.gz: a00d91e7bddb444c493fc4fb7ae74167b0b7af598e9d78f5a8790aafd53cfcc730439b81e99520658c1f657eccd015acce30bb6e0ce7b931b793f529f170ef33
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.gem
2
+ *.lock
2
3
  .bundle
3
4
  pkg/*
4
5
  *~
@@ -16,4 +17,3 @@ tmp
16
17
  /bin
17
18
  .rvmrc
18
19
  .rbenv-version
19
- Gemfile.lock
@@ -1,18 +1,18 @@
1
1
  language: ruby
2
- before_install: gem update --system
3
- before_script:
4
- - mysql -e 'create database thinking_sphinx;'
5
2
  rvm:
6
- - 1.8.7
7
- - ree
8
- - 1.9.2
9
3
  - 1.9.3
4
+ before_install:
5
+ - gem update --system
6
+ - gem install bundler --no-ri --no-rdoc
7
+ - bundle install
8
+ before_script:
9
+ - "mysql -e 'create database thinking_sphinx;' > /dev/null"
10
+ - "psql -c 'create database thinking_sphinx;' -U postgres > /dev/null"
11
+ services:
12
+ - redis-server
13
+ env:
14
+ - SPHINX_BIN=/usr/local/sphinx-2.0.9/bin/ SPHINX_VERSION=2.0.9 DATABASE=mysql2
15
+ - SPHINX_BIN=/usr/local/sphinx-2.0.9/bin/ SPHINX_VERSION=2.0.9 DATABASE=postgresql
10
16
  gemfile:
11
- - gemfiles/activerecord2.gemfile
12
- - gemfiles/activerecord3.gemfile
13
- matrix:
14
- exclude:
15
- - rvm: 1.9.2
16
- gemfile: gemfiles/activerecord2.gemfile
17
- - rvm: 1.9.3
18
- gemfile: gemfiles/activerecord2.gemfile
17
+ - gemfiles/binary.gemfile
18
+ - gemfiles/sphinxql.gemfile
data/Appraisals CHANGED
@@ -1,9 +1,7 @@
1
- appraise "activerecord2" do
2
- gem "activerecord", "~> 2.3.14"
3
- gem "mysql2", "~> 0.2.7"
1
+ appraise 'binary' do
2
+ gem 'thinking-sphinx', '~> 2.1.0'
4
3
  end
5
4
 
6
- appraise "activerecord3" do
7
- gem "activerecord", "~> 3.2.9"
8
- gem "mysql2", "~> 0.3.11"
5
+ appraise 'sphinxql' do
6
+ gem 'thinking-sphinx', '~> 3.0.3'
9
7
  end
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in ts-resque-delta.gemspec
4
4
  gemspec
@@ -7,67 +7,56 @@ Delayed Deltas for Thinking Sphinx (with Resque)
7
7
 
8
8
  Installation
9
9
  ------------
10
- This gem depends on the following gems: _thinking-sphinx_, _resque_, and
11
- _resque-lock-timeout_.
12
10
 
13
- gem install ts-resque-delta
11
+ This gem depends on the following gems: `thinking-sphinx` and `resque`.
14
12
 
15
- Add _ts-resque-delta_ to your **Gemfile** file, with the rest of your gem
16
- dependencies:
13
+ Currently, you'll need Thinking Sphinx v1.5.0 (for Rails 2), v2.1.0
14
+ (for Rails 3), or - ideally - v3.0.3 or newer (for Rails 3.1 onwards). If you're
15
+ on a version of Thinking Sphinx that's too old, you better go upgrade - but
16
+ otherwise, add `ts-resque-delta` to your `Gemfile` file with the rest of your
17
+ gem dependencies:
17
18
 
18
- gem 'ts-resque-delta', '1.1.1'
19
+ gem 'ts-resque-delta', '~> 2.0.0'
19
20
 
20
- If you're using Rails 3, the rake tasks will automatically be loaded by Rails.
21
- If you're using Rails 2, add the following line to your **Rakefile**:
21
+ Add the delta property to index definition. If you're using Thinking Sphinx v3,
22
+ then it'll look something like this:
22
23
 
23
- require 'thinking_sphinx/deltas/resque_delta/tasks'
24
+ ThinkingSphinx::Index.define(:article,
25
+ :with => :active_record,
26
+ :delta => ThinkingSphinx::Deltas::ResqueDelta
27
+ ) do
28
+ # fields and attributes and so on...
29
+ end
24
30
 
25
- Add the delta property to each `define_index` block:
31
+ But if you're still using v1.5 or v2.1, you'll want the following:
26
32
 
27
33
  define_index do
28
- # ...
29
- set_property :delta => ThinkingSphinx::Deltas::ResqueDelta
30
- end
34
+ # fields and attributes and so on...
31
35
 
32
- If you've never used delta indexes before, you'll want to add the boolean
33
- column named `:delta` to each model's table (note, you must set the `:default`
34
- value to `true`):
35
-
36
- def self.up
37
- add_column :foos, :delta, :boolean, :default => true, :null => false
36
+ set_property :delta => ThinkingSphinx::Deltas::ResqueDelta
38
37
  end
39
38
 
40
- Also, I highly recommend adding a MySQL index to the table of any model using
41
- delta indexes. The Sphinx indexer uses `WHERE table.delta = 1` whenever the
42
- delta indexer runs and `... = 0` whenever the normal indexer runs. Having the
43
- MySQL index on the delta column will generally be a win:
39
+ If you've never used delta indexes before, you'll need to add the boolean
40
+ column named `:delta` to each table for indexed models. A database index for
41
+ that column is also recommended.
44
42
 
45
- def self.up
46
- # ...
47
- add_index :foos, :delta
43
+ def change
44
+ add_column :articles, :delta, :boolean, :default => true, :null => false
45
+ add_index :articles, :delta
48
46
  end
49
47
 
50
48
  Usage
51
49
  -----
50
+
52
51
  Once you've got it all set up, all you need to do is make sure that the Resque
53
- worker is running. You can do this by specifying the `:ts_delta` queue when
52
+ worker is running. You can do this by specifying the `ts_delta` queue when
54
53
  running Resque:
55
54
 
56
55
  QUEUE=ts_delta,other_queues rake resque:work
57
56
 
58
- Additionally, ts-resque-delta will wrap thinking-sphinx's
59
- `thinking_sphinx:index` and `thinking_sphinx:reindex` tasks with
60
- `thinking_sphinx:lock_deltas` and `thinking_sphinx:unlock_deltas`. This will
61
- prevent the delta indexer from running at the same time as the main indexer.
62
-
63
- Finally, ts-resque-delta also provides a rake task called
64
- `thinking_sphinx:smart_index` (or `ts:si` for short). This task, instead of
65
- locking all the delta indexes at once while the main indexer runs, will lock
66
- each delta index independently and sequentially. Thay way, your delta indexer
67
- can run while the main indexer is processing large core indexes.
68
-
69
57
  Contributors (for ts-resque-delta)
70
58
  -----------------------------------
59
+
71
60
  * [Aaron Gibralter](https://github.com/agibralter)
72
61
  * [Ryan Schlesinger](https://github.com/ryansch) (Locking/`smart_index`)
73
62
  * [Pat Allan](https://github.com/freelancing-god) (FlyingSphinx support)
@@ -75,6 +64,7 @@ Contributors (for ts-resque-delta)
75
64
 
76
65
  Original Contributors (for ts-delayed-delta)
77
66
  --------------------------------------------
67
+
78
68
  * [Pat Allan](https://github.com/freelancing-god)
79
69
  * [Ryan Schlesinger](https://github.com/ryansch) (Allowing installs as a plugin)
80
70
  * [Maximilian Schulz](https://max.jungeelite.de) (Ensuring compatibility with Bundler)
@@ -83,4 +73,6 @@ Original Contributors (for ts-delayed-delta)
83
73
 
84
74
  Copyright
85
75
  ---------
86
- Copyright (c) 2011 Aaron Gibralter, and released under an MIT Licence.
76
+
77
+ Copyright (c) 2011-2014 Aaron Gibralter and Pat Allan, and released under an MIT
78
+ Licence.
data/Rakefile CHANGED
@@ -1,17 +1,10 @@
1
- #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
3
- require 'appraisal'
4
- require 'rspec/core/rake_task'
5
- require 'cucumber'
6
- require 'cucumber/rake/task'
1
+ require 'bundler/setup'
7
2
 
8
- RSpec::Core::RakeTask.new(:spec) do |t|
9
- t.rspec_opts = ["-c", "--format progress"]
10
- end
3
+ Bundler::GemHelper.install_tasks
11
4
 
12
- Cucumber::Rake::Task.new(:features) do |t|
13
- end
5
+ require 'appraisal'
6
+ require 'rspec/core/rake_task'
14
7
 
15
- task :all_tests => [:spec, :features]
8
+ RSpec::Core::RakeTask.new
16
9
 
17
- task :default => :all_tests
10
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "thinking-sphinx", "~> 2.1.0"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "thinking-sphinx", "~> 3.0.3"
6
+
7
+ gemspec :path=>"../"
@@ -1,9 +1,6 @@
1
1
  require 'resque'
2
2
  require 'thinking_sphinx'
3
3
 
4
- require 'thinking_sphinx/deltas/resque_delta/flag_as_deleted_set'
5
- require 'thinking_sphinx/deltas/resque_delta/index_utils'
6
-
7
4
  # Delayed Deltas for Thinking Sphinx, using Resque.
8
5
  #
9
6
  # This documentation is aimed at those reading the code. If you're looking for
@@ -15,104 +12,97 @@ require 'thinking_sphinx/deltas/resque_delta/index_utils'
15
12
  # @see http://ts.freelancing-gods.com Thinking Sphinx
16
13
  #
17
14
  class ThinkingSphinx::Deltas::ResqueDelta < ThinkingSphinx::Deltas::DefaultDelta
15
+ JOB_PREFIX = 'ts-delta'
16
+
18
17
  def self.job_types
19
18
  [
20
- ThinkingSphinx::Deltas::ResqueDelta::DeltaJob
19
+ ThinkingSphinx::Deltas::ResqueDelta::DeltaJob,
20
+ ThinkingSphinx::Deltas::ResqueDelta::FlagAsDeletedJob
21
21
  ]
22
22
  end
23
-
24
- def self.job_prefix
25
- 'ts-delta'
26
- end
27
-
23
+
28
24
  # LTRIM + LPOP deletes all items from the Resque queue without loading it
29
25
  # into client memory (unlike Resque.dequeue).
30
26
  # WARNING: This will clear ALL jobs in any queue used by a ResqueDelta job.
31
27
  # If you're sharing a queue with other jobs they'll be deleted!
32
- def self.clear_thinking_sphinx_queues
33
- job_types.collect { |c| c.instance_variable_get(:@queue) }.uniq.each do |q|
34
- Resque.redis.ltrim("queue:#{q}", 0, 0)
35
- Resque.redis.lpop("queue:#{q}")
28
+ def self.cancel_jobs
29
+ job_types.collect { |klass|
30
+ klass.instance_variable_get :@queue
31
+ }.uniq.each do |queue|
32
+ Resque.redis.ltrim "queue:#{queue}", 0, 0
33
+ Resque.redis.lpop "queue:#{queue}"
36
34
  end
37
35
  end
38
36
 
39
- # Clear both the resque queues and any other state maintained in redis
40
- def self.clear!
41
- self.clear_thinking_sphinx_queues
42
-
43
- FlagAsDeletedSet.clear_all!
44
- end
45
-
46
- # Use simplistic locking. We're assuming that the user won't run more than one
47
- # `rake ts:si` or `rake ts:in` task at a time.
48
- def self.lock(index_name)
49
- Resque.redis.set("#{job_prefix}:index:#{index_name}:locked", 'true')
50
- end
37
+ module Binary
38
+ # Adds a job to the queue for processing the given model's delta index. A
39
+ # job for hiding the instance in the core index is also created, if an
40
+ # instance is provided.
41
+ #
42
+ # Neither job will be queued if updates or deltas are disabled, or if the
43
+ # instance (when given) is not toggled to be in the delta index. The first
44
+ # two options are controlled via ThinkingSphinx.updates_enabled? and
45
+ # ThinkingSphinx.deltas_enabled?.
46
+ #
47
+ # @param [Class] model the ActiveRecord model to index.
48
+ # @param [ActiveRecord::Base] instance the instance of the given model that
49
+ # has changed. Optional.
50
+ # @return [Boolean] true
51
+ #
52
+ def index(model, instance = nil)
53
+ return true if skip? instance
51
54
 
52
- def self.unlock(index_name)
53
- Resque.redis.del("#{job_prefix}:index:#{index_name}:locked")
54
- end
55
+ Resque.enqueue ThinkingSphinx::Deltas::ResqueDelta::DeltaJob,
56
+ model.delta_index_names
55
57
 
56
- def self.locked?(index_name)
57
- Resque.redis.get("#{job_prefix}:index:#{index_name}:locked") == 'true'
58
- end
58
+ Resque.enqueue(
59
+ ThinkingSphinx::Deltas::ResqueDelta::FlagAsDeletedJob,
60
+ model.core_index_names, instance.sphinx_document_id
61
+ ) if instance
59
62
 
60
- def self.prepare_for_core_index(index_name)
61
- core = "#{index_name}_core"
62
- delta = "#{index_name}_delta"
63
+ true
64
+ end
63
65
 
64
- FlagAsDeletedSet.clear!(core)
66
+ private
65
67
 
66
- #clear delta jobs
67
- # dequeue is fast for jobs with arguments
68
- Resque.dequeue(ThinkingSphinx::Deltas::ResqueDelta::DeltaJob, delta)
68
+ # Checks whether jobs should be enqueued. Only true if updates and deltas are
69
+ # enabled, and the instance (if there is one) is toggled.
70
+ #
71
+ # @param [ActiveRecord::Base, NilClass] instance
72
+ # @return [Boolean]
73
+ #
74
+ def skip?(instance)
75
+ !ThinkingSphinx.updates_enabled? ||
76
+ !ThinkingSphinx.deltas_enabled? ||
77
+ (instance && !toggled(instance))
78
+ end
69
79
  end
70
80
 
71
- # Adds a job to the queue for processing the given model's delta index. A job
72
- # for hiding the instance in the core index is also created, if an instance is
73
- # provided.
74
- #
75
- # Neither job will be queued if updates or deltas are disabled, or if the
76
- # instance (when given) is not toggled to be in the delta index. The first two
77
- # options are controlled via ThinkingSphinx.updates_enabled? and
78
- # ThinkingSphinx.deltas_enabled?.
79
- #
80
- # @param [Class] model the ActiveRecord model to index.
81
- # @param [ActiveRecord::Base] instance the instance of the given model that
82
- # has changed. Optional.
83
- # @return [Boolean] true
84
- #
85
- def index(model, instance = nil)
86
- return true if skip?(instance)
87
- model.delta_index_names.each do |delta|
88
- next if self.class.locked?(delta)
81
+ module SphinxQL
82
+ def delete(index, instance)
89
83
  Resque.enqueue(
90
- ThinkingSphinx::Deltas::ResqueDelta::DeltaJob,
91
- delta
84
+ ThinkingSphinx::Deltas::ResqueDelta::FlagAsDeletedJob,
85
+ index.name, index.document_id_for_key(instance.id)
92
86
  )
93
87
  end
94
- if instance
95
- model.core_index_names.each do |core|
96
- FlagAsDeletedSet.add(core, instance.sphinx_document_id)
97
- end
88
+
89
+ def index(index)
90
+ Resque.enqueue ThinkingSphinx::Deltas::ResqueDelta::DeltaJob, index.name
98
91
  end
99
- true
100
92
  end
101
93
 
102
- private
103
-
104
- # Checks whether jobs should be enqueued. Only true if updates and deltas are
105
- # enabled, and the instance (if there is one) is toggled.
106
- #
107
- # @param [ActiveRecord::Base, NilClass] instance
108
- # @return [Boolean]
109
- #
110
- def skip?(instance)
111
- !ThinkingSphinx.updates_enabled? ||
112
- !ThinkingSphinx.deltas_enabled? ||
113
- (instance && !toggled(instance))
94
+ if [:delayed_job_priority, 'delayed_job_priority'].any? { |method|
95
+ ThinkingSphinx::Configuration.instance_methods.include?(method)
96
+ }
97
+ include Binary
98
+ else
99
+ include SphinxQL
114
100
  end
115
101
  end
116
102
 
103
+ require 'thinking_sphinx/deltas/resque_delta/flag_as_deleted_job'
117
104
  require 'thinking_sphinx/deltas/resque_delta/delta_job'
118
- require 'thinking_sphinx/deltas/resque_delta/core_index'
105
+
106
+ ThinkingSphinx.before_index_hooks << Proc.new {
107
+ ThinkingSphinx::Deltas::ResqueDelta.cancel_jobs
108
+ }
@@ -1,12 +1,7 @@
1
- require 'resque-lock-timeout'
2
-
3
1
  # A simple job class that processes a given index.
4
2
  #
5
3
  class ThinkingSphinx::Deltas::ResqueDelta::DeltaJob
6
-
7
- extend Resque::Plugins::LockTimeout
8
4
  @queue = :ts_delta
9
- @lock_timeout = 240
10
5
 
11
6
  # Runs Sphinx's indexer tool to process the index. Currently assumes Sphinx
12
7
  # is running.
@@ -14,113 +9,6 @@ class ThinkingSphinx::Deltas::ResqueDelta::DeltaJob
14
9
  # @param [String] index the name of the Sphinx index
15
10
  #
16
11
  def self.perform(index)
17
- return if skip?(index)
18
-
19
- config = ThinkingSphinx::Configuration.instance
20
-
21
- # Delta Index
22
- output = `#{config.bin_path}#{config.indexer_binary_name} --config #{config.config_file} --rotate #{index}`
23
- puts output unless ThinkingSphinx.suppress_delta_output?
24
-
25
- # Flag As Deleted
26
- return unless ThinkingSphinx.sphinx_running?
27
-
28
- index = ThinkingSphinx::Deltas::ResqueDelta::IndexUtils.delta_to_core(index)
29
-
30
- # Get the document ids we've saved
31
- flag_as_deleted_ids = ThinkingSphinx::Deltas::ResqueDelta::FlagAsDeletedSet.processing_members(index)
32
-
33
- unless flag_as_deleted_ids.empty?
34
- # Filter out the ids that aren't present in sphinx
35
- flag_as_deleted_ids = filter_flag_as_deleted_ids(flag_as_deleted_ids, index)
36
-
37
- unless flag_as_deleted_ids.empty?
38
- # Each hash element should be of the form { id => [1] }
39
- flag_hash = Hash[*flag_as_deleted_ids.collect {|id| [id, [1]] }.flatten(1)]
40
-
41
- config.client.update(index, ['sphinx_deleted'], flag_hash)
42
- end
43
- end
44
- end
45
-
46
- # Try again later if lock is in use.
47
- def self.lock_failed(*args)
48
- Resque.enqueue(self, *args)
49
- end
50
-
51
- # Run only one DeltaJob at a time regardless of index.
52
- #def self.identifier(*args)
53
- #nil
54
- #end
55
-
56
- # This allows us to have a concurrency safe version of ts-delayed-delta's
57
- # duplicates_exist:
58
- #
59
- # http://github.com/freelancing-god/ts-delayed-delta/blob/master/lib/thinkin
60
- # g_sphinx/deltas/delayed_delta/job.rb#L47
61
- #
62
- # The name of this method ensures that it runs within around_perform_lock.
63
- #
64
- # We've leveraged resque-lock-timeout to ensure that only one DeltaJob is
65
- # running at a time. Now, this around filter essentially ensures that only
66
- # one DeltaJob of each index type can sit at the queue at once. If the queue
67
- # has more than one, lrem will clear the rest off.
68
- #
69
- def self.around_perform_lock1(*args)
70
- # Remove all other instances of this job (with the same args) from the
71
- # queue. Uses LREM (http://code.google.com/p/redis/wiki/LremCommand) which
72
- # takes the form: "LREM key count value" and if count == 0 removes all
73
- # instances of value from the list.
74
- redis_job_value = Resque.encode(:class => self.to_s, :args => args)
75
- Resque.redis.lrem("queue:#{@queue}", 0, redis_job_value)
76
-
77
- # Grab the subset of flag as deleted document ids to work on
78
- core_index = ThinkingSphinx::Deltas::ResqueDelta::IndexUtils.delta_to_core(*args)
79
- ThinkingSphinx::Deltas::ResqueDelta::FlagAsDeletedSet.get_subset_for_processing(core_index)
80
-
81
- yield
82
-
83
- # Clear processing set
84
- ThinkingSphinx::Deltas::ResqueDelta::FlagAsDeletedSet.clear_processing(core_index)
85
- end
86
-
87
- protected
88
-
89
- def self.skip?(index)
90
- ThinkingSphinx::Deltas::ResqueDelta.locked?(index)
91
- end
92
-
93
- def self.filter_flag_as_deleted_ids(ids, index)
94
- search_results = []
95
- partition_ids(ids, 4096) do |subset|
96
- search_results += ThinkingSphinx.search_for_ids(
97
- :with => {:@id => subset}, :index => index
98
- ).results[:matches].collect { |match| match[:doc] }
99
- end
100
-
101
- search_results
102
- end
103
-
104
- def self.partition_ids(ids, n)
105
- if n > 0 && n < ids.size
106
- result = []
107
- max_subarray_size = n - 1
108
- i = j = 0
109
- while i < ids.size && j < ids.size
110
- j = i + max_subarray_size
111
- result << ids.slice(i..j)
112
- i += n
113
- end
114
- else
115
- result = ids
116
- end
117
-
118
- if block_given?
119
- result.each do |ary|
120
- yield ary
121
- end
122
- end
123
-
124
- result
12
+ ThinkingSphinx::Deltas::IndexJob.new(index).perform
125
13
  end
126
14
  end