Class: RailsDevMCP::ProcessManager
- Inherits:
-
Object
- Object
- RailsDevMCP::ProcessManager
- Defined in:
- lib/rails_dev_mcp/process_manager.rb
Instance Attribute Summary collapse
-
#log_file ⇒ Object
readonly
Returns the value of attribute log_file.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#initialize(rails_root:, port: 3000) ⇒ ProcessManager
constructor
A new instance of ProcessManager.
- #logs(lines: 50) ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
- #status ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(rails_root:, port: 3000) ⇒ ProcessManager
Returns a new instance of ProcessManager.
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rails_dev_mcp/process_manager.rb', line 10 def initialize(rails_root:, port: 3000) @rails_root = rails_root @port = port @log_file = File.join(rails_root, 'tmp', 'rails-dev-mcp.log') @pid_file = File.join(rails_root, 'tmp', 'pids', 'rails-dev-mcp.pid') @pid = nil @stdin = nil @stdout = nil @stderr = nil @wait_thread = nil ensure_directories end |
Instance Attribute Details
#log_file ⇒ Object (readonly)
Returns the value of attribute log_file.
8 9 10 |
# File 'lib/rails_dev_mcp/process_manager.rb', line 8 def log_file @log_file end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
8 9 10 |
# File 'lib/rails_dev_mcp/process_manager.rb', line 8 def pid @pid end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
8 9 10 |
# File 'lib/rails_dev_mcp/process_manager.rb', line 8 def port @port end |
Instance Method Details
#logs(lines: 50) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rails_dev_mcp/process_manager.rb', line 83 def logs(lines: 50) return { success: false, error: 'Log file not found' } unless File.exist?(@log_file) begin log_content = `tail -n #{lines} #{@log_file}` # Extract notable events (errors, warnings, requests) notable_lines = log_content.lines.select do |line| line =~ /ERROR|WARN|FATAL|Started|Completed|ActionController::RoutingError|ActiveRecord::|NoMethodError|NameError|SyntaxError/ end { success: true, full_logs: log_content, notable_events: notable_lines.join, log_file: @log_file } rescue StandardError => e { success: false, error: e. } end end |
#running? ⇒ Boolean
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rails_dev_mcp/process_manager.rb', line 123 def running? return false unless @pid || read_pid_file begin Process.kill(0, @pid || read_pid_file) true rescue Errno::ESRCH false end end |
#start ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rails_dev_mcp/process_manager.rb', line 24 def start return { success: false, error: 'Server already running' } if running? # Start Rails server with output captured cmd = "rails server -p #{@port}" @stdin, @stdout, @stderr, @wait_thread = Open3.popen3( cmd, chdir: @rails_root, err: [:child, :out] # Combine stderr into stdout ) @pid = @wait_thread.pid write_pid_file(@pid) # Start log capture thread Thread.new do File.open(@log_file, 'a') do |log| @stdout.each_line do |line| log.puts(line) log.flush end end end # Wait a moment to check if it started successfully sleep 2 if running? { success: true, message: "Rails server started on port #{@port}", pid: @pid } else { success: false, error: 'Failed to start Rails server' } end rescue StandardError => e { success: false, error: e. } end |
#status ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rails_dev_mcp/process_manager.rb', line 105 def status if running? { running: true, pid: @pid, port: @port, url: "http://localhost:#{@port}", log_file: @log_file } else { running: false, port: @port, log_file: @log_file } end end |
#stop ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rails_dev_mcp/process_manager.rb', line 61 def stop return { success: false, error: 'Server not running' } unless running? begin Process.kill('TERM', @pid) @wait_thread&.join(5) # Wait up to 5 seconds for graceful shutdown if running? Process.kill('KILL', @pid) # Force kill if still running end cleanup { success: true, message: 'Rails server stopped' } rescue Errno::ESRCH # Process already dead cleanup { success: true, message: 'Rails server was already stopped' } rescue StandardError => e { success: false, error: e. } end end |