Skip to content

Commit 25c6eaa

Browse files
committed
Revert "Started migration to AMQP."
This reverts commit ed74c35.
1 parent 4d82a48 commit 25c6eaa

File tree

8 files changed

+116
-216
lines changed

8 files changed

+116
-216
lines changed

bin/smartguard

+59-61
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,85 @@
11
#!/usr/bin/env ruby
22

3-
require "bundler/setup"
4-
53
require 'smartguard'
64
require 'trollop'
75
require 'colored'
86

97
def start_application(application, opts, &block)
108

11-
EventMachine.run do
12-
Smartguard::Logging.logger.info "Starting application"
9+
Smartguard::Logging.logger.info "Starting application"
1310

14-
unless opts[:pid].nil?
15-
begin
16-
File.open(opts[:pid], "w") do |io|
17-
io.write Process.pid.to_s
18-
end
19-
rescue => e
20-
Smartguard::Logging.logger.error "Unable to write pidfile: #{e}"
11+
unless opts[:pid].nil?
12+
begin
13+
File.open(opts[:pid], "w") do |io|
14+
io.write Process.pid.to_s
2115
end
16+
rescue => e
17+
Smartguard::Logging.logger.error "Unable to write pidfile: #{e}"
2218
end
19+
end
2320

24-
application = application.new opts[:path], opts[:broker]
21+
application = application.new opts[:path]
2522

26-
signal_handler = ->(signal) do
27-
begin
28-
if Smartguard.shutting_down
29-
Smartguard::Logging.logger.info "Okay, okay."
30-
else
31-
Smartguard::Logging.logger.info "Catched signal #{signal}, shutting down"
23+
signal_handler = ->(signal) do
24+
begin
25+
if Smartguard.shutting_down
26+
Smartguard::Logging.logger.info "Okay, okay."
27+
else
28+
Smartguard::Logging.logger.info "Catched signal #{signal}, shutting down"
29+
30+
Smartguard.shutting_down = true
31+
Smartguard::Logging.logger.info "Stopping services"
32+
application.stop_services
33+
end
3234

33-
Smartguard.shutting_down = true
34-
Smartguard::Logging.logger.info "Stopping services"
35-
application.stop_services
36-
end
35+
rescue => e
36+
Smartguard::Logging.logger.error "Shutdown failed: #{e}"
3737

38-
rescue => e
39-
Smartguard::Logging.logger.error "Shutdown failed: #{e}"
38+
ensure
39+
exit 1
40+
end
41+
end
4042

41-
ensure
42-
exit 1
43-
end
43+
trap :HUP, signal_handler
44+
trap :INT, signal_handler
45+
trap :QUIT, signal_handler
46+
trap :PIPE, "IGNORE"
47+
trap :TERM, signal_handler
48+
Smartguard::ProcessManager.init
49+
50+
at_exit do
51+
begin
52+
Smartguard::Logging.logger.info "Killing any remaining processes"
53+
ensure
54+
Process.kill :KILL, 0
4455
end
56+
end
4557

46-
trap :HUP, signal_handler
47-
trap :INT, signal_handler
48-
trap :QUIT, signal_handler
49-
trap :PIPE, "IGNORE"
50-
trap :TERM, signal_handler
51-
Smartguard::ProcessManager.init
52-
53-
at_exit do
54-
begin
55-
Smartguard::Logging.logger.info "Killing any remaining processes"
56-
ensure
57-
Process.kill :KILL, 0
58-
end
58+
Smartguard::Logging.logger.info "Starting services"
59+
60+
begin
61+
application.start_services do
62+
Smartguard::Logging.logger.error "Startup failed, cleaning up and exiting"
63+
application.stop_services
64+
exit 1
65+
end
66+
rescue => e
67+
Smartguard::Logging.logger.error "Exception catched during service startup: #{e}"
68+
e.backtrace.each do |line|
69+
Smartguard::Logging.logger.error line
5970
end
6071

61-
EventMachine.defer do
62-
Smartguard::Logging.logger.info "Starting services"
72+
exit 1
73+
end
6374

64-
begin
65-
application.start do
66-
Smartguard::Logging.logger.error "Startup failed, cleaning up and exiting"
67-
application.stop
68-
exit 1
69-
end
70-
rescue => e
71-
Smartguard::Logging.logger.error "Exception catched during service startup: #{e}"
72-
e.backtrace.each do |line|
73-
Smartguard::Logging.logger.error line
74-
end
75+
Smartguard::Logging.logger.info "Services started"
7576

76-
exit 1
77-
end
77+
DRb.start_service("druby://localhost:#{opts[:port]}", application)
78+
Smartguard::Logging.logger.info "Smartguard ready"
7879

79-
Smartguard::Logging.logger.info "Services started"
80-
Smartguard::Logging.logger.info "Smartguard ready"
80+
yield if block_given?
8181

82-
yield if block_given?
83-
end
84-
end
82+
DRb.thread.join
8583
end
8684

8785
trap :TTIN do
@@ -100,7 +98,7 @@ opts = Trollop.options do
10098

10199
opt :app, "Application to use", type: String
102100
opt :path, "Path to application", type: String
103-
opt :broker, "AMQP broker", default: "amqp://guest:[email protected]:5672"
101+
opt :port, "Port to run DRB", default: 10000
104102
opt :pid, "Pid file name", type: String
105103
opt :log, "Log file name", type: String
106104
opt :development, "Run in the development environment"

bin/smartguardctl

+28-83
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,60 @@
11
#!/usr/bin/env ruby
22

3-
require "bundler/setup"
4-
53
require "trollop"
6-
require "eventmachine"
7-
require "json"
8-
require "amqp"
4+
require "smartguard"
95

106
module CommandHandler
11-
def self.cmd_start
12-
[ [ "start" ], nil ]
7+
def self.cmd_start(drb)
8+
status = drb.start_services
139
end
1410

15-
def self.cmd_stop
16-
[ [ "stop" ], nil ]
11+
def self.cmd_stop(drb)
12+
drb.stop_services
1713
end
1814

19-
def self.cmd_restart
20-
[ [ "restart" ], nil ]
15+
def self.cmd_restart(drb)
16+
drb.restart
2117
end
2218

23-
def self.cmd_status
24-
done = ->(status) do
25-
status.each do |service, (pid, active)|
26-
printf "%-16s: ", service
19+
def self.cmd_status(drb)
20+
status = drb.status
21+
22+
status.each do |service, (pid, active)|
23+
printf "%-16s: ", service
2724

28-
if active
29-
puts "running, pid #{pid}"
30-
else
31-
puts "stopped"
32-
end
25+
if active
26+
puts "running, pid #{pid}"
27+
else
28+
puts "stopped"
3329
end
3430
end
35-
36-
[ [ "status" ], done ]
3731
end
3832

39-
def self.cmd_switch_release
33+
def self.cmd_switch_release(drb)
4034
if ARGV.empty?
4135
warn "release must be specified"
4236
exit 1
4337
end
4438

4539
release = ARGV.shift
46-
47-
done = ->(success) do
48-
if success
49-
puts "releases switched"
50-
else
51-
warn "switch failed"
52-
exit 1
53-
end
40+
status = drb.switch_release release
41+
if status
42+
puts "releases switched"
43+
else
44+
warn "switch failed"
45+
exit 1
5446
end
55-
56-
[ [ "switch_release", release ], done ]
5747
end
5848
end
5949

6050
opts = Trollop.options do
61-
version "Smartguard CLI"
62-
opt :broker, "AMQP broker", default: "amqp://guest:[email protected]:5672"
51+
version "Smartguard CLI #{Smartguard::VERSION}"
52+
opt :port, "DRB Port", default: 10000
6353
stop_on_unknown
6454
end
6555

56+
drb = DRb::DRbObject.new_with_uri "druby://localhost:#{opts[:port]}"
57+
6658
if ARGV.empty?
6759
warn "command must be specified"
6860
exit 1
@@ -75,51 +67,4 @@ unless CommandHandler.respond_to? cmd
7567
exit 1
7668
end
7769

78-
EventMachine.run do
79-
amqp_connection = AMQP.connect opts[:broker]
80-
amqp_channel = AMQP::Channel.new amqp_connection
81-
guard_commands = amqp_channel.fanout "smartguard.commands", auto_delete: true
82-
guard_status = amqp_channel.topic "smartguard.events", auto_delete: true
83-
84-
status_queue = amqp_channel.queue '', exclusive: true
85-
status_queue.bind guard_status, routing_key: 'command.*'
86-
87-
cmd, complete = CommandHandler.send(cmd)
88-
id = Process.pid
89-
90-
timer = EventMachine.add_timer(1) do
91-
warn "Smartguard is not responding"
92-
93-
exit 1
94-
end
95-
96-
status_queue.subscribe do |header, message|
97-
body = JSON.load(message)
98-
99-
case header.routing_key
100-
when "command.started"
101-
id, = body
102-
if id == id
103-
warn "Executing command"
104-
EventMachine.cancel_timer timer
105-
end
106-
107-
when "command.finished"
108-
id, result, error = body
109-
if id == id
110-
if !error.nil?
111-
warn "Command failed: #{error}"
112-
elsif !complete.nil?
113-
complete.call result
114-
end
115-
116-
exit 0
117-
end
118-
end
119-
end
120-
121-
guard_commands.publish JSON.dump({
122-
id: id,
123-
command: cmd
124-
})
125-
end
70+
CommandHandler.send cmd, drb

lib/smartguard.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
require 'active_support/all'
22
require 'pathname'
33
require 'fileutils'
4+
require 'drb'
45
require 'socket'
5-
require 'eventmachine'
6-
require 'amqp'
7-
require 'json'
86

97
require 'smartkiosk/common'
108

lib/smartguard/application.rb

+1-43
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
module Smartguard
22
class Application
33
attr_reader :releases_path, :current_path, :shared_path, :active_path
4-
attr_reader :amqp_connection, :amqp_channel, :guard_commands, :guard_status
54

6-
def initialize(path, broker)
5+
def initialize(path)
76
@base_path = Pathname.new(File.absolute_path path)
87

98
if Smartguard.environment == :production
@@ -17,47 +16,6 @@ def initialize(path, broker)
1716
@shared_path = @base_path
1817
@active_path = @base_path
1918
end
20-
21-
@amqp_connection = AMQP.connect broker
22-
@amqp_channel = AMQP::Channel.new @amqp_connection
23-
@guard_commands = @amqp_channel.fanout "smartguard.commands", auto_delete: true
24-
@guard_status = @amqp_channel.topic "smartguard.events", auto_delete: true
25-
26-
command_queue = @amqp_channel.queue '', exclusive: true
27-
command_queue.bind @guard_commands
28-
command_queue.subscribe &method(:command)
29-
end
30-
31-
def post_event(event, *args)
32-
EventMachine.schedule do
33-
@guard_status.publish JSON.dump(args), routing_key: event
34-
end
35-
end
36-
37-
private
38-
39-
def command(header, data)
40-
data = JSON.load data
41-
42-
operation = ->() do
43-
post_event 'command.started', data["id"]
44-
45-
begin
46-
dispatch_command *data["command"]
47-
rescue => e
48-
e
49-
end
50-
end
51-
52-
callback = ->(result) do
53-
if result.respond_to? :exception
54-
post_event 'command.finished', data["id"], nil, result.to_s
55-
else
56-
post_event 'command.finished', data["id"], result
57-
end
58-
end
59-
60-
EventMachine.defer operation, callback
6119
end
6220
end
6321
end

0 commit comments

Comments
 (0)