Skip to content

Commit

Permalink
Merge branch 'refactor_5'
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed Nov 10, 2024
2 parents 2a474c6 + be92de9 commit 3bc0916
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 150 deletions.
12 changes: 9 additions & 3 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ bin/procodile status --simple |grep '^Issues || app1 has 0 instances (should hav
header '(5) Checking procodile stop ...'
bin/procodile stop && sleep 3
bin/procodile status --simple |grep '^Issues || app1 has 0 instances (should have 1), app2 has 0 instances (should have 1), app3 has 0 instances (should have 1)$'
header '(5.1) Checking procodile start when stopped ...'
bin/procodile start && sleep 3
bin/procodile status --simple |grep '^OK || app1\[1\], app2\[1\], app3\[1\]$'
header '(5.2) Checking procodile stop when started ...'
bin/procodile stop && sleep 3
bin/procodile status --simple |grep '^Issues || app1 has 0 instances (should have 1), app2 has 0 instances (should have 1), app3 has 0 instances (should have 1)$'
header '(6) Checking procodile restart when stopped ...'
bin/procodile restart && sleep 3
bin/procodile status --simple |grep '^OK || app1\[1\], app2\[1\], app3\[1\]$'
Expand Down Expand Up @@ -119,15 +125,15 @@ HEREDOC
header '(12) Checking procodile restart will failed when run app3.sh ...'
bin/procodile restart && sleep 3
header '(12.1) Checking procodile restart app3.sh Unknown status ...'
bin/procodile status |grep -F 'app3.4' |grep -F 'Unknown'
bin/procodile status |grep -F 'app3.5' |grep -F 'Unknown'

while ! bin/procodile status |grep -F 'app3.4' |grep -F 'Failed' |grep -F 'respawns:5'; do
while ! bin/procodile status |grep -F 'app3.5' |grep -F 'Failed' |grep -F 'respawns:5'; do
sleep 1
echo 'Waiting respawns to become 5'
done

header '(12.1) Checking procodile restart app3.sh Failed status ...'
bin/procodile status |grep -F 'app3.4' |grep -F 'Failed'
bin/procodile status |grep -F 'app3.5' |grep -F 'Failed'

header '(13) Change Procfile to set correct env for app3.sh'

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec"
require "yaml"
require "../src/procodile/procfile_option"
require "../src/procodile"
APPS_ROOT = File.expand_path("apps", __DIR__)

# require "../src/procodile"
2 changes: 1 addition & 1 deletion spec/specs/app_determination_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe Procodile::AppDetermination do
pwd: "/myapps",
given_root: nil,
given_procfile: nil,
global_options: Procodile::ProcfileOption.from_yaml(global_options)
global_options: Procodile::Config::Option.from_yaml(global_options)
)
ap.root.should eq "/app"
ap.procfile.should eq "/app/Procfile"
Expand Down
20 changes: 10 additions & 10 deletions spec/specs/cli_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe Procodile::CLI do

it "should run help command" do
command = cli.class.commands["help"]
command.should be_a Procodile::CliCommand
command.should be_a Procodile::CLI::Command
command.name.should eq "help"
command.description.should eq "Shows this help output"
command.options.should be_a Proc(OptionParser, Procodile::CLI, Nil)
Expand All @@ -19,7 +19,7 @@ describe Procodile::CLI do

it "should run kill command" do
command = cli.class.commands["kill"]
command.should be_a Procodile::CliCommand
command.should be_a Procodile::CLI::Command
command.name.should eq "kill"
command.description.should eq "Forcefully kill all known processes"
command.options.should be_a Proc(OptionParser, Procodile::CLI, Nil)
Expand All @@ -29,7 +29,7 @@ describe Procodile::CLI do

it "should run start command" do
command = cli.class.commands["start"]
command.should be_a Procodile::CliCommand
command.should be_a Procodile::CLI::Command
command.name.should eq "start"
command.description.should eq "Starts processes and/or the supervisor"
command.options.should be_a Proc(OptionParser, Procodile::CLI, Nil)
Expand All @@ -39,7 +39,7 @@ describe Procodile::CLI do

it "should run stop command" do
command = cli.class.commands["stop"]
command.should be_a Procodile::CliCommand
command.should be_a Procodile::CLI::Command
command.name.should eq "stop"
command.description.should eq "Stops processes and/or the supervisor"
command.options.should be_a Proc(OptionParser, Procodile::CLI, Nil)
Expand All @@ -49,7 +49,7 @@ describe Procodile::CLI do

it "should run status command" do
command = cli.class.commands["status"]
command.should be_a Procodile::CliCommand
command.should be_a Procodile::CLI::Command
command.name.should eq "status"
command.description.should eq "Show the current status of processes"
command.options.should be_a Proc(OptionParser, Procodile::CLI, Nil)
Expand All @@ -59,7 +59,7 @@ describe Procodile::CLI do

it "should run exec command" do
command = cli.class.commands["exec"]
command.should be_a Procodile::CliCommand
command.should be_a Procodile::CLI::Command
command.name.should eq "exec"
command.description.should eq "Execute a command within the environment"
command.options.should be_a Proc(OptionParser, Procodile::CLI, Nil)
Expand All @@ -69,7 +69,7 @@ describe Procodile::CLI do

it "should run reload command" do
command = cli.class.commands["reload"]
command.should be_a Procodile::CliCommand
command.should be_a Procodile::CLI::Command
command.name.should eq "reload"
command.description.should eq "Reload Procodile configuration"
command.options.should be_a Proc(OptionParser, Procodile::CLI, Nil)
Expand All @@ -79,7 +79,7 @@ describe Procodile::CLI do

it "should run check_concurrency command" do
command = cli.class.commands["check_concurrency"]
command.should be_a Procodile::CliCommand
command.should be_a Procodile::CLI::Command
command.name.should eq "check_concurrency"
command.description.should eq "Check process concurrency"
command.options.should be_a Proc(OptionParser, Procodile::CLI, Nil)
Expand All @@ -89,7 +89,7 @@ describe Procodile::CLI do

it "should run log command" do
command = cli.class.commands["log"]
command.should be_a Procodile::CliCommand
command.should be_a Procodile::CLI::Command
command.name.should eq "log"
command.description.should eq "Open/stream a Procodile log file"
command.options.should be_a Proc(OptionParser, Procodile::CLI, Nil)
Expand All @@ -99,7 +99,7 @@ describe Procodile::CLI do

it "should run restart command" do
command = cli.class.commands["restart"]
command.should be_a Procodile::CliCommand
command.should be_a Procodile::CLI::Command
command.name.should eq "restart"
command.description.should eq "Restart processes"
command.options.should be_a Proc(OptionParser, Procodile::CLI, Nil)
Expand Down
8 changes: 4 additions & 4 deletions spec/specs/config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ describe Procodile::Config do
end

it "should not have any options" do
config.options.should be_a Procodile::ProcfileOption
config.options.should eq Procodile::ProcfileOption.new
config.options.should be_a Procodile::Config::Option
config.options.should eq Procodile::Config::Option.new
end

it "should not have any local options" do
config.local_options.should be_a Procodile::ProcfileOption
config.local_options.should eq Procodile::ProcfileOption.new
config.local_options.should be_a Procodile::Config::Option
config.local_options.should eq Procodile::Config::Option.new
end

it "should have a determined pid root and socket path" do
Expand Down
5 changes: 2 additions & 3 deletions spec/specs/procfile_option_spec.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require "../spec_helper"
require "../../src/procodile/procfile_option"

describe Procodile::ProcfileOption do
describe Procodile::Config::Option do
it "should allow root and procfile to be provided" do
procfile_option_file = File.join(APPS_ROOT, "full", "Procfile.options")
procfile_option = Procodile::ProcfileOption.from_yaml(File.read(procfile_option_file))
procfile_option = Procodile::Config::Option.from_yaml(File.read(procfile_option_file))
procfile_option.app_name.should eq "specapp"
procfile_option.pid_root.should eq "tmp/pids"
procfile_option.log_path.should eq "log/procodile.log"
Expand Down
9 changes: 7 additions & 2 deletions src/procodile.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
require "option_parser"
require "yaml"
require "json"
require "socket"
require "file_utils"
require "wait_group"

require "./procodile/app_determination"
require "./procodile/cli"

Expand Down Expand Up @@ -50,15 +55,15 @@ end
global_config_path = ENV["PROCODILE_CONFIG"]? || "/etc/procodile"

if File.file?(global_config_path)
global_config = Procodile::ProcfileOption.from_yaml(File.read(global_config_path))
global_config = Procodile::Config::Option.from_yaml(File.read(global_config_path))
end

# Create a determination to work out where we want to load our app from
ap = Procodile::AppDetermination.new(
FileUtils.pwd,
options[:root]?,
options[:procfile]?,
global_config || Procodile::ProcfileOption.new
global_config || Procodile::Config::Option.new
)

begin
Expand Down
10 changes: 4 additions & 6 deletions src/procodile/app_determination.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "./procfile_option"

module Procodile
#
# This class is responsible for determining which application should be used
Expand All @@ -18,7 +16,7 @@ module Procodile
@pwd : String,
given_root : String?,
@given_procfile : String?,
@global_options : ProcfileOption = ProcfileOption.new
@global_options : Config::Option = Config::Option.new
)
@given_root = given_root ? expand_path(given_root, pwd) : nil

Expand Down Expand Up @@ -81,12 +79,12 @@ module Procodile
end
end

private def find_root_and_procfile_from_options(options : ProcfileOption) : String?
private def find_root_and_procfile_from_options(options : Config::Option) : String?
case options
when ProcfileOption
when Config::Option
# Use the current hash
find_root_and_procfile(@pwd, options.root, options.procfile)
when Array(ProcfileOption)
when Array(Config::Option)
# Global options is provides a list of apps. We need to know which one of
# these we should be looking at.

Expand Down
58 changes: 46 additions & 12 deletions src/procodile/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ module Procodile
]
property options, config

def self.commands : Hash(String, CliCommand)
@@commands ||= {} of String => CliCommand
def self.commands : Hash(String, Command)
@@commands ||= {} of String => Command
end

@@options = {} of Symbol => Proc(OptionParser, Procodile::CLI, Nil)
Expand All @@ -39,14 +39,14 @@ module Procodile
{% end %}

def initialize
@options = Procodile::CliOptions.new
@options = Options.new
@config = uninitialized Procodile::Config

{% for e in COMMANDS %}
{% name = e[0] %}
{% description = e[1] %}

self.class.commands[{{ name.id.stringify }}] = CliCommand.new(
self.class.commands[{{ name.id.stringify }}] = Command.new(
name: {{ name.id.stringify }},
description: {{ description.id.stringify }},
options: @@options[{{ name }}],
Expand All @@ -66,16 +66,17 @@ module Procodile

def self.start_supervisor(
config : Procodile::Config,
options = Procodile::CliOptions.new,
options : Options = Options.new,
&after_start : Proc(Procodile::Supervisor, Nil)
)
run_options = RunOptions.new
run_options.respawn = options.respawn
run_options.stop_when_none = options.stop_when_none
# run_options.proxy = options.proxy
run_options.force_single_log = options.foreground
run_options.port_allocations = options.port_allocations
run_options.foreground = options.foreground
run_options = Supervisor::RunOptions.new(
respawn: options.respawn,
stop_when_none: options.stop_when_none,
proxy: options.proxy,
force_single_log: options.foreground,
port_allocations: options.port_allocations,
foreground: !!options.foreground
)

tidy_pids(config)

Expand Down Expand Up @@ -161,5 +162,38 @@ module Procodile
processes
end
end

struct Command
getter name : String, description : String?, options : Proc(OptionParser, Procodile::CLI, Nil)?, callable : Proc(Nil)

def initialize(@name, @description, @options, @callable)
end
end

struct Options
property foreground : Bool?
property respawn : Bool?
property stop_when_none : Bool?
property proxy : Bool?
property tag : String?
property port_allocations : Hash(String, Int32)?
property start_supervisor : Bool?
property start_processes : Bool?
property stop_supervisor : Bool?
property wait_until_supervisor_stopped : Bool?
property reload : Bool?
property json : Bool?
property json_pretty : Bool?
property simple : Bool?
property processes : String? # A String split by comma.
property clean : Bool?
property development : Bool?
property wait : Bool?
property lines : Int32?
property process : String?

def initialize
end
end
end
end
10 changes: 5 additions & 5 deletions src/procodile/commands/start_command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module Procodile
cli.options.respawn = false
cli.options.foreground = true
cli.options.stop_when_none = true
# cli.options.proxy = true
cli.options.proxy = true
end
end
end
Expand All @@ -79,19 +79,19 @@ module Procodile
raise Error.new "Cannot enable the proxy when the supervisor is running"
end

instances = ControlClient.run(
instance_configs = ControlClient.run(
@config.sock_path,
"start_processes",
processes: process_names_from_cli_option,
tag: @options.tag,
port_allocations: @options.port_allocations,
).as Array(Instance::Config)

if instances.empty?
if instance_configs.empty?
puts "No processes to start."
else
instances.each do |instance|
puts "Started".color(32) + " #{instance.description} (PID: #{instance.pid})"
instance_configs.each do |instance_config|
puts "Started".color(32) + " #{instance_config.description} (PID: #{instance_config.pid})"
end
end
else
Expand Down
11 changes: 7 additions & 4 deletions src/procodile/commands/status_command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ module Procodile

def status : Nil
if supervisor_running?
status = ControlClient.run(@config.sock_path, "status").as ControlClient::ReplyOfStatusCommand
status = ControlClient.run(
@config.sock_path, "status"
).as ControlClient::ReplyOfStatusCommand

if @options.json
case @options
when .json
puts status.to_json
elsif @options.json_pretty
when .json_pretty
puts status
nil
elsif @options.simple
when .simple
if status.messages.empty?
message = status.instances.map { |p, i| "#{p}[#{i.size}]" }

Expand Down
Loading

0 comments on commit 3bc0916

Please sign in to comment.