Class: Esse::AsyncIndexing::Worker
- Inherits:
-
Object
- Object
- Esse::AsyncIndexing::Worker
- Defined in:
- lib/esse/async_indexing/worker.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
-
#worker_class ⇒ Object
readonly
Returns the value of attribute worker_class.
Class Method Summary collapse
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
-
#in(timestamp) ⇒ Object
(also: #at)
Schedule the time when a job will be executed.
-
#initialize(worker_class, service: nil, **options) ⇒ Worker
constructor
A new instance of Worker.
-
#push ⇒ Object
Response of service.
-
#with_args(*args) ⇒ Object
Adds arguments to the job.
- #with_job_jid(jid = nil) ⇒ Object
Constructor Details
#initialize(worker_class, service: nil, **options) ⇒ Worker
Returns a new instance of Worker.
9 10 11 12 13 14 |
# File 'lib/esse/async_indexing/worker.rb', line 9 def initialize(worker_class, service: nil, **) @worker_class = worker_class @service = service @options = @payload = {} end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
7 8 9 |
# File 'lib/esse/async_indexing/worker.rb', line 7 def arguments @arguments end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/esse/async_indexing/worker.rb', line 5 def @options end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
5 6 7 |
# File 'lib/esse/async_indexing/worker.rb', line 5 def payload @payload end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
5 6 7 |
# File 'lib/esse/async_indexing/worker.rb', line 5 def service @service end |
#worker_class ⇒ Object (readonly)
Returns the value of attribute worker_class.
5 6 7 |
# File 'lib/esse/async_indexing/worker.rb', line 5 def worker_class @worker_class end |
Class Method Details
.coerce(service:, payload:, **opts) ⇒ Object
16 17 18 |
# File 'lib/esse/async_indexing/worker.rb', line 16 def self.coerce(service:, payload:, **opts) Esse::AsyncIndexing::SERVICES.fetch(service).coerce_to_worker(payload, **opts) end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
76 77 78 79 80 81 82 |
# File 'lib/esse/async_indexing/worker.rb', line 76 def eql?(other) return false unless other.is_a?(self.class) worker_class == other.worker_class && payload == other.payload && == other. end |
#in(timestamp) ⇒ Object Also known as: at
Schedule the time when a job will be executed. Jobs which are scheduled in the past are enqueued for immediate execution.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/esse/async_indexing/worker.rb', line 46 def in() now = Time.now.to_f = Time.parse() if .is_a?(String) int = .respond_to?(:strftime) ? .to_f : now + .to_f return self if int <= now @payload["at"] = int @payload["created_at"] = now self end |
#push ⇒ Object
Returns Response of service.
67 68 69 70 71 72 73 74 |
# File 'lib/esse/async_indexing/worker.rb', line 67 def push unless Esse::AsyncIndexing::SERVICES.key?(service) raise Esse::AsyncIndexing::Error, format("Service %<service>p is not implemented. Please use one of #{Esse::AsyncIndexing::SERVICES.keys.map(&:inspect).join(" or ")}.", service: service) end @payload["created_at"] ||= Time.now.to_f worker_to_push = with_job_jid Esse::AsyncIndexing::SERVICES[service].push(worker_to_push) end |
#with_args(*args) ⇒ Object
Adds arguments to the job
37 38 39 40 41 |
# File 'lib/esse/async_indexing/worker.rb', line 37 def with_args(*args) @payload["args"] = args self end |
#with_job_jid(jid = nil) ⇒ Object
59 60 61 62 63 |
# File 'lib/esse/async_indexing/worker.rb', line 59 def with_job_jid(jid = nil) @payload["jid"] ||= jid || Esse::AsyncIndexing.jid self end |