Module: Whoosh::Jobs
- Defined in:
- lib/whoosh/jobs.rb,
lib/whoosh/jobs/worker.rb,
lib/whoosh/jobs/redis_backend.rb,
lib/whoosh/jobs/memory_backend.rb
Defined Under Namespace
Classes: MemoryBackend, RedisBackend, Worker
Class Attribute Summary collapse
-
.backend ⇒ Object
readonly
Returns the value of attribute backend.
-
.di ⇒ Object
readonly
Returns the value of attribute di.
Class Method Summary collapse
-
.build_backend(config_data = {}) ⇒ Object
Build the right backend from config (auto-detect pattern).
- .configure(backend:, di: nil) ⇒ Object
- .configured? ⇒ Boolean
- .enqueue(job_class, run_at: nil, **args) ⇒ Object
- .find(id) ⇒ Object
- .reset! ⇒ Object
Class Attribute Details
.backend ⇒ Object (readonly)
Returns the value of attribute backend.
15 16 17 |
# File 'lib/whoosh/jobs.rb', line 15 def backend @backend end |
.di ⇒ Object (readonly)
Returns the value of attribute di.
15 16 17 |
# File 'lib/whoosh/jobs.rb', line 15 def di @di end |
Class Method Details
.build_backend(config_data = {}) ⇒ Object
Build the right backend from config (auto-detect pattern)
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/whoosh/jobs.rb', line 56 def build_backend(config_data = {}) jobs_config = config_data["jobs"] || {} redis_url = ENV["REDIS_URL"] || jobs_config["redis_url"] if redis_url && jobs_config["backend"] != "memory" RedisBackend.new(url: redis_url) else MemoryBackend.new end end |
.configure(backend:, di: nil) ⇒ Object
17 18 19 20 |
# File 'lib/whoosh/jobs.rb', line 17 def configure(backend:, di: nil) @backend = backend @di = di end |
.configured? ⇒ Boolean
22 23 24 |
# File 'lib/whoosh/jobs.rb', line 22 def configured? !!@backend end |
.enqueue(job_class, run_at: nil, **args) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/whoosh/jobs.rb', line 26 def enqueue(job_class, run_at: nil, **args) raise Errors::DependencyError, "Jobs not configured — boot a Whoosh::App first" unless configured? id = SecureRandom.uuid queue_name = job_class.respond_to?(:queue) ? job_class.queue : "default" record = { id: id, class_name: job_class.name, args: args, queue: queue_name, status: run_at ? :scheduled : :pending, run_at: run_at, result: nil, error: nil, retry_count: 0, created_at: Time.now.to_f, started_at: nil, completed_at: nil } @backend.save(record) @backend.push({ id: id, class_name: job_class.name, args: args, queue: queue_name, run_at: run_at }) id end |
.find(id) ⇒ Object
50 51 52 53 |
# File 'lib/whoosh/jobs.rb', line 50 def find(id) raise Errors::DependencyError, "Jobs not configured" unless configured? @backend.find(id) end |
.reset! ⇒ Object
67 68 69 70 |
# File 'lib/whoosh/jobs.rb', line 67 def reset! @backend = nil @di = nil end |