Class: NatsWork::Job
- Inherits:
-
Object
- Object
- NatsWork::Job
- Defined in:
- lib/natswork/job.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#job_id ⇒ Object
readonly
Returns the value of attribute job_id.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#retry_count ⇒ Object
readonly
Returns the value of attribute retry_count.
Class Method Summary collapse
- .get_queue ⇒ Object
- .get_retries ⇒ Object
- .get_timeout ⇒ Object
- .inherited(subclass) ⇒ Object
- .perform_async(**args) ⇒ Object
- .perform_sync(**args) ⇒ Object
- .queue(name = nil) ⇒ Object
- .retries(count = nil) ⇒ Object
- .timeout(seconds = nil) ⇒ Object
- .timeout_value ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Job
constructor
A new instance of Job.
- #perform(*args) ⇒ Object
Constructor Details
#initialize ⇒ Job
Returns a new instance of Job.
89 90 91 92 93 94 |
# File 'lib/natswork/job.rb', line 89 def initialize @job_id = nil @retry_count = 0 @metadata = {} @context = {} end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
87 88 89 |
# File 'lib/natswork/job.rb', line 87 def context @context end |
#job_id ⇒ Object (readonly)
Returns the value of attribute job_id.
86 87 88 |
# File 'lib/natswork/job.rb', line 86 def job_id @job_id end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
86 87 88 |
# File 'lib/natswork/job.rb', line 86 def @metadata end |
#retry_count ⇒ Object (readonly)
Returns the value of attribute retry_count.
86 87 88 |
# File 'lib/natswork/job.rb', line 86 def retry_count @retry_count end |
Class Method Details
.get_queue ⇒ Object
45 46 47 |
# File 'lib/natswork/job.rb', line 45 def get_queue @queue || (superclass.respond_to?(:get_queue) && superclass.get_queue) || 'default' end |
.get_retries ⇒ Object
49 50 51 |
# File 'lib/natswork/job.rb', line 49 def get_retries @retries || (superclass.respond_to?(:get_retries) && superclass.get_retries) || 3 end |
.get_timeout ⇒ Object
53 54 55 |
# File 'lib/natswork/job.rb', line 53 def get_timeout @timeout || (superclass.respond_to?(:get_timeout) && superclass.get_timeout) || 30 end |
.inherited(subclass) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/natswork/job.rb', line 8 def inherited(subclass) super return unless subclass.name && !subclass.name.empty? Registry.instance.register( subclass.name, subclass, queue: subclass.instance_variable_get(:@queue) || 'default', retries: subclass.instance_variable_get(:@retries), timeout: subclass.instance_variable_get(:@timeout) ) end |
.perform_async(**args) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/natswork/job.rb', line 61 def perform_async(**args) # Accept keyword arguments directly Client.push( job_class: name, queue: get_queue, max_retries: get_retries, timeout: get_timeout, arguments: args ) end |
.perform_sync(**args) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/natswork/job.rb', line 73 def perform_sync(**args) # Accept keyword arguments directly Client.perform_sync( job_class: name, queue: get_queue, max_retries: get_retries, timeout: get_timeout, arguments: args ) end |
.queue(name = nil) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/natswork/job.rb', line 21 def queue(name = nil) if name @queue = name else @queue end end |
.retries(count = nil) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/natswork/job.rb', line 29 def retries(count = nil) if count @retries = count else @retries end end |
.timeout(seconds = nil) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/natswork/job.rb', line 37 def timeout(seconds = nil) if seconds @timeout = seconds else @timeout end end |
.timeout_value ⇒ Object
57 58 59 |
# File 'lib/natswork/job.rb', line 57 def timeout_value get_timeout end |
Instance Method Details
#perform(*args) ⇒ Object
96 97 98 |
# File 'lib/natswork/job.rb', line 96 def perform(*args) raise NotImplementedError, "#{self.class} must implement #perform" end |