Skip to content

Commit

Permalink
Pulling out ProgressMessage error logging
Browse files Browse the repository at this point in the history
A progress message only has value if there is an order attached to it
Since we have now order we are now logging the full payload to the
logger so we would have the data available to us in case there was no record found
with the request id in ApprovalRequest
  • Loading branch information
syncrou committed Mar 27, 2019
1 parent f992122 commit f606cb6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 40 deletions.
30 changes: 11 additions & 19 deletions lib/approval_request_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,27 @@ def run
end

def subscribe_to_approval_updates
self.client = ManageIQ::Messaging::Client.open(messaging_client_options)
ManageIQ::Messaging::Client.open(messaging_client_options) do |client|

client.subscribe_topic(
:service => SERVICE_NAME,
:max_bytes => 500_000
) do |messages|
messages.each do |msg|
process_message(msg)
:persist_ref => CLIENT_AND_GROUP_REF,
:max_bytes => 500_000) do |topic|
process_event(topic)
end
end
ensure
client&.close
self.client = nil
end

private

def process_message(msg)
approval = ApprovalRequest.find_by!(:approval_request_ref => msg.payload["request_id"])
approval.order_item.update_message("info", "Task update message received with payload: #{msg.payload}")
if msg.message == EVENT_REQUEST_FINISHED && (msg.payload["decision"] == "approved" || msg.payload["decision"] == "denied")
update_and_log_state(approval, msg.payload)
def process_event(topic)
approval = ApprovalRequest.find_by!(:approval_request_ref => topic.payload["request_id"])
approval.order_item.update_message("info", "Task update message received with payload: #{topic.payload}")
if topic.message == EVENT_REQUEST_FINISHED
update_and_log_state(approval, topic.payload)
end
rescue ActiveRecord::RecordNotFound
Rails.logger.error("Could not find Approval Request with request_id of #{msg.payload['request_id']}")
ProgressMessage.create!(
:level => "error",
:message => "Could not find Approval Request with request_id of #{msg.payload['request_id']}"
)
Rails.logger.error("Could not find Approval Request with payload of #{topic.payload}")
end

def update_and_log_state(approval, payload)
Expand All @@ -59,7 +51,7 @@ def default_messaging_options
{
:protocol => :Kafka,
:client_ref => CLIENT_AND_GROUP_REF,
:group_ref => CLIENT_AND_GROUP_REF
:encoding => 'json'
}
end
end
30 changes: 9 additions & 21 deletions spec/lib/approval_request_listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@
let(:request_id) { "1" }

describe "#subscribe_to_approval_updates" do
let(:request) do
{ :headers => { 'x-rh-identity' => encoded_user_hash }, :original_url => 'whatever' }
end

around do |example|
ManageIQ::API::Common::Request.with_request(request) { example.call }
end

let(:messages) { [ManageIQ::Messaging::ReceivedMessage.new(nil, event, payload, nil)] }
let(:message) { ManageIQ::Messaging::ReceivedMessage.new(nil, event, payload, nil) }
let!(:order_item) { create(:order_item, :order_id => "123", :portfolio_item_id => "234") }
let!(:approval_request) do
ApprovalRequest.create!(
Expand All @@ -28,24 +20,22 @@
allow(ManageIQ::Messaging::Client).to receive(:open).with(
:protocol => :Kafka,
:client_ref => ApprovalRequestListener::CLIENT_AND_GROUP_REF,
:group_ref => ApprovalRequestListener::CLIENT_AND_GROUP_REF
).and_return(client)
:encoding => 'json'
).and_yield(client)
allow(client).to receive(:subscribe_topic).with(
:service => ApprovalRequestListener::SERVICE_NAME,
:max_bytes => 500_000
).and_yield(messages)
allow(client).to receive(:close)
:service => ApprovalRequestListener::SERVICE_NAME,
:persist_ref => ApprovalRequestListener::CLIENT_AND_GROUP_REF,
:max_bytes => 500_000
).and_yield(message)
end

context "when the approval request is not findable" do
context "when the request_id is anything else" do
let(:payload) { {"request_id" => "10" } }

it "creates a progress message about the payload" do
expect(Rails.logger).to receive(:error).with("Could not find Approval Request with payload of #{payload}")
subject.subscribe_to_approval_updates
latest_progress_message = ProgressMessage.last
expect(latest_progress_message.level).to eq("error")
expect(latest_progress_message.message).to eq("Could not find Approval Request with request_id of 10")
end

it "does not update the approval request" do
Expand All @@ -60,10 +50,8 @@
let(:decision) { "approved" }

it "creates a progress message about the approval request error" do
expect(Rails.logger).to receive(:error).with("Could not find Approval Request with payload of #{payload}")
subject.subscribe_to_approval_updates
latest_progress_message = ProgressMessage.last
expect(latest_progress_message.level).to eq("error")
expect(latest_progress_message.message).to eq("Could not find Approval Request with request_id of 1")
end

it "does not update the approval request" do
Expand Down

0 comments on commit f606cb6

Please sign in to comment.