Class: MultiBackgroundJob::Worker
- Inherits:
-
Object
- Object
- MultiBackgroundJob::Worker
- Defined in:
- lib/multi_background_job/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.
-
#unique_job ⇒ Object
readonly
Returns the value of attribute unique_job.
-
#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, **options) ⇒ Worker
constructor
A new instance of Worker.
-
#push(to: nil) ⇒ Object
Response of service.
-
#unique(value) ⇒ Object
Wrap uniq options.
- #unique_job? ⇒ Boolean
-
#with_args(*args) ⇒ Object
Adds arguments to the job.
- #with_job_jid(jid = nil) ⇒ Object
Constructor Details
#initialize(worker_class, **options) ⇒ Worker
Returns a new instance of Worker.
11 12 13 14 15 16 |
# File 'lib/multi_background_job/worker.rb', line 11 def initialize(worker_class, **) @worker_class = worker_class = @payload = {} unique(.delete(:uniq)) if .key?(:uniq) end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
9 10 11 |
# File 'lib/multi_background_job/worker.rb', line 9 def arguments @arguments end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/multi_background_job/worker.rb', line 7 def end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
7 8 9 |
# File 'lib/multi_background_job/worker.rb', line 7 def payload @payload end |
#unique_job ⇒ Object (readonly)
Returns the value of attribute unique_job.
7 8 9 |
# File 'lib/multi_background_job/worker.rb', line 7 def unique_job @unique_job end |
#worker_class ⇒ Object (readonly)
Returns the value of attribute worker_class.
7 8 9 |
# File 'lib/multi_background_job/worker.rb', line 7 def worker_class @worker_class end |
Class Method Details
.coerce(service:, payload:, **opts) ⇒ Object
18 19 20 |
# File 'lib/multi_background_job/worker.rb', line 18 def self.coerce(service:, payload:, **opts) SERVICES.fetch(service).coerce_to_worker(payload, **opts) end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
100 101 102 103 104 105 106 107 |
# File 'lib/multi_background_job/worker.rb', line 100 def eql?(other) return false unless other.is_a?(self.class) worker_class == other.worker_class && \ payload == other.payload && == other. && unique_job == other.unique_job 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.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/multi_background_job/worker.rb', line 48 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(to: nil) ⇒ Object
Returns Response of service.
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/multi_background_job/worker.rb', line 87 def push(to: nil) to ||= [:service] unless SERVICES.include?(to) raise Error, format('Service %<to>p is not implemented. Please use one of %<list>p', to: to, list: SERVICES.keys) end @payload['created_at'] ||= Time.now.to_f worker_to_push = with_job_jid MultiBackgroundJob.config.middleware.invoke(worker_to_push, to) do SERVICES[to].push(worker_to_push) end end |
#unique(value) ⇒ Object
Wrap uniq options
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/multi_background_job/worker.rb', line 65 def unique(value) value = {} if value == true @unique_job = \ case value when Hash then UniqueJob.coerce(value) when UniqueJob then value else nil end self end |
#unique_job? ⇒ Boolean
110 111 112 |
# File 'lib/multi_background_job/worker.rb', line 110 def unique_job? unique_job.is_a?(UniqueJob) end |
#with_args(*args) ⇒ Object
Adds arguments to the job
39 40 41 42 43 |
# File 'lib/multi_background_job/worker.rb', line 39 def with_args(*args) @payload['args'] = args self end |
#with_job_jid(jid = nil) ⇒ Object
78 79 80 81 82 |
# File 'lib/multi_background_job/worker.rb', line 78 def with_job_jid(jid = nil) @payload['jid'] ||= jid || MultiBackgroundJob.jid self end |