async_cache 1.0.2 → 1.1.0
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/async_cache.rb +1 -0
- data/lib/async_cache/railtie.rb +11 -0
- data/lib/async_cache/store.rb +24 -0
- data/lib/async_cache/tasks/clear.rake +16 -0
- data/lib/async_cache/version.rb +1 -1
- data/lib/async_cache/workers/active_job.rb +4 -0
- data/lib/async_cache/workers/base.rb +5 -0
- data/lib/async_cache/workers/sidekiq.rb +13 -3
- data/spec/railtie_spec.rb +17 -0
- data/spec/tasks_spec.rb +30 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f27471956b05c3bb40ca9741a98a95134308fc84
|
4
|
+
data.tar.gz: 0cc68507dc421ff154b487d1db2fa2db56f4dc07
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: edacd1a9fff44a91e2791d8bcf27b9511bd2d9806d475637d93a874d51c715a11d48959138c8509d91f6a9b4f4de8b35cab6827821a61bdcc96478fd8c244df4
|
7
|
+
data.tar.gz: 88a1777e3e4e4bca3fcc51695c670e5160f1380da356ae9417e49c36aa02877b0663f6679243cf8c26623b3f4e3e5f0b92ec2c8566533c0664825b635fea35c6
|
data/Gemfile.lock
CHANGED
data/lib/async_cache.rb
CHANGED
data/lib/async_cache/store.rb
CHANGED
@@ -2,6 +2,11 @@ module AsyncCache
|
|
2
2
|
class Store
|
3
3
|
attr_accessor :backend, :worker_klass
|
4
4
|
|
5
|
+
# Global index of store instances
|
6
|
+
def self.stores
|
7
|
+
@stores ||= []
|
8
|
+
end
|
9
|
+
|
5
10
|
# @param [Hash] opts Initialization options
|
6
11
|
# @option opts [Object] :backend The backend that it will read/write
|
7
12
|
# entries to/from
|
@@ -19,6 +24,9 @@ module AsyncCache
|
|
19
24
|
end
|
20
25
|
|
21
26
|
@backend = opts[:backend] || AsyncCache.backend
|
27
|
+
|
28
|
+
# Register ourselves in the array of known store instances
|
29
|
+
self.class.stores << self
|
22
30
|
end
|
23
31
|
|
24
32
|
# @param [String] locator The constant locator for the entry in the cache
|
@@ -66,6 +74,10 @@ module AsyncCache
|
|
66
74
|
end
|
67
75
|
end
|
68
76
|
|
77
|
+
def clear
|
78
|
+
@worker_klass.clear
|
79
|
+
end
|
80
|
+
|
69
81
|
def determine_strategy(has_cached_data:, needs_regen:, synchronous_regen:)
|
70
82
|
case
|
71
83
|
when !has_cached_data
|
@@ -111,6 +123,18 @@ module AsyncCache
|
|
111
123
|
)
|
112
124
|
end
|
113
125
|
|
126
|
+
def inspect
|
127
|
+
pointer_format = '0x%014x'
|
128
|
+
pointer = Kernel.sprintf pointer_format, self.object_id * 2
|
129
|
+
backend_pointer = Kernel.sprintf pointer_format, @backend.object_id * 2
|
130
|
+
|
131
|
+
'#<' + [
|
132
|
+
"#{self.class.name}:#{pointer} ",
|
133
|
+
"@worker_klass=#{@worker_klass.name}, ",
|
134
|
+
"@backend=#<#{@backend.class.name}:#{backend_pointer}>"
|
135
|
+
].join('') + '>'
|
136
|
+
end
|
137
|
+
|
114
138
|
private
|
115
139
|
|
116
140
|
# Ensures the arguments are primitives.
|
@@ -0,0 +1,16 @@
|
|
1
|
+
namespace :async_cache do
|
2
|
+
prereqs =
|
3
|
+
if defined?(Rails)
|
4
|
+
[:environment]
|
5
|
+
else
|
6
|
+
[]
|
7
|
+
end
|
8
|
+
|
9
|
+
desc 'Clear the worker queues'
|
10
|
+
task :clear => prereqs do
|
11
|
+
AsyncCache::Store.stores.each do |store|
|
12
|
+
store.clear
|
13
|
+
puts "Cleared #{store.inspect}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/async_cache/version.rb
CHANGED
@@ -11,6 +11,10 @@ module AsyncCache
|
|
11
11
|
true
|
12
12
|
end
|
13
13
|
|
14
|
+
def self.clear
|
15
|
+
raise NotImplementedError, 'ActiveJob does not support clearing queues'
|
16
|
+
end
|
17
|
+
|
14
18
|
def self.enqueue_async_job(key:, version:, expires_in:, block:, arguments:)
|
15
19
|
self.perform_later key, version, expires_in, arguments, block
|
16
20
|
end
|
@@ -23,6 +23,11 @@ module AsyncCache
|
|
23
23
|
raise NotImplementedError
|
24
24
|
end
|
25
25
|
|
26
|
+
# Clear the active jobs from this worker's queue.
|
27
|
+
def self.clear
|
28
|
+
raise NotImplementedError
|
29
|
+
end
|
30
|
+
|
26
31
|
# Public interface for enqueuing jobs. This is what is called by
|
27
32
|
# {AsyncCache::Store}.
|
28
33
|
def self.enqueue_async_job(key:, version:, expires_in:, block:, arguments:)
|
@@ -13,22 +13,32 @@ module AsyncCache
|
|
13
13
|
# Use the Sidekiq API to see if there are worker processes available to
|
14
14
|
# handle the async cache jobs queue.
|
15
15
|
def self.has_workers?
|
16
|
-
target_queue = self.sidekiq_options['queue'].to_s
|
17
|
-
|
18
16
|
processes = Sidekiq::ProcessSet.new.to_a
|
19
17
|
queues_being_processed = processes.flat_map { |p| p['queues'] }
|
20
18
|
|
21
|
-
if queues_being_processed.include?
|
19
|
+
if queues_being_processed.include? sidekiq_queue
|
22
20
|
true
|
23
21
|
else
|
24
22
|
false
|
25
23
|
end
|
26
24
|
end
|
27
25
|
|
26
|
+
def self.clear
|
27
|
+
queue = Sidekiq::Queue.new sidekiq_queue
|
28
|
+
|
29
|
+
queue.clear
|
30
|
+
end
|
31
|
+
|
28
32
|
def self.enqueue_async_job(key:, version:, expires_in:, block:, arguments:)
|
29
33
|
self.perform_async key, version, expires_in, arguments, block
|
30
34
|
end
|
31
35
|
|
36
|
+
private
|
37
|
+
|
38
|
+
def self.sidekiq_queue
|
39
|
+
self.sidekiq_options['queue'].to_s
|
40
|
+
end
|
41
|
+
|
32
42
|
end # class SidekiqWorker
|
33
43
|
end
|
34
44
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AsyncCache do
|
4
|
+
context 'Railtie' do
|
5
|
+
class Application < Rails::Application
|
6
|
+
end
|
7
|
+
|
8
|
+
before(:all) do
|
9
|
+
app = Application.new
|
10
|
+
app.load_tasks
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'adds the async_cache:* tasks' do
|
14
|
+
expect(Rake::Task.task_defined?('async_cache:clear')).to eq true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/spec/tasks_spec.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AsyncCache do
|
4
|
+
context 'tasks' do
|
5
|
+
before(:all) do
|
6
|
+
require 'rake'
|
7
|
+
Rake.application.clear
|
8
|
+
Rake.application.add_import File.join(File.dirname(__FILE__), '../lib/async_cache/tasks/clear.rake')
|
9
|
+
Rake.application.load_imports
|
10
|
+
end
|
11
|
+
|
12
|
+
before(:each) do
|
13
|
+
# Reset the array of known store instances
|
14
|
+
AsyncCache::Store.instance_eval { @stores = [] }
|
15
|
+
end
|
16
|
+
|
17
|
+
describe ':clear' do
|
18
|
+
it 'clears with worker queues' do
|
19
|
+
store = AsyncCache::Store.new backend: Rails.cache, worker: :sidekiq
|
20
|
+
|
21
|
+
queue_double = double 'Sidekiq::Queue'
|
22
|
+
allow(Sidekiq::Queue).to receive(:new).and_return(queue_double)
|
23
|
+
|
24
|
+
expect(queue_double).to receive(:clear)
|
25
|
+
|
26
|
+
Rake.application['async_cache:clear'].execute
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: async_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Derewecki
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-01-
|
12
|
+
date: 2016-01-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sourcify
|
@@ -185,7 +185,9 @@ files:
|
|
185
185
|
- async_cache.gemspec
|
186
186
|
- examples/examples_controller.rb
|
187
187
|
- lib/async_cache.rb
|
188
|
+
- lib/async_cache/railtie.rb
|
188
189
|
- lib/async_cache/store.rb
|
190
|
+
- lib/async_cache/tasks/clear.rake
|
189
191
|
- lib/async_cache/version.rb
|
190
192
|
- lib/async_cache/workers/active_job.rb
|
191
193
|
- lib/async_cache/workers/base.rb
|
@@ -195,8 +197,10 @@ files:
|
|
195
197
|
- spec/integration/support/run_all.rb
|
196
198
|
- spec/integration/support/sinatra.rb
|
197
199
|
- spec/integration/support/version.txt
|
200
|
+
- spec/railtie_spec.rb
|
198
201
|
- spec/spec_helper.rb
|
199
202
|
- spec/store_spec.rb
|
203
|
+
- spec/tasks_spec.rb
|
200
204
|
- spec/workers/active_job_spec.rb
|
201
205
|
- spec/workers/base_spec.rb
|
202
206
|
- spec/workers/sidekiq_spec.rb
|