-
Notifications
You must be signed in to change notification settings - Fork 1
/
warming_drawer.rb
52 lines (41 loc) · 1.3 KB
/
warming_drawer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'warming_drawer/configuration'
require 'warming_drawer/version'
require 'warming_drawer/workers/base_worker'
require 'warming_drawer/workers/url_worker'
module WarmingDrawer
class << self
attr_accessor :configuration
end
def self.configure
self.configuration ||= Configuration.new
yield(configuration)
end
# Warms a cache. Delegates to the proper worker depending on the type.
#
# @param [Array] Arguments or Array to warm
# @return [Boolean]
# @example
# WarmingDrawer.warm('http://sweet.dev/1', 'http://sweet2.dev/2', :type => :url)
# => true
def self.warm(*args)
options = args.extract_options!
options[:type] ||= :url
if worker = available_worker_for_type(options[:type])
# TODO: can this move into worker since its reading config?
worker.options queue: configuration.queue_name, retry: configuration.retry
worker.perform_with(args)
end
!worker.nil?
end
# Returns true if a queuing system is defined
# @return [Boolean]
def self.use_queue?
configuration.queue_system != :inline
end
private
def self.available_worker_for_type(type)
if worker_name = Workers.constants.detect {|c| c.downcase.match /^#{type.to_s.downcase}/}
WarmingDrawer::Workers.const_get worker_name.to_s
end
end
end