-
Notifications
You must be signed in to change notification settings - Fork 70
DEV: Add compatibility with Pitchfork #293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DockerManager | ||
| class PitchforkAdapter < WebServerAdapter | ||
| def server_name | ||
| "Pitchfork" | ||
| end | ||
|
|
||
| def launcher_pid | ||
| `pgrep -f unicorn_launcher`.strip.to_i | ||
| end | ||
|
|
||
| def master_pid | ||
| `pgrep -f "pitchfork monitor"`.strip.to_i | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DockerManager | ||
| class UnicornAdapter < WebServerAdapter | ||
| def server_name | ||
| "Unicorn" | ||
| end | ||
|
|
||
| def launcher_pid | ||
| `pgrep -f unicorn_launcher`.strip.to_i | ||
| end | ||
|
|
||
| def master_pid | ||
| `pgrep -f "unicorn master -E"`.strip.to_i | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DockerManager | ||
| class WebServerAdapter | ||
| RESTART_FLAG_KEY = "docker_manager:upgrade:server_restarting" | ||
|
|
||
| attr_reader :upgrader | ||
|
|
||
| delegate :log, to: :upgrader | ||
|
|
||
| def initialize(upgrader) | ||
| @upgrader = upgrader | ||
| end | ||
|
|
||
| def workers | ||
| `pgrep -f -P #{master_pid} worker`.split("\n").map(&:to_i) | ||
| end | ||
|
|
||
| def local_web_url | ||
| "http://127.0.0.1:#{ENV["UNICORN_PORT"] || 3000}/srv/status" | ||
| end | ||
|
|
||
| def scale_down_workers(count) | ||
| count.times { Process.kill("TTOU", master_pid) } | ||
| end | ||
|
|
||
| def scale_up_workers(count) | ||
| count.times { Process.kill("TTIN", master_pid) } | ||
| end | ||
|
|
||
| def min_workers | ||
| 1 | ||
| end | ||
|
|
||
| def reload | ||
| set_restart_flag | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With |
||
| log("Restarting #{server_name} pid: #{launcher_pid}") | ||
| original_master_pid = master_pid | ||
| Process.kill("USR2", launcher_pid) | ||
|
|
||
| # Wait for the original master/monitor to exit (it will spawn a new one) | ||
| iterations = 0 | ||
| while pid_exists?(original_master_pid) | ||
| iterations += 1 | ||
| break if iterations >= 60 | ||
| log("Waiting for #{server_name} to reload#{"." * iterations}") | ||
| sleep 2 | ||
| end | ||
|
|
||
| # Wait for workers to be ready | ||
| iterations = 0 | ||
| while `curl -s #{local_web_url}` != "ok" | ||
| iterations += 1 | ||
| break if iterations >= 60 | ||
| log("Waiting for #{server_name} workers to start up#{"." * iterations}") | ||
| sleep 2 | ||
| end | ||
| clear_restart_flag | ||
| end | ||
|
|
||
| def set_restart_flag | ||
| Discourse.redis.setex(RESTART_FLAG_KEY, 2.minutes.to_i, 1) | ||
| end | ||
|
|
||
| def clear_restart_flag | ||
| Discourse.redis.del(RESTART_FLAG_KEY) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def pid_exists?(pid) | ||
| Process.getpgid(pid) | ||
| rescue Errno::ESRCH | ||
| false | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "docker_manager/upgrader" | ||
|
|
||
| RSpec.describe DockerManager::PitchforkAdapter do | ||
| subject(:adapter) { described_class.new(upgrader) } | ||
|
|
||
| let(:upgrader) { instance_double(DockerManager::Upgrader, log: nil) } | ||
|
|
||
| before { allow_any_instance_of(Kernel).to receive(:`) } | ||
|
|
||
| it_behaves_like "a web server adapter" | ||
|
|
||
| describe "#server_name" do | ||
| it "returns 'Pitchfork'" do | ||
| expect(adapter.server_name).to eq("Pitchfork") | ||
| end | ||
| end | ||
|
|
||
| describe "#launcher_pid" do | ||
| before do | ||
| allow_any_instance_of(Kernel).to receive(:`).with("pgrep -f unicorn_launcher").and_return( | ||
| "1234\n", | ||
| ) | ||
| end | ||
|
|
||
| it "returns the pid of the 'unicorn_launcher' process" do | ||
| expect(adapter.launcher_pid).to eq(1234) | ||
| end | ||
| end | ||
|
|
||
| describe "#master_pid" do | ||
| before do | ||
| allow_any_instance_of(Kernel).to receive(:`).with('pgrep -f "pitchfork monitor"').and_return( | ||
| "5678\n", | ||
| ) | ||
| end | ||
|
|
||
| it "returns the pid of the Pitchfork monitor process" do | ||
| expect(adapter.master_pid).to eq(5678) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "docker_manager/upgrader" | ||
|
|
||
| RSpec.describe DockerManager::UnicornAdapter do | ||
| subject(:adapter) { described_class.new(upgrader) } | ||
|
|
||
| let(:upgrader) { instance_double(DockerManager::Upgrader, log: nil) } | ||
|
|
||
| before { allow_any_instance_of(Kernel).to receive(:`) } | ||
|
|
||
| it_behaves_like "a web server adapter" | ||
|
|
||
| describe "#server_name" do | ||
| it "returns 'Unicorn'" do | ||
| expect(adapter.server_name).to eq("Unicorn") | ||
| end | ||
| end | ||
|
|
||
| describe "#launcher_pid" do | ||
| before do | ||
| allow_any_instance_of(Kernel).to receive(:`).with("pgrep -f unicorn_launcher").and_return( | ||
| "1234\n", | ||
| ) | ||
| end | ||
|
|
||
| it "returns the pid of the 'unicorn_launcher' process" do | ||
| expect(adapter.launcher_pid).to eq(1234) | ||
| end | ||
| end | ||
|
|
||
| describe "#master_pid" do | ||
| before do | ||
| allow_any_instance_of(Kernel).to receive(:`).with('pgrep -f "unicorn master -E"').and_return( | ||
| "5678\n", | ||
| ) | ||
| end | ||
|
|
||
| it "returns the pid of the Unicorn master process" do | ||
| expect(adapter.master_pid).to eq(5678) | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually doesn’t work anymore. It hasn’t for some time now, at least one year (see discourse/discourse#27257).