Class: Desiru::AsyncResult
- Inherits:
-
Object
- Object
- Desiru::AsyncResult
- Defined in:
- lib/desiru/async_capable.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#job_id ⇒ Object
readonly
Returns the value of attribute job_id.
Instance Method Summary collapse
- #error ⇒ Object
- #failed? ⇒ Boolean
-
#initialize(job_id) ⇒ AsyncResult
constructor
A new instance of AsyncResult.
- #progress ⇒ Object
- #ready? ⇒ Boolean
- #result ⇒ Object
- #status ⇒ Object
- #success? ⇒ Boolean
- #wait(timeout: 60, poll_interval: 0.5) ⇒ Object
Constructor Details
#initialize(job_id) ⇒ AsyncResult
Returns a new instance of AsyncResult.
59 60 61 62 |
# File 'lib/desiru/async_capable.rb', line 59 def initialize(job_id) @job_id = job_id @redis = Redis.new(url: Desiru.configuration.redis_url || ENV.fetch('REDIS_URL', nil)) end |
Instance Attribute Details
#job_id ⇒ Object (readonly)
Returns the value of attribute job_id.
57 58 59 |
# File 'lib/desiru/async_capable.rb', line 57 def job_id @job_id end |
Instance Method Details
#error ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/desiru/async_capable.rb', line 88 def error data = fetch_result return nil unless data && !data[:success] { message: data[:error], class: data[:error_class] } end |
#failed? ⇒ Boolean
74 75 76 77 |
# File 'lib/desiru/async_capable.rb', line 74 def failed? result = fetch_result result && !result[:success] end |
#progress ⇒ Object
105 106 107 108 109 110 |
# File 'lib/desiru/async_capable.rb', line 105 def progress status_data = fetch_status return nil unless status_data status_data[:progress] end |
#ready? ⇒ Boolean
64 65 66 67 |
# File 'lib/desiru/async_capable.rb', line 64 def ready? result = fetch_result !result.nil? end |
#result ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/desiru/async_capable.rb', line 79 def result data = fetch_result return nil unless data raise ModuleError, "Async job failed: #{data[:error]}" unless data[:success] ModuleResult.new(data[:result], metadata: { async: true, job_id: job_id }) end |
#status ⇒ Object
98 99 100 101 102 103 |
# File 'lib/desiru/async_capable.rb', line 98 def status status_data = fetch_status return 'pending' unless status_data status_data[:status] || 'pending' end |
#success? ⇒ Boolean
69 70 71 72 |
# File 'lib/desiru/async_capable.rb', line 69 def success? result = fetch_result result && result[:success] end |
#wait(timeout: 60, poll_interval: 0.5) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/desiru/async_capable.rb', line 112 def wait(timeout: 60, poll_interval: 0.5) start_time = Time.now while Time.now - start_time < timeout return result if ready? sleep poll_interval end raise TimeoutError, "Async result not ready after #{timeout} seconds" end |