Skip to content

Commit 993a464

Browse files
committed
started working on internal supervisor
1 parent 11681a7 commit 993a464

File tree

11 files changed

+113
-6
lines changed

11 files changed

+113
-6
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: ruby
22
rvm:
3-
- 2.6.3
3+
- 2.3.8
44
before_install:
55
- gem update --system
66
- gem install bundler

Gemfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
source 'http://rubygems.org'
22

3+
ruby '2.3.8'
4+
35
gem "ansi", "~> 1.5"
46
gem 'bson', '~> 3.0'
57
gem 'git', '~> 1.2'
6-
gem 'nutella_lib','~>0.4', '>=0.4.24'
8+
gem "grpc", "~> 1.24"
9+
gem 'nutella_lib','~>0.5'
710
gem 'nokogiri', '~>1.6'
811
gem 'slop', '~>4.0'
912
gem 'semantic', '~> 1.4'
@@ -17,6 +20,7 @@ group :development do
1720
gem 'rdoc', '~> 4.0'
1821
gem 'bundler', '~> 2.0'
1922
gem 'jeweler', '~> 2.3'
23+
gem "grpc-tools", "~> 1.24"
2024
end
2125

2226
group :test do

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Jeweler::Tasks.new do |gem|
2121
gem.description = %Q{utella is a framework to create and run RoomApps}
2222
gem.email = "[email protected]"
2323
gem.authors = ["Alessandro Gnoli"]
24+
gem.required_ruby_version = "~> 2.0"
2425
# dependencies defined in Gemfile
2526
end
2627
Jeweler::RubygemsDotOrgTasks.new

lib/bots/commands_server/commands_server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Command server
22
# Connects to MQTT broker and listens for commands over (RPC)
33
# Executes the commands and returns the output
4-
# require 'nutella_lib'
4+
require 'nutella_lib'
55
Dir["#{File.dirname(__FILE__)}/commands/*.rb"].each do |file|
66
require_relative "commands/#{File.basename(file, File.extname(file))}"
77
end

lib/supervisor/supervisor.proto

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
syntax = "proto3";
2+
3+
service Supervisor {
4+
rpc StartProcess (StartProcessRequest) returns (StartProcessResponse);
5+
}
6+
7+
message StartProcessRequest {
8+
string process_name = 1;
9+
string process_command = 2;
10+
string process_log = 3;
11+
}
12+
13+
message StartProcessResponse {
14+
bool success = 1;
15+
string process_pid = 2;
16+
}

lib/supervisor/supervisor.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Super-simple process supervisor
2+
3+
require_relative 'supervisor_services_pb'
4+
5+
class SupervisorServer < Supervisor::Service
6+
# say_hello implements the SayHello rpc method.
7+
def start_process(start_process_req, _unused_call)
8+
puts "Someone requested to start a process!!! #{start_process_req}"
9+
res = StartProcessResponse.new(success: true)
10+
puts "Responding with: #{res}"
11+
res
12+
end
13+
end
14+
15+
# Everything that is not an SIGINT (2), SIGTERM (15),
16+
# or a hard SIGKILL (9) (and a SIGSTOP(19) I guess...)
17+
# will trigger a process restart
18+
begin
19+
# Listen for commands over GRPC
20+
# Start and stop processes
21+
# Persist after each operation
22+
s = GRPC::RpcServer.new
23+
s.add_http2_port('0.0.0.0:50051', :this_port_is_insecure)
24+
s.handle(SupervisorServer)
25+
s.run_till_terminated_or_interrupted([1, 'int', 'SIGQUIT'])
26+
rescue SignalException => e
27+
# Legit termination, exit
28+
rescue
29+
# everything else... restart
30+
end

lib/supervisor/supervisor_pb.rb

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/supervisor/supervisor_services_pb.rb

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/supervisor/test_client.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'grpc'
2+
require_relative 'supervisor_services_pb'
3+
4+
stub = Supervisor::Stub.new('localhost:50051', :this_channel_is_insecure)
5+
res = stub.start_process(StartProcessRequest.new({process_name: "p123", process_log: "abc"}))
6+
puts "Start process? #{res}"

lib/util/supervisor.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ def initialize
1919
# Adds a process to supervision
2020
def add(name, command)
2121
write_config_file(name, command)
22-
@rpc.call("supervisor.reloadConfig")
23-
@rpc.call("supervisor.addProcessGroup", name)
22+
begin
23+
@rpc.call("supervisor.addProcessGroup", name)
24+
rescue => exception
25+
# Yup, we're swallowing this one because it returns true if it adds it and
26+
# exceptions out if it can't... ^___^
27+
end
2428
end
2529

2630
# Adds a group of process to supervision
@@ -56,6 +60,11 @@ def stop(name)
5660
@rpc.call("supervisor.stopProcess", name)
5761
end
5862

63+
# Gets all the info about a process
64+
def getInfo(name)
65+
@rpc.call("supervisor.getProcessInfo", name)
66+
end
67+
5968

6069
private
6170

0 commit comments

Comments
 (0)