Class: Cline::Server

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket_file) ⇒ Server

Returns a new instance of Server.



57
58
59
60
# File 'lib/cline/server.rb', line 57

def initialize(socket_file)
  @socket_file = socket_file
  @server      = UNIXServer.new(@socket_file.to_s)
end

Class Method Details

.cleanObject



31
32
33
34
# File 'lib/cline/server.rb', line 31

def clean
  File.unlink pid_file
  File.unlink socket_file
end

.client_process?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/cline/server.rb', line 40

def client_process?
  Process.pid != pid
end

.pidObject



44
45
46
# File 'lib/cline/server.rb', line 44

def pid
  Integer(pid_file.read)
end

.pid_fileObject



48
49
50
# File 'lib/cline/server.rb', line 48

def pid_file
  Pathname.new(Cline.cline_dir).join('cline.pid')
end

.running?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/cline/server.rb', line 36

def running?
  socket_file.exist?
end

.socket_fileObject



52
53
54
# File 'lib/cline/server.rb', line 52

def socket_file
  Pathname.new(Cline.cline_dir).join('cline.sock')
end

.startObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cline/server.rb', line 11

def start
  raise %(Socket file #{socket_file} already exists.) if running?

  Process.daemon

  pid_file.open 'w' do |pid|
    pid.write Process.pid
  end

  Signal.trap(:KILL) { Server.clean }

  new(socket_file).run
end

.stopObject



25
26
27
28
29
# File 'lib/cline/server.rb', line 25

def stop
  raise %(Server isn't running) unless running?

  Process.kill :TERM, pid
end

Instance Method Details

#runObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cline/server.rb', line 62

def run
  invoke_jobs

  loop do
    Thread.fork @server.accept do |socket|
      request = socket.recv(120)

      process socket, JSON.parse(request)

      socket.close

      GC.start
    end
  end
ensure
  Server.clean
end