Class: ConeyIsland::JobsCache
- Inherits:
-
Object
- Object
- ConeyIsland::JobsCache
- Defined in:
- lib/coney_island/jobs_cache.rb
Overview
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.
Instance Method Summary collapse
-
#cache_job(*args) ⇒ Object
Cache a job with the given args.
-
#cache_jobs ⇒ Object
Start caching jobs.
-
#cached_jobs ⇒ Object
List of the currently cached jobs, anxiously waiting to be flushed.
-
#caching_jobs(&blk) ⇒ Object
Caches jobs for the duration of the block, flushes them at the end.
-
#caching_jobs? ⇒ Boolean
Are we caching jobs?.
- #clear ⇒ Object
-
#flush_jobs ⇒ Object
Publish all the cached jobs.
-
#initialize ⇒ JobsCache
constructor
A new instance of JobsCache.
-
#stop_caching_jobs ⇒ Object
Stop caching jobs.
Constructor Details
#initialize ⇒ JobsCache
Returns a new instance of JobsCache.
9 10 11 |
# File 'lib/coney_island/jobs_cache.rb', line 9 def initialize @adapter = RequestStore end |
Instance Method Details
#cache_job(*args) ⇒ Object
Cache a job with the given args
41 42 43 44 |
# File 'lib/coney_island/jobs_cache.rb', line 41 def cache_job(*args) self.cached_jobs[generate_id(*args)] = args self end |
#cache_jobs ⇒ Object
Start caching jobs
19 20 21 22 |
# File 'lib/coney_island/jobs_cache.rb', line 19 def cache_jobs self.is_caching_jobs = true self end |
#cached_jobs ⇒ Object
List of the currently cached jobs, anxiously waiting to be flushed
59 60 61 |
# File 'lib/coney_island/jobs_cache.rb', line 59 def cached_jobs @adapter.store[:jobs] ||= {} end |
#caching_jobs(&blk) ⇒ Object
Caches jobs for the duration of the block, flushes them at the end.
31 32 33 34 35 36 37 38 |
# 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? ⇒ Boolean
Are we caching jobs?
14 15 16 |
# File 'lib/coney_island/jobs_cache.rb', line 14 def caching_jobs? !! is_caching_jobs end |
#clear ⇒ Object
63 64 65 66 |
# File 'lib/coney_island/jobs_cache.rb', line 63 def clear self.is_caching_jobs = false self.cached_jobs = {} end |
#flush_jobs ⇒ Object
Publish all the cached jobs
47 48 49 50 51 52 53 54 55 56 |
# 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 ⇒ Object
Stop caching jobs
25 26 27 28 |
# File 'lib/coney_island/jobs_cache.rb', line 25 def stop_caching_jobs self.is_caching_jobs = false self end |