-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement workaround for Pitchfork PID provider compatibility (#62)
* Implement workaround for Pitchfork PID provider compatibility * Use Puma method for PID provider compatibility, adding a fallback * Fix Namespace ref * Ignore rubocop I think the complexity in the setup method is unavoidable, and trying to factor this logic into another method doesn't improve anything. The assignment in if conditions stuff is a bit suspect, but I don't really mind it ... * Don't do code coverage on specs * Add a spec to check the pid_provider selection logic * Improve code coverage * Improve code coverage --------- Co-authored-by: Ed Robinson <[email protected]>
- Loading branch information
1 parent
c8b5abc
commit c29a8fb
Showing
6 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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,31 @@ | ||
module Promenade | ||
module Pitchfork | ||
class WorkerPidProvider | ||
def self.fetch | ||
worker_id || "process_id_#{Process.pid}" | ||
end | ||
|
||
def self.object_based_worker_id | ||
return unless defined?(::Pitchfork::Worker) | ||
|
||
workers = ObjectSpace.each_object(::Pitchfork::Worker) | ||
return if workers.nil? | ||
|
||
workers_first = workers.first | ||
workers_first&.nr | ||
end | ||
|
||
def self.program_name | ||
$PROGRAM_NAME | ||
end | ||
|
||
def self.worker_id | ||
if matchdata = program_name.match(/pitchfork.*worker\[(.+)\]/) # rubocop:disable Lint/AssignmentInCondition | ||
"pitchfork_#{matchdata[1]}" | ||
elsif object_worker_id = object_based_worker_id # rubocop:disable Lint/AssignmentInCondition | ||
"pitchfork_#{object_worker_id}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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,82 @@ | ||
require "spec_helper" | ||
require "promenade/pitchfork/worker_pid_provider" | ||
|
||
RSpec.describe Promenade::Pitchfork::WorkerPidProvider do | ||
describe ".fetch" do | ||
subject { described_class.fetch } | ||
|
||
context "when worker_id is defined" do | ||
let(:worker_id) { "worker_id" } | ||
|
||
before do | ||
allow(described_class).to receive(:worker_id).and_return(worker_id) | ||
end | ||
|
||
it { is_expected.to eq(worker_id) } | ||
end | ||
|
||
context "when worker_id is not defined" do | ||
before do | ||
allow(described_class).to receive(:worker_id).and_return(nil) | ||
allow(Process).to receive(:pid).and_return(123) | ||
end | ||
|
||
it { is_expected.to eq("process_id_123") } | ||
end | ||
end | ||
|
||
describe ".worker_id" do | ||
subject { described_class.send(:worker_id) } | ||
around(:example) do |ex| | ||
old_name = $PROGRAM_NAME | ||
$PROGRAM_NAME = program_name | ||
ex.run | ||
$PROGRAM_NAME = old_name | ||
end | ||
|
||
context "when program_name matches pitchfork worker" do | ||
let(:program_name) { "pitchfork (gen:0) worker[1] - requests: 13, waiting" } | ||
|
||
|
||
it { is_expected.to eq("pitchfork_1") } | ||
|
||
context "when program_name matches pitchfork worker" do | ||
let(:program_name) { "pitchfork worker[1]" } | ||
|
||
it { is_expected.to eq("pitchfork_1") } | ||
end | ||
|
||
context "when program_name doesn't match pitchfork worker" do | ||
let(:program_name) { "something else" } | ||
|
||
let(:worker) { double("Pitchfork::Worker", nr: 2) } | ||
|
||
before do | ||
stub_const("Pitchfork::Worker", Class.new) | ||
allow(ObjectSpace).to receive(:each_object).with(Pitchfork::Worker).and_return([worker]) | ||
end | ||
|
||
it { is_expected.to eq("pitchfork_2") } | ||
end | ||
end | ||
end | ||
|
||
describe ".object_based_worker_id" do | ||
subject { described_class.send(:object_based_worker_id) } | ||
|
||
context "when Pitchfork::Worker is defined" do | ||
let(:worker) { double("Pitchfork::Worker", nr: 1) } | ||
|
||
before do | ||
stub_const("Pitchfork::Worker", Class.new) | ||
allow(ObjectSpace).to receive(:each_object).with(Pitchfork::Worker).and_return([worker]) | ||
end | ||
|
||
it { is_expected.to eq(1) } | ||
end | ||
|
||
context "when Pitchfork::Worker is not defined" do | ||
it { is_expected.to be_nil } | ||
end | ||
end | ||
end |
This file contains 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 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 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