index-tanked 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,10 +10,18 @@ class CreateIndexTankedDocuments < ActiveRecord::Migration
10
10
  end
11
11
 
12
12
  add_index :index_tanked_documents, :locked_at
13
+ add_index :index_tanked_documents, :locked_by
14
+ add_index :index_tanked_documents, [:model_name, :record_id]
15
+ add_index :index_tanked_documents, [:model_name, :record_id, :locked_by], :name => 'index_index_tanked_documents_docid_by_identifier'
16
+
13
17
  end
14
18
 
15
19
  def self.down
16
20
  remove_index :index_tanked_documents, :locked_at
21
+ remove_index :index_tanked_documents, :locked_by
22
+ remove_index :index_tanked_documents, [:model_name, :record_id]
23
+ remove_index :index_tanked_documents, :name => 'index_index_tanked_documents_docid_by_identifier'
24
+
17
25
  drop_table :index_tanked_documents
18
26
  end
19
27
  end
@@ -10,10 +10,18 @@ class CreateIndexTankedDocuments < ActiveRecord::Migration
10
10
  end
11
11
 
12
12
  add_index :index_tanked_documents, :locked_at
13
+ add_index :index_tanked_documents, :locked_by
14
+ add_index :index_tanked_documents, [:model_name, :record_id]
15
+ add_index :index_tanked_documents, [:model_name, :record_id, :locked_by], :name => 'index_index_tanked_documents_docid_by_identifier'
16
+
13
17
  end
14
18
 
15
19
  def self.down
16
20
  remove_index :index_tanked_documents, :locked_at
21
+ remove_index :index_tanked_documents, :locked_by
22
+ remove_index :index_tanked_documents, [:model_name, :record_id]
23
+ remove_index :index_tanked_documents, :name => 'index_index_tanked_documents_docid_by_identifier'
24
+
17
25
  drop_table :index_tanked_documents
18
26
  end
19
27
  end
@@ -14,9 +14,14 @@ module IndexTanked
14
14
  end
15
15
 
16
16
  def inspect
17
+ return super if read_attribute(:document).nil?
17
18
  super.sub(/document: \"[^\"\r\n]*\"/, %{document: #{document.inspect}})
18
19
  end
19
20
 
21
+ def newest_record_with_this_docid?
22
+ self.class.find_by_model_name_and_record_id(self.model_name, self.record_id, :order => 'created_at DESC', :limit => 1) == self
23
+ end
24
+
20
25
  def self.clear_locks_by_identifier(identifier)
21
26
  locks_cleared = update_all(["locked_by = NULL, locked_at = NULL"],
22
27
  ["locked_by = ?", identifier])
@@ -29,8 +34,28 @@ module IndexTanked
29
34
  locks_cleared
30
35
  end
31
36
 
37
+ def self.delete_outdated_locked_records_by_identifier(identifier)
38
+ ids_to_delete = non_unique_docids_by_identifier(identifier).inject([]) do |ids, (model_name, record_id)|
39
+ record = find_by_model_name_and_record_id_and_locked_by(model_name, record_id, identifier)
40
+ ids << record.id unless record.newest_record_with_this_docid?
41
+ ids
42
+ end
43
+
44
+ if ids_to_delete.any?
45
+ delete_all(['id in (?)', ids_to_delete])
46
+ else
47
+ 0
48
+ end
49
+ end
50
+
51
+ def self.non_unique_docids_by_identifier(identifier)
52
+ find_by_sql(['SELECT model_name, record_id FROM index_tanked_documents GROUP BY model_name, record_id HAVING count(*) > 1 INTERSECT SELECT model_name, record_id FROM index_tanked_documents WHERE locked_by = ?', identifier]).map do |partial_record|
53
+ [partial_record.model_name, partial_record.record_id]
54
+ end
55
+ end
56
+
32
57
  def self.enqueue(record_id, model_name, document_hash)
33
- delete_all(:record_id => record_id, :model_name => model_name)
58
+ delete_all(:record_id => record_id, :model_name => model_name, :locked_by => nil)
34
59
  create(:record_id => record_id, :model_name => model_name, :document => document_hash)
35
60
  end
36
61
 
@@ -48,7 +48,9 @@ module IndexTanked
48
48
  documents_deleted
49
49
  rescue StandardError, Timeout::Error => e
50
50
  handle_error(e)
51
+ outdated_locked_records_deleted = Queue::Document.delete_outdated_locked_records_by_identifier(@identifier)
51
52
  locks_cleared = Queue::Document.clear_locks_by_identifier(@identifier)
53
+ log("#{outdated_locked_records_deleted} outdated locks deleted")
52
54
  log("#{locks_cleared} locks cleared")
53
55
  0 # return 0 so it sleeps
54
56
  end
@@ -1,3 +1,3 @@
1
1
  module IndexTanked
2
- GEM_VERSION = '0.3.0'
2
+ GEM_VERSION = '0.3.1'
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: index-tanked
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Adam Kittelson
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-06-16 00:00:00 -07:00
19
+ date: 2011-06-17 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency