Module: NatsWork::Rails::ConsoleHelpers

Included in:
Rails::Console
Defined in:
lib/natswork/rails/console_helpers.rb

Class Method Summary collapse

Class Method Details

.benchmark_job(job_class, *args, count: 100) ⇒ Object

Benchmark job execution



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/natswork/rails/console_helpers.rb', line 148

def benchmark_job(job_class, *args, count: 100)
  job = job_class.is_a?(Class) ? job_class : job_class.constantize

  require 'benchmark'

  time = Benchmark.realtime do
    count.times do
      job.perform_async(*args)
    end
  end

  rate = count / time
  puts "Enqueued #{count} jobs in #{time.round(3)} seconds"
  puts "Rate: #{rate.round(2)} jobs/second"

  { time: time, count: count, rate: rate }
end

.cancel_job(job_id) ⇒ Object

Cancel a job



85
86
87
88
89
# File 'lib/natswork/rails/console_helpers.rb', line 85

def cancel_job(job_id)
  Client.instance.cancel_job(job_id)
  puts "Cancellation message sent for job #{job_id}"
  true
end

.clear_resultsObject

Clear all results



64
65
66
67
68
69
70
# File 'lib/natswork/rails/console_helpers.rb', line 64

def clear_results
  client = Client.instance
  count = client.instance_variable_get(:@result_store).size
  client.instance_variable_set(:@result_store, {})
  client.instance_variable_set(:@result_expiration_times, {})
  puts "Cleared #{count} job results"
end

.debug(enabled = true) ⇒ Object

Enable/disable debug logging



105
106
107
108
109
# File 'lib/natswork/rails/console_helpers.rb', line 105

def debug(enabled = true)
  logger = enabled ? Logger.new($stdout) : nil
  Client.instance.configuration.logger = logger
  puts "Debug logging #{enabled ? 'enabled' : 'disabled'}"
end

.health_checkObject

Perform a health check



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/natswork/rails/console_helpers.rb', line 112

def health_check
  client = Client.instance
  pool = client.connection_pool

  if pool.nil?
    puts '❌ Connection pool not initialized'
    return false
  end

  healthy = pool.healthy?
  puts healthy ? '✅ System healthy' : '❌ System unhealthy'

  # Check individual connections
  pool.with_connection do |conn|
    if conn.connected?
      puts '✅ NATS connection active'
    else
      puts '❌ NATS connection inactive'
    end
  end

  healthy
rescue StandardError => e
  puts "❌ Health check failed: #{e.message}"
  false
end

.job_result(job_id) ⇒ Object

Check job result



59
60
61
# File 'lib/natswork/rails/console_helpers.rb', line 59

def job_result(job_id)
  Client.instance.job_status(job_id)
end

.jobsObject

List all registered jobs



9
10
11
# File 'lib/natswork/rails/console_helpers.rb', line 9

def jobs
  Registry.instance.all_jobs
end

.memory_usageObject

Show memory usage



140
141
142
143
144
145
# File 'lib/natswork/rails/console_helpers.rb', line 140

def memory_usage
  kb = `ps -o rss= -p #{Process.pid}`.to_i
  mb = kb / 1024.0
  puts "Memory usage: #{mb.round(2)} MB"
  mb
end

.pool_statsObject

Show connection pool statistics



99
100
101
102
# File 'lib/natswork/rails/console_helpers.rb', line 99

def pool_stats
  pool = Client.instance.connection_pool
  pool&.stats || {}
end

.queue_jobs(queue_name) ⇒ Object

Show jobs for a specific queue



14
15
16
# File 'lib/natswork/rails/console_helpers.rb', line 14

def queue_jobs(queue_name)
  Registry.instance.jobs_for_queue(queue_name)
end

.queuesObject

Show all queues



19
20
21
# File 'lib/natswork/rails/console_helpers.rb', line 19

def queues
  Registry.instance.all_queues
end

.reconnect!Object

Reconnect to NATS



92
93
94
95
96
# File 'lib/natswork/rails/console_helpers.rb', line 92

def reconnect!
  Client.instance.reset_connection!
  puts 'Reconnected to NATS'
  true
end

.run_job(job_class, *args) ⇒ Object

Execute job synchronously (for testing)



50
51
52
53
54
55
56
# File 'lib/natswork/rails/console_helpers.rb', line 50

def run_job(job_class, *args)
  job = job_class.is_a?(Class) ? job_class : job_class.constantize
  instance = job.new
  result = instance.perform(*args)
  puts "Job completed with result: #{result.inspect}"
  result
end

.scheduled_jobsObject

Show scheduled jobs



73
74
75
76
77
78
79
80
81
82
# File 'lib/natswork/rails/console_helpers.rb', line 73

def scheduled_jobs
  Client.instance.instance_variable_get(:@scheduled_jobs).map do |job|
    {
      job_id: job[:message].job_id,
      job_class: job[:message].job_class,
      scheduled_for: job[:scheduled_for],
      status: job[:status]
    }
  end
end

.statusObject

Get client status



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/natswork/rails/console_helpers.rb', line 24

def status
  client = Client.instance
  pool = client.connection_pool

  {
    connected: pool&.healthy? || false,
    pool_size: pool&.size || 0,
    active_connections: pool&.active_connections || 0,
    available_connections: pool&.available_connections || 0,
    configuration: {
      servers: client.configuration.servers,
      namespace: client.configuration.namespace,
      use_jetstream: client.configuration.use_jetstream
    }
  }
end

.test_job(job_class, *args) ⇒ Object

Test job execution



42
43
44
45
46
47
# File 'lib/natswork/rails/console_helpers.rb', line 42

def test_job(job_class, *args)
  job = job_class.is_a?(Class) ? job_class : job_class.constantize
  job_id = job.perform_async(*args)
  puts "Job #{job_id} enqueued to queue '#{job.get_queue}'"
  job_id
end