Class: Docscribe::Server::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/docscribe/server/daemon.rb

Overview

Daemon process that loads the Ruby runtime once and serves requests.

Constant Summary collapse

ERROR_CODES =

Standardized JSON-RPC error codes.

{
  gem_not_found: -32_000,
  syntax_error: -32_001,
  config_load_failure: -32_002,
  timeout: -32_010,
  internal: -32_099
}.freeze
REQUEST_HANDLERS =
{
  'check' => :handle_check,
  'fix' => :handle_fix,
  'check_batch' => :handle_check_batch,
  'update_types' => :handle_update_types
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(socket_path: nil, idle_timeout: IDLE_TIMEOUT, config_path: nil) ⇒ void

Parameters:

  • socket_path (String?) (defaults to: nil) —

    custom socket path

  • idle_timeout (Integer) (defaults to: IDLE_TIMEOUT) —

    seconds before automatic shutdown

  • config_path (String?) (defaults to: nil) —

    custom config path



34
35
36
37
38
# File 'lib/docscribe/server/daemon.rb', line 34

def initialize(socket_path: nil, idle_timeout: IDLE_TIMEOUT, config_path: nil)
  setup_socket_config(socket_path, config_path, idle_timeout)
  setup_runtime_state
  setup_caches
end

Instance Method Details

#setup_caches ⇒ void

This method returns an undefined value.



60
61
62
63
64
# File 'lib/docscribe/server/daemon.rb', line 60

def setup_caches
  @file_cache = LRUCache.new
  @cache_mutex = Mutex.new
  @config_mutex = Mutex.new
end

#setup_runtime_state ⇒ void

This method returns an undefined value.



51
52
53
54
55
56
57
# File 'lib/docscribe/server/daemon.rb', line 51

def setup_runtime_state
  @last_request_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @running = false
  @server = nil
  @started_at = Time.now
  @last_sig_hash = nil
end

#setup_socket_config(socket_path, config_path, idle_timeout) ⇒ void

This method returns an undefined value.

Parameters:

  • socket_path (String?)
  • config_path (String?)
  • idle_timeout (Integer)


44
45
46
47
48
# File 'lib/docscribe/server/daemon.rb', line 44

def setup_socket_config(socket_path, config_path, idle_timeout)
  @socket_path = socket_path || Server.socket_path(config_path)
  @config_path = config_path
  @idle_timeout = idle_timeout
end

#start ⇒ void

This method returns an undefined value.

Start the daemon: load dependencies, bind socket, enter listen loop.



69
70
71
72
73
74
75
76
# File 'lib/docscribe/server/daemon.rb', line 69

def start
  load_dependencies
  setup_socket
  @running = true
  $PROGRAM_NAME = "docscribe server (#{Dir.pwd})"
  write_pid
  listen_loop
end