This repository has been archived by the owner on Sep 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
launch_core.rb
executable file
·145 lines (99 loc) · 3.9 KB
/
launch_core.rb
1
2
3
4
5
6
7
8
9
10
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env ruby
require 'fileutils'
require_relative './niasconfig'
=begin
Launcher of NIAS infrastructure. Must be run before ./launcher_nias.rb
Options:
* ./launcher_core.rb launches NIAS infrastructure
* ./launcher_core.rb -K shuts down NIAS infrastructure
Dependencies
Script creates a ./core.pid file storing the process ids of NIAS infrastructure services.
Launch presupposes the file does not exist, and creates it.
Shutdown presupposes the file does exist, and deletes it, after shutting down each process it references.
Functionality:
1. Creates /tmp/nias, with subdirectories redis, moneta. Subdirectory kafka-logs is created through Kafka config
2. Launches the following infrastructure service processes:
* Zookeeper
* Kafka
* Redis
* Rackup
3. Creates all the Kafka topics that are known in advance to be required. There is latency in auto-creating Kafka topics, which can lead to messages being dropped.
=end
puts "\n\nStarting in: #{__dir__}\n\n"
# create root node for data files in /tmp
FileUtils.mkdir '/tmp/nias' unless File.directory? '/tmp/nias'
# create storage area for redis - directory needs to pre-exist
FileUtils.mkdir '/tmp/nias/redis' unless File.directory? '/tmp/nias/redis'
# create working area for moneta key-value store - needs to pre-exist
FileUtils.mkdir '/tmp/nias/moneta' unless File.directory? '/tmp/nias/moneta'
def banner( text )
puts "\n\n********************************************************"
puts "**"
puts "** #{text}"
puts "**"
puts "**"
puts "********************************************************\n\n"
end
@pids = {}
@PID_FILE = 'core.pid'
def launch
config = NiasConfig.new()
puts config.get_host
# just in case an old one gets left behind, delete on startup
#File.delete( @pid_file ) if File.exist?( @pid_file )
# if an old pid file exists, abort, should be running -K instead
if ( File.exist?( @PID_FILE) ) then
puts "The file #{@PID_FILE} exists: run ./launch_core.rb -K to terminate any existing processes"
exit
end
# daemonise launched processes
Process.daemon( true, true )
banner 'Starting zookeeper'
@pids['zk'] = Process.spawn( './kafka/bin/zookeeper-server-start.sh', './kafka/config/zookeeper.properties' )
banner 'Waiting for ZK to come up'
sleep 5
banner 'Starting kafka'
@pids['kafka'] = Process.spawn( './kafka/bin/kafka-server-start.sh', './kafka/config/server.properties' )
banner 'Starting SMS Redis'
@pids['sms-redis'] = Process.spawn( 'redis-server', './sms/sms-redis.conf' )
=begin
# moving this to launch_nias.rb and launch_naplan.rb : the web services depend on kafka queues which have not been initiated yet
banner 'Starting Web Services'
# @pids['ssf'] = Process.spawn( 'ruby', './ssf/ssf_server.rb', '-e', 'production', '-p', '4567' )
@pids['web'] = Process.spawn( 'rackup' )
banner "Web services running on #{config.get_host}:#{config.get_sinatra_port}/"
=end
banner 'Kafka logs will be created under /tmp/nias/kafka'
banner 'Zookeeper logs will be created under /tmp/nias/zookeeper'
banner 'Redis backups will be created under /tmp/nias/redis'
banner 'SIF Key Value store will be created under /tmp/nias/moneta'
File.open(@PID_FILE, 'w') {|f|
f.puts "#{@pids['kafka']}"
f.puts "#{@pids['zk']}"
f.puts "#{@pids['sms-redis']}"
#f.puts "#{@pids['web']}"
}
banner "pid file written to #{@PID_FILE}"
banner 'Core NIAS services are up.'
end
def shut_down
banner "\n Core Services shutting down...\n\n"
File.readlines( @PID_FILE ).each do |line|
begin
Process.kill :INT, line.chomp.to_i
sleep 2
rescue Exception => e
puts e.message
puts e.backtrace.inspect
end
end
File.delete( @PID_FILE ) if File.exist?( @PID_FILE )
banner "All core services shut down"
end
if ARGV[0] == '-K' then
shut_down
exit 130
else
launch
exit 130
end