Class: Desiru::AsyncResult

Inherits:
Object
  • Object
show all
Defined in:
lib/desiru/async_capable.rb

Direct Known Subclasses

BatchResult

Instance Attribute Summary collapse

Instance Method Summary collapse

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_idObject (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

#errorObject



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

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/desiru/async_capable.rb', line 74

def failed?
  result = fetch_result
  result && !result[:success]
end

#progressObject



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

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/desiru/async_capable.rb', line 64

def ready?
  result = fetch_result
  !result.nil?
end

#resultObject

Raises:



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

#statusObject



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

Returns:

  • (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

Raises:



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