class ConeyIsland::JobsCache
Handles job caching hijinks. This is especially useful for Rails apps where you can set Coney to cache jobs at the beginning of the request and flush them once you the request is served. Most methods are exposed by ConeyIsland
so you'd just use ConeyIsland.cache_jobs
, ConeyIsland.flush_jobs
.
Public Class Methods
new()
click to toggle source
# File lib/coney_island/jobs_cache.rb, line 9 def initialize @adapter = RequestStore end
Public Instance Methods
cache_job(*args)
click to toggle source
Cache a job with the given args
# File lib/coney_island/jobs_cache.rb, line 41 def cache_job(*args) self.cached_jobs[generate_id(*args)] = args self end
cache_jobs()
click to toggle source
Start caching jobs
# File lib/coney_island/jobs_cache.rb, line 19 def cache_jobs self.is_caching_jobs = true self end
cached_jobs()
click to toggle source
List of the currently cached jobs, anxiously waiting to be flushed
# File lib/coney_island/jobs_cache.rb, line 59 def cached_jobs @adapter.store[:jobs] ||= {} end
caching_jobs(&blk)
click to toggle source
Caches jobs for the duration of the block, flushes them at the end.
# File lib/coney_island/jobs_cache.rb, line 31 def caching_jobs(&blk) _was_caching = caching_jobs? cache_jobs blk.call flush_jobs self.is_caching_jobs = _was_caching self end
caching_jobs?()
click to toggle source
Are we caching jobs?
# File lib/coney_island/jobs_cache.rb, line 14 def caching_jobs? !! is_caching_jobs end
clear()
click to toggle source
# File lib/coney_island/jobs_cache.rb, line 63 def clear self.is_caching_jobs = false self.cached_jobs = {} end
flush_jobs()
click to toggle source
Publish all the cached jobs
# File lib/coney_island/jobs_cache.rb, line 47 def flush_jobs # Get all the jobs, one at a time, pulling from the list while job = self.cached_jobs.shift # Map the array to the right things job_id, args = *job # Submit! takes care of rescuing, error logging, etc and never caches submit! args, job_id end self end
stop_caching_jobs()
click to toggle source
Stop caching jobs
# File lib/coney_island/jobs_cache.rb, line 25 def stop_caching_jobs self.is_caching_jobs = false self end
Protected Instance Methods
cached_jobs=(something)
click to toggle source
# File lib/coney_island/jobs_cache.rb, line 70 def cached_jobs=(something) @adapter.store[:jobs] = something end
generate_id(*args)
click to toggle source
# File lib/coney_island/jobs_cache.rb, line 82 def generate_id(*args) # Duplicate the args so we don't mess with the original _args = args.dup # Do we have job arguments and highlander is true? if _args.last.is_a?(Hash) && !!ActiveSupport::HashWithIndifferentAccess.new(_args.pop)[:highlander] # We simply generate an id based on the class, method, arguments signature _args.map(&:to_s).join("-") else # We generate a new id every time SecureRandom.uuid end end
is_caching_jobs()
click to toggle source
# File lib/coney_island/jobs_cache.rb, line 74 def is_caching_jobs @adapter.store[:caching_jobs] end
is_caching_jobs=(boolean)
click to toggle source
# File lib/coney_island/jobs_cache.rb, line 78 def is_caching_jobs=(boolean) @adapter.store[:caching_jobs] = boolean end