Class: Consolle::Adapters::RailsConsole

Inherits:
Object
  • Object
show all
Defined in:
lib/consolle/adapters/rails_console.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket_path: nil, pid_path: nil, log_path: nil, rails_root: nil, rails_env: nil, verbose: false, command: nil, wait_timeout: nil) ⇒ RailsConsole

Returns a new instance of RailsConsole.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/consolle/adapters/rails_console.rb', line 15

def initialize(socket_path: nil, pid_path: nil, log_path: nil, rails_root: nil, rails_env: nil, verbose: false,
               command: nil, wait_timeout: nil)
  @socket_path = socket_path || default_socket_path
  @pid_path = pid_path || default_pid_path
  @log_path = log_path || default_log_path
  @rails_root = rails_root || Dir.pwd
  @rails_env = rails_env || 'development'
  @verbose = verbose
  @command = command || 'bin/rails console'
  @wait_timeout = wait_timeout || Consolle::DEFAULT_WAIT_TIMEOUT
  @server_pid = nil
end

Instance Attribute Details

#log_pathObject (readonly)

Returns the value of attribute log_path.



13
14
15
# File 'lib/consolle/adapters/rails_console.rb', line 13

def log_path
  @log_path
end

#pid_pathObject (readonly)

Returns the value of attribute pid_path.



13
14
15
# File 'lib/consolle/adapters/rails_console.rb', line 13

def pid_path
  @pid_path
end

#process_pidObject (readonly)

Returns the value of attribute process_pid.



13
14
15
# File 'lib/consolle/adapters/rails_console.rb', line 13

def process_pid
  @process_pid
end

#socket_pathObject (readonly)

Returns the value of attribute socket_path.



13
14
15
# File 'lib/consolle/adapters/rails_console.rb', line 13

def socket_path
  @socket_path
end

Instance Method Details

#get_statusObject



111
112
113
114
115
116
117
118
119
120
# File 'lib/consolle/adapters/rails_console.rb', line 111

def get_status
  request = {
    'action' => 'status',
    'request_id' => SecureRandom.uuid
  }

  send_request(request, timeout: 5)
rescue StandardError
  nil
end

#restartObject



55
56
57
58
59
# File 'lib/consolle/adapters/rails_console.rb', line 55

def restart
  stop
  sleep 1
  start
end

#running?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
# File 'lib/consolle/adapters/rails_console.rb', line 61

def running?
  return false unless File.exist?(@socket_path)

  # Check if socket is responsive
  status = get_status
  status && status['success'] && status['running']
rescue StandardError
  false
end

#send_code(code, timeout: 60) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/consolle/adapters/rails_console.rb', line 81

def send_code(code, timeout: 60)
  raise 'Console is not running' unless running?

  request = {
    'action' => 'eval',
    'code' => code,
    'timeout' => timeout,
    'request_id' => SecureRandom.uuid
  }

  response = send_request(request, timeout: timeout + 5)

  # Format response for compatibility
  if response['success']
    {
      'success' => true,
      'result' => response['result'],
      'execution_time' => response['execution_time'],
      'request_id' => response['request_id']
    }
  else
    {
      'success' => false,
      'error' => response['error'] || 'Unknown',
      'message' => response['message'] || 'Unknown error',
      'request_id' => response['request_id']
    }
  end
end

#startObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/consolle/adapters/rails_console.rb', line 28

def start
  return false if running?

  # Start socket server daemon
  start_server_daemon

  # Wait for server to be ready
  wait_for_server(timeout: @wait_timeout)

  # Get server status
  status = get_status
  @server_pid = status['pid'] if status && status['success']

  true
rescue StandardError => e
  stop_server_daemon
  raise e
end

#stopObject



47
48
49
50
51
52
53
# File 'lib/consolle/adapters/rails_console.rb', line 47

def stop
  return false unless running?

  stop_server_daemon
  @server_pid = nil
  true
end