Class: BrainzLab::Nerve::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/brainzlab/nerve/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/brainzlab/nerve/client.rb', line 10

def initialize(config)
  @config = config
  @base_url = config.nerve_url || 'https://nerve.brainzlab.ai'
end

Instance Method Details

#delete_job(job_id) ⇒ Object

Delete a job



129
130
131
132
133
134
135
# File 'lib/brainzlab/nerve/client.rb', line 129

def delete_job(job_id)
  response = request(:delete, "/api/v1/jobs/#{job_id}")
  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPNoContent)
rescue StandardError => e
  log_error('delete_job', e)
  false
end

#get_queue(name) ⇒ Object

Get queue details



108
109
110
111
112
113
114
115
116
117
# File 'lib/brainzlab/nerve/client.rb', line 108

def get_queue(name)
  response = request(:get, "/api/v1/queues/#{CGI.escape(name)}")

  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('get_queue', e)
  nil
end

#list_jobs(queue: nil, status: nil, limit: 100) ⇒ Object

List recent jobs



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/brainzlab/nerve/client.rb', line 78

def list_jobs(queue: nil, status: nil, limit: 100)
  params = { limit: limit }
  params[:queue] = queue if queue
  params[:status] = status if status

  response = request(:get, '/api/v1/jobs', params: params)

  return [] unless response.is_a?(Net::HTTPSuccess)

  data = JSON.parse(response.body, symbolize_names: true)
  data[:jobs] || []
rescue StandardError => e
  log_error('list_jobs', e)
  []
end

#list_queuesObject

List queues



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/brainzlab/nerve/client.rb', line 95

def list_queues
  response = request(:get, '/api/v1/queues')

  return [] unless response.is_a?(Net::HTTPSuccess)

  data = JSON.parse(response.body, symbolize_names: true)
  data[:queues] || []
rescue StandardError => e
  log_error('list_queues', e)
  []
end

#provision(project_id:, app_name:) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/brainzlab/nerve/client.rb', line 157

def provision(project_id:, app_name:)
  response = request(
    :post,
    '/api/v1/projects/provision',
    body: { project_id: project_id, app_name: app_name },
    use_service_key: true
  )

  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPCreated)
rescue StandardError => e
  log_error('provision', e)
  false
end

#report_failure(job_class:, job_id:, queue:, error_class:, error_message:, backtrace: nil, **attributes) ⇒ Object

Report a job failure



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/brainzlab/nerve/client.rb', line 39

def report_failure(job_class:, job_id:, queue:, error_class:, error_message:, backtrace: nil, **attributes)
  response = request(
    :post,
    '/api/v1/jobs/failures',
    body: {
      job_class: job_class,
      job_id: job_id,
      queue: queue,
      error_class: error_class,
      error_message: error_message,
      backtrace: backtrace&.first(20),
      failed_at: Time.now.utc.iso8601(3),
      **attributes
    }
  )

  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPCreated)
rescue StandardError => e
  log_error('report_failure', e)
  false
end

#report_job(job_class:, job_id:, queue:, status:, started_at:, ended_at:, **attributes) ⇒ Object

Report a job execution



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/brainzlab/nerve/client.rb', line 16

def report_job(job_class:, job_id:, queue:, status:, started_at:, ended_at:, **attributes)
  response = request(
    :post,
    '/api/v1/jobs',
    body: {
      job_class: job_class,
      job_id: job_id,
      queue: queue,
      status: status,
      started_at: started_at.iso8601(3),
      ended_at: ended_at.iso8601(3),
      duration_ms: ((ended_at - started_at) * 1000).round(2),
      **attributes
    }
  )

  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPCreated)
rescue StandardError => e
  log_error('report_job', e)
  false
end

#report_metrics(queue:, size:, latency_ms: nil, workers: nil) ⇒ Object

Report queue metrics



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/brainzlab/nerve/client.rb', line 138

def report_metrics(queue:, size:, latency_ms: nil, workers: nil)
  response = request(
    :post,
    '/api/v1/metrics',
    body: {
      queue: queue,
      size: size,
      latency_ms: latency_ms,
      workers: workers,
      timestamp: Time.now.utc.iso8601(3)
    }
  )

  response.is_a?(Net::HTTPSuccess)
rescue StandardError => e
  log_error('report_metrics', e)
  false
end

#retry_job(job_id) ⇒ Object

Retry a failed job



120
121
122
123
124
125
126
# File 'lib/brainzlab/nerve/client.rb', line 120

def retry_job(job_id)
  response = request(:post, "/api/v1/jobs/#{job_id}/retry")
  response.is_a?(Net::HTTPSuccess)
rescue StandardError => e
  log_error('retry_job', e)
  false
end

#stats(queue: nil, job_class: nil, period: '1h') ⇒ Object

Get job statistics



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/brainzlab/nerve/client.rb', line 62

def stats(queue: nil, job_class: nil, period: '1h')
  params = { period: period }
  params[:queue] = queue if queue
  params[:job_class] = job_class if job_class

  response = request(:get, '/api/v1/stats', params: params)

  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
  log_error('stats', e)
  nil
end