Class: RuboCop::Server::ClientCommand::Start

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/service/patch/server/client_command/start.rb

Instance Method Summary collapse

Instance Method Details

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
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
# File 'lib/rubocop/service/patch/server/client_command/start.rb', line 11

def run
  if Server.running?
    warn "RuboCop server (#{Cache.pid_path.read}) is already running."
    return
  end

  if ENV["RUBOCOP_SERVICE_SERVER_PROCESS"] == "true"
    Cache.acquire_lock do |locked|
      unless locked
        # Another process is already starting server,
        # so wait for it to be ready.
        Server.wait_for_running_status!(true)
        exit 0
      end

      Cache.write_version_file(RuboCop::Version::STRING)

      host = ENV.fetch("RUBOCOP_SERVER_HOST", "127.0.0.1")
      port = ENV.fetch("RUBOCOP_SERVER_PORT", 0)

      Server::Core.new.start(host, port)
    end
  else
    exit_code =
      RuboCop::Service::Server.connect do |connection|
        connection.puts JSON.generate(
                          { type: :spawn, directory: Dir.pwd }
                        )
      end
    if exit_code.nil?
      warn(
        "\nConnection closed without exit code. " \
          "Please check the server log: #{File.expand_path("~/.rubocop-service.log")}"
      )
      exit 1
    else
      exit exit_code.to_i
    end
  end
end