Class: Rays::Service::ApplicationService

Inherits:
Object
  • Object
show all
Defined in:
lib/rays/services/application_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, host, port, start_script, debug_script, stop_script, log_file, remote, path, deploy) ⇒ ApplicationService

Returns a new instance of ApplicationService.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rays/services/application_service.rb', line 28

def initialize(name, host, port, start_script, debug_script, stop_script, log_file, remote, path, deploy)
  @name = name
  @host = host
  @port = port
  @start_script = start_script
  @debug_script = debug_script
  @stop_script = stop_script
  @log_file = log_file
  @remote = remote
  @path = path
  @deploy = deploy
end

Instance Method Details

#alive?Boolean

Is the service running? Returns true is the service is running or false otherwise

Returns:

  • (Boolean)


135
136
137
# File 'lib/rays/services/application_service.rb', line 135

def alive?
  Utils::NetworkUtils.port_open?(@host, @port)
end

#debugObject

Debug service



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rays/services/application_service.rb', line 72

def debug
  if @debug_script.nil?
    $log.warn('debug is disabled for this server')
    return
  end
  unless alive?
    execute @debug_script
    log unless remote?
  else
    raise RaysException.new('service is already running')
  end
end

#deployObject

Raises:



56
57
58
59
# File 'lib/rays/services/application_service.rb', line 56

def deploy
  raise RaysException.new(missing_environment_option('service', 'deploy')) if @deploy.nil?
  @deploy
end

#hostObject

Raises:



41
42
43
44
# File 'lib/rays/services/application_service.rb', line 41

def host
  raise RaysException.new(missing_environment_option('service', 'host')) if @host.nil?
  @host
end

#logObject

Show logs (live)



116
117
118
119
120
121
122
123
124
125
# File 'lib/rays/services/application_service.rb', line 116

def log
  if remote?
    @remote.loop_exec("tail -f #{@log_file}")
  else
    Thread.new do
      exec("tail -f #{@log_file}")
    end
    $log.info("Following logs of #{@name} service on #{@host}.\nUse ctrl+c to interrupt.")
  end
end

#pathObject

Raises:



51
52
53
54
# File 'lib/rays/services/application_service.rb', line 51

def path
  raise RaysException.new(missing_environment_option('service', 'path')) if @path.nil?
  @path
end

#portObject

Raises:



46
47
48
49
# File 'lib/rays/services/application_service.rb', line 46

def port
  raise RaysException.new(missing_environment_option('service', 'port')) if @port.nil?
  @port
end

#remote?Boolean

Is the service resides on a remote service? returns true if the service is on a remote server and false if the service is local.

Returns:

  • (Boolean)


129
130
131
# File 'lib/rays/services/application_service.rb', line 129

def remote?
  !@remote.nil?
end

#restart_debugObject

restart in debug mode



109
110
111
112
113
# File 'lib/rays/services/application_service.rb', line 109

def restart_debug
  restart do
    debug
  end
end

#restart_normalObject

normal restart



102
103
104
105
106
# File 'lib/rays/services/application_service.rb', line 102

def restart_normal
  restart do
    start
  end
end

#startObject

Start service



62
63
64
65
66
67
68
69
# File 'lib/rays/services/application_service.rb', line 62

def start
  unless alive?
    execute @start_script
    log unless remote?
  else
    raise RaysException.new('service is already running')
  end
end

#stopObject

Stop service

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rays/services/application_service.rb', line 86

def stop
  raise RaysException.new('service is not running') if not alive?

  execute @stop_script
  10.times do
    break if not alive?
    sleep(1)
  end

  if alive?
    execute ("kill -9 `lsof -t -i tcp:#{@port}`")
    sleep(2)
  end
end