From 871abc7db0ad7c316298293d603bf1095f3e79c8 Mon Sep 17 00:00:00 2001 From: Brian Leonard Date: Tue, 9 Jul 2019 14:31:43 +0200 Subject: [PATCH 01/13] search for subscriptions in web server --- lib/resque_bus/server/views/bus.erb | 41 +++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/resque_bus/server/views/bus.erb b/lib/resque_bus/server/views/bus.erb index 312b691..2d31066 100644 --- a/lib/resque_bus/server/views/bus.erb +++ b/lib/resque_bus/server/views/bus.erb @@ -16,20 +16,38 @@ else app_hash = {} class_hash = {} event_hash = {} + query_string = params[:query].to_s.strip + + query_attributes = nil + if query_string.length > 0 + query_attributes = JSON.parse(query_string) rescue nil + query_attributes ||= { "bus_event_type" => query_string, "other" => true } + end # collect each differently ::QueueBus::Application.all.each do |app| app_key = app.app_key - app_hash[app_key] ||= [] - app.event_display_tuples.each do |tuple| - class_name, queue, filters = tuple + # TODO: all of this can move to method in queue-bus to replace event_display_tuples + if query_attributes + subscriptions = app.subscription_matches(query_attributes) + else + subscriptions = app.send(:subscriptions).all + end + + + subscriptions.each do |sub| + class_name = sub.class_name + queue = sub.queue_name + filters = sub.matcher.filters + if filters["bus_event_type"] event = filters["bus_event_type"] else event = "see filter" end + app_hash[app_key] ||= [] app_hash[app_key] << [event, class_name, queue, filters] class_hash[class_name] ||= [] @@ -53,7 +71,7 @@ else form = "" if button text, url = button - form = "
" + form = "
" end if !val @@ -85,7 +103,19 @@ else end %> +

Event Search

+

Filter by the Event Type or full JSON.

+
" style="float:none;padding:0;margin:0;"> + + +
+<% if query_attributes %> +
<%= 
+  h(JSON.pretty_generate(query_attributes).strip).gsub("\n", "
").gsub(" ", " ") + %>
+<% end %> +

Applications

The apps below have registered the given classes and queues.

@@ -97,7 +127,8 @@ else Queue Filters - <%= output_hash(app_hash, ["Unsubscribe", "bus/unsubscribe"]) %> + + <%= output_hash(app_hash, query_attributes ? false : ["Unsubscribe", "bus/unsubscribe"]) %>

 

From bf13e2c00ec8363b0f6481934387087e3a012365 Mon Sep 17 00:00:00 2001 From: Brian Leonard Date: Tue, 9 Jul 2019 15:08:38 +0200 Subject: [PATCH 02/13] Always do JSON for server event search. --- lib/resque_bus/server/views/bus.erb | 30 ++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/resque_bus/server/views/bus.erb b/lib/resque_bus/server/views/bus.erb index 2d31066..ca84a1d 100644 --- a/lib/resque_bus/server/views/bus.erb +++ b/lib/resque_bus/server/views/bus.erb @@ -19,9 +19,14 @@ else query_string = params[:query].to_s.strip query_attributes = nil + query_error = nil if query_string.length > 0 - query_attributes = JSON.parse(query_string) rescue nil - query_attributes ||= { "bus_event_type" => query_string, "other" => true } + begin + query_attributes = JSON.parse(query_string) + rescue Exception => e + query_attributes = nil + query_error = e.message + end end # collect each differently @@ -103,16 +108,23 @@ else end %> -

Event Search

-

Filter by the Event Type or full JSON.

+

Sample Event

+

Enter JSON of an event to see applicable subscriptions.

" style="float:none;padding:0;margin:0;"> - - +
+ +
+ + +
-<% if query_attributes %> -
<%= 
-  h(JSON.pretty_generate(query_attributes).strip).gsub("\n", "
").gsub(" ", " ") +<% if query_error %> + +
<%= 
+  h(query_error.strip)
   %>
<% end %>
From 9c1d498cb6dabd62e56c57cda9029889eba9db67 Mon Sep 17 00:00:00 2001 From: Brian Leonard Date: Tue, 9 Jul 2019 15:44:24 +0200 Subject: [PATCH 03/13] try various ways to fix up the search query --- lib/resque_bus/server/views/bus.erb | 63 +++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/lib/resque_bus/server/views/bus.erb b/lib/resque_bus/server/views/bus.erb index ca84a1d..95255c3 100644 --- a/lib/resque_bus/server/views/bus.erb +++ b/lib/resque_bus/server/views/bus.erb @@ -13,6 +13,58 @@ else <% + + def fixup_query(query_string) + has_open_brace = query_string.include?("{") + has_close_brace = query_string.include?("}") + has_multiple_lines = query_string.include?("\n") + has_colon = query_string.include?(":") + has_comma = query_string.include?(",") + has_quote = query_string.include?("\"") + + if !has_open_brace && !has_close_brace + # maybe they just forgot the braces + fixed = JSON.parse("{ #{query_string} }") rescue nil + return fixed if fixed + end + + if !has_open_brace + # maybe they just forgot the braces + fixed = JSON.parse("{ #{query_string}") rescue nil + return fixed if fixed + end + + if !has_close_brace + # maybe they just forgot the braces + fixed = JSON.parse("#{query_string} }") rescue nil + return fixed if fixed + end + + if !has_multiple_lines && !has_colon && !has_open_brace && !has_close_brace + # we say they just put a bus_event type here, so help them out + return {"bus_event_type" => query_string, "more_here" => true} + end + + if has_colon && !has_quote + # maybe it's some search syntax like this: field: value other: true, etc + # maybe use something like this later: https://github.com/dxwcyber/search-query-parser + + # quote all the strings, (simply) tries to avoid integers + test_query = query_string.gsub(/([a-zA-z]\w*)/,'"\0"') + if !has_comma + test_query.gsub!("\n", ",\n") + end + if !has_open_brace && !has_close_brace + test_query = "{ #{test_query} }" + end + + fixed = JSON.parse(test_query) rescue nil + return fixed if fixed + end + + nil + end + app_hash = {} class_hash = {} event_hash = {} @@ -24,8 +76,13 @@ else begin query_attributes = JSON.parse(query_string) rescue Exception => e - query_attributes = nil - query_error = e.message + # see if simple event type to help user get in the right format + query_attributes = fixup_query(query_string) + # give error if we couldn't fix it up + if !query_attributes + query_attributes = nil + query_error = e.message + end end end @@ -112,7 +169,7 @@ else

Enter JSON of an event to see applicable subscriptions.

" style="float:none;padding:0;margin:0;">
-
From 29f2ea18b77b72dd5fbb3b022e7c96f52c39855c Mon Sep 17 00:00:00 2001 From: Brian Leonard Date: Tue, 9 Jul 2019 16:10:30 +0200 Subject: [PATCH 04/13] Clean up a view things in event search --- lib/resque_bus/server.rb | 64 ++++++++++++++++++++++ lib/resque_bus/server/views/bus.erb | 84 ++++++----------------------- 2 files changed, 79 insertions(+), 69 deletions(-) diff --git a/lib/resque_bus/server.rb b/lib/resque_bus/server.rb index d26eb62..73ab8ba 100644 --- a/lib/resque_bus/server.rb +++ b/lib/resque_bus/server.rb @@ -22,6 +22,70 @@ def self.included(base) } end + + class Helpers + class << self + def fixup_query(query_string) + has_open_brace = query_string.include?("{") + has_close_brace = query_string.include?("}") + has_multiple_lines = query_string.include?("\n") + has_colon = query_string.include?(":") + has_comma = query_string.include?(",") + has_quote = query_string.include?("\"") + + if !has_open_brace && !has_close_brace + # maybe they just forgot the braces + fixed = JSON.parse("{ #{query_string} }") rescue nil + return fixed if fixed + end + + if !has_open_brace + # maybe they just forgot the braces + fixed = JSON.parse("{ #{query_string}") rescue nil + return fixed if fixed + end + + if !has_close_brace + # maybe they just forgot the braces + fixed = JSON.parse("#{query_string} }") rescue nil + return fixed if fixed + end + + if !has_multiple_lines && !has_colon && !has_open_brace && !has_close_brace + # we say they just put a bus_event type here, so help them out + return {"bus_event_type" => query_string, "more_here" => true} + end + + if has_colon && !has_quote + # maybe it's some search syntax like this: field: value other: true, etc + # maybe use something like this later: https://github.com/dxwcyber/search-query-parser + + # quote all the strings, (simply) tries to avoid integers + test_query = query_string.gsub(/([a-zA-z]\w*)/,'"\0"') + if !has_comma + test_query.gsub!("\n", ",\n") + end + if !has_open_brace && !has_close_brace + test_query = "{ #{test_query} }" + end + + fixed = JSON.parse(test_query) rescue nil + return fixed if fixed + end + + nil + end + + def query_subscriptions(app, query_attributes) + # TODO: all of this can move to method in queue-bus to replace event_display_tuples + if query_attributes + subscriptions = app.subscription_matches(query_attributes) + else + subscriptions = app.send(:subscriptions).all + end + end + end + end end end diff --git a/lib/resque_bus/server/views/bus.erb b/lib/resque_bus/server/views/bus.erb index 95255c3..1b7fd90 100644 --- a/lib/resque_bus/server/views/bus.erb +++ b/lib/resque_bus/server/views/bus.erb @@ -13,58 +13,6 @@ else <% - - def fixup_query(query_string) - has_open_brace = query_string.include?("{") - has_close_brace = query_string.include?("}") - has_multiple_lines = query_string.include?("\n") - has_colon = query_string.include?(":") - has_comma = query_string.include?(",") - has_quote = query_string.include?("\"") - - if !has_open_brace && !has_close_brace - # maybe they just forgot the braces - fixed = JSON.parse("{ #{query_string} }") rescue nil - return fixed if fixed - end - - if !has_open_brace - # maybe they just forgot the braces - fixed = JSON.parse("{ #{query_string}") rescue nil - return fixed if fixed - end - - if !has_close_brace - # maybe they just forgot the braces - fixed = JSON.parse("#{query_string} }") rescue nil - return fixed if fixed - end - - if !has_multiple_lines && !has_colon && !has_open_brace && !has_close_brace - # we say they just put a bus_event type here, so help them out - return {"bus_event_type" => query_string, "more_here" => true} - end - - if has_colon && !has_quote - # maybe it's some search syntax like this: field: value other: true, etc - # maybe use something like this later: https://github.com/dxwcyber/search-query-parser - - # quote all the strings, (simply) tries to avoid integers - test_query = query_string.gsub(/([a-zA-z]\w*)/,'"\0"') - if !has_comma - test_query.gsub!("\n", ",\n") - end - if !has_open_brace && !has_close_brace - test_query = "{ #{test_query} }" - end - - fixed = JSON.parse(test_query) rescue nil - return fixed if fixed - end - - nil - end - app_hash = {} class_hash = {} event_hash = {} @@ -77,7 +25,7 @@ else query_attributes = JSON.parse(query_string) rescue Exception => e # see if simple event type to help user get in the right format - query_attributes = fixup_query(query_string) + query_attributes = ::ResqueBus::Server::Helpers.fixup_query(query_string) # give error if we couldn't fix it up if !query_attributes query_attributes = nil @@ -90,14 +38,7 @@ else ::QueueBus::Application.all.each do |app| app_key = app.app_key - # TODO: all of this can move to method in queue-bus to replace event_display_tuples - if query_attributes - subscriptions = app.subscription_matches(query_attributes) - else - subscriptions = app.send(:subscriptions).all - end - - + subscriptions = ::ResqueBus::Server::Helpers.query_subscriptions(app, query_attributes) subscriptions.each do |sub| class_name = sub.class_name queue = sub.queue_name @@ -167,23 +108,28 @@ else

Sample Event

Enter JSON of an event to see applicable subscriptions.

-" style="float:none;padding:0;margin:0;"> -
-
-
- + +
<% if query_error %> - -
<%= 
+  
<%= 
   h(query_error.strip)
   %>
<% end %> +<% if query_attributes %> +
<%= 
+  h(JSON.pretty_generate(query_attributes).strip)
+  %>
+<% end %> +

Applications

From c13f907157691301f13f1326b6c3fd44a395198d Mon Sep 17 00:00:00 2001 From: Brian Leonard Date: Tue, 9 Jul 2019 16:47:35 +0200 Subject: [PATCH 05/13] support hash.inspect logging from event publish --- lib/resque_bus/server.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/resque_bus/server.rb b/lib/resque_bus/server.rb index 73ab8ba..c9b6c6c 100644 --- a/lib/resque_bus/server.rb +++ b/lib/resque_bus/server.rb @@ -73,6 +73,26 @@ def fixup_query(query_string) return fixed if fixed end + if has_open_brace && has_close_brace + # maybe the whole thing is a hash output from a hash.inspect log + ruby_hash_text = query_string.clone + # https://stackoverflow.com/questions/1667630/how-do-i-convert-a-string-object-into-a-hash-object + # Transform object string symbols to quoted strings + ruby_hash_text.gsub!(/([{,]\s*):([^>\s]+)\s*=>/, '\1"\2"=>') + # Transform object string numbers to quoted strings + ruby_hash_text.gsub!(/([{,]\s*)([0-9]+\.?[0-9]*)\s*=>/, '\1"\2"=>') + # Transform object value symbols to quotes strings + ruby_hash_text.gsub!(/([{,]\s*)(".+?"|[0-9]+\.?[0-9]*)\s*=>\s*:([^,}\s]+\s*)/, '\1\2=>"\3"') + # Transform array value symbols to quotes strings + ruby_hash_text.gsub!(/([\[,]\s*):([^,\]\s]+)/, '\1"\2"') + # fix up nil situation + ruby_hash_text.gsub!(/=>nil/, '=>null') + # Transform object string object value delimiter to colon delimiter + ruby_hash_text.gsub!(/([{,]\s*)(".+?"|[0-9]+\.?[0-9]*)\s*=>/, '\1\2:') + fixed = YAML.load(ruby_hash_text) rescue nil + return fixed if fixed + end + nil end From 2f3149f2bd3219e49c158ddf66ef3959c9bd05b1 Mon Sep 17 00:00:00 2001 From: Brian Leonard Date: Tue, 9 Jul 2019 16:55:34 +0200 Subject: [PATCH 06/13] Sort query for display --- lib/resque_bus/server.rb | 9 +++++++++ lib/resque_bus/server/views/bus.erb | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/lib/resque_bus/server.rb b/lib/resque_bus/server.rb index c9b6c6c..d1f004e 100644 --- a/lib/resque_bus/server.rb +++ b/lib/resque_bus/server.rb @@ -96,6 +96,15 @@ def fixup_query(query_string) nil end + def sort_query(query_attributes) + query_attributes.each do |key, value| + if value.is_a?(Hash) + query_attributes[key] = sort_query(value) + end + end + query_attributes.sort_by { |key| key }.to_h + end + def query_subscriptions(app, query_attributes) # TODO: all of this can move to method in queue-bus to replace event_display_tuples if query_attributes diff --git a/lib/resque_bus/server/views/bus.erb b/lib/resque_bus/server/views/bus.erb index 1b7fd90..edd0c50 100644 --- a/lib/resque_bus/server/views/bus.erb +++ b/lib/resque_bus/server/views/bus.erb @@ -32,6 +32,11 @@ else query_error = e.message end end + + if query_attributes + # sort keys for display + query_attributes = ::ResqueBus::Server::Helpers.sort_query(query_attributes) + end end # collect each differently From efdafa989d7cda2b1546a340ac0da4f9bef2ab82 Mon Sep 17 00:00:00 2001 From: Brian Leonard Date: Tue, 9 Jul 2019 17:33:31 +0200 Subject: [PATCH 07/13] handle parsing of enqueued array --- lib/resque_bus/server.rb | 30 ++++++++++++++++++++++++++--- lib/resque_bus/server/views/bus.erb | 12 ++++-------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/lib/resque_bus/server.rb b/lib/resque_bus/server.rb index d1f004e..a0d3921 100644 --- a/lib/resque_bus/server.rb +++ b/lib/resque_bus/server.rb @@ -25,7 +25,7 @@ def self.included(base) class Helpers class << self - def fixup_query(query_string) + def parse_query(query_string) has_open_brace = query_string.include?("{") has_close_brace = query_string.include?("}") has_multiple_lines = query_string.include?("\n") @@ -33,6 +33,30 @@ def fixup_query(query_string) has_comma = query_string.include?(",") has_quote = query_string.include?("\"") + exception = nil + + # first let's see if it parses + begin + query_attributes = JSON.parse(query_string) + raise "Not a JSON Object" unless query_attributes.is_a?(Hash) + rescue Exception => e + exception = e + end + return query_attributes unless exception + + if query_attributes + # it parsed but it's something else + if query_attributes.is_a?(Array) && query_attributes.length == 1 + # maybe it's the thing from the queue + json_string = query_attributes.first + fixed = JSON.parse(json_string) rescue nil + return fixed if fixed + end + + # something else? + raise exception + end + if !has_open_brace && !has_close_brace # maybe they just forgot the braces fixed = JSON.parse("{ #{query_string} }") rescue nil @@ -89,11 +113,11 @@ def fixup_query(query_string) ruby_hash_text.gsub!(/=>nil/, '=>null') # Transform object string object value delimiter to colon delimiter ruby_hash_text.gsub!(/([{,]\s*)(".+?"|[0-9]+\.?[0-9]*)\s*=>/, '\1\2:') - fixed = YAML.load(ruby_hash_text) rescue nil + fixed = JSON.parse(ruby_hash_text) rescue nil return fixed if fixed end - nil + raise exception end def sort_query(query_attributes) diff --git a/lib/resque_bus/server/views/bus.erb b/lib/resque_bus/server/views/bus.erb index edd0c50..ed14a27 100644 --- a/lib/resque_bus/server/views/bus.erb +++ b/lib/resque_bus/server/views/bus.erb @@ -22,15 +22,11 @@ else query_error = nil if query_string.length > 0 begin - query_attributes = JSON.parse(query_string) + query_attributes = ::ResqueBus::Server::Helpers.parse_query(query_string) + raise "Not a JSON Object" unless query_attributes.is_a?(Hash) rescue Exception => e - # see if simple event type to help user get in the right format - query_attributes = ::ResqueBus::Server::Helpers.fixup_query(query_string) - # give error if we couldn't fix it up - if !query_attributes - query_attributes = nil - query_error = e.message - end + query_attributes = nil + query_error = e.message end if query_attributes From e7bc606d16d4a1a0220bb39856718652ccb070e3 Mon Sep 17 00:00:00 2001 From: Brian Leonard Date: Tue, 9 Jul 2019 21:04:46 +0200 Subject: [PATCH 08/13] add the subscription key to the table. --- lib/resque_bus/server/views/bus.erb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/resque_bus/server/views/bus.erb b/lib/resque_bus/server/views/bus.erb index ed14a27..6636f2d 100644 --- a/lib/resque_bus/server/views/bus.erb +++ b/lib/resque_bus/server/views/bus.erb @@ -44,6 +44,7 @@ else class_name = sub.class_name queue = sub.queue_name filters = sub.matcher.filters + sub_key = sub.key if filters["bus_event_type"] event = filters["bus_event_type"] @@ -52,13 +53,13 @@ else end app_hash[app_key] ||= [] - app_hash[app_key] << [event, class_name, queue, filters] + app_hash[app_key] << [sub_key, event, class_name, queue, filters] class_hash[class_name] ||= [] - class_hash[class_name] << [app_key, event, queue, filters] + class_hash[class_name] << [app_key, sub_key, event, queue, filters] event_hash[event] ||= [] - event_hash[event] << [app_key, class_name, queue, filters] + event_hash[event] << [app_key, sub_key, class_name, queue, filters] end end @@ -81,8 +82,8 @@ else if !val out = "  " else - one, two, queue, filters = val - out = "#{h(one)}#{h(two)}#{h(queue)}" + one, two, three, queue, filters = val + out = "#{h(one)}#{h(two)}#{h(three)}#{h(queue)}" out << "#{h(::QueueBus::Util.encode(filters).gsub(/\"bus_special_value_(\w+)\"/){ "(#{$1})" }).gsub(" ", " ").gsub('","', '", "')}" end @@ -138,6 +139,7 @@ else + @@ -155,6 +157,7 @@ else + @@ -172,6 +175,7 @@ else + From cddbb5c77379073f7d1ae934920a9bd550b362c0 Mon Sep 17 00:00:00 2001 From: Brian Leonard Date: Tue, 9 Jul 2019 21:14:43 +0200 Subject: [PATCH 09/13] button to set the text area to the pretty version. --- lib/resque_bus/server/views/bus.erb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/resque_bus/server/views/bus.erb b/lib/resque_bus/server/views/bus.erb index 6636f2d..aba5b9b 100644 --- a/lib/resque_bus/server/views/bus.erb +++ b/lib/resque_bus/server/views/bus.erb @@ -9,6 +9,13 @@ if (agree) else return false ; } + +function setSample() { + var text = document.getElementById("query_attributes").textContent; + var textArea = document.getElementById('querytext'); + textArea.value = text; + return false; +} // --> @@ -112,7 +119,7 @@ else

Enter JSON of an event to see applicable subscriptions.

" style="float:none;padding:0;margin:0;"> -
@@ -127,9 +134,12 @@ else %> <% end %> <% if query_attributes %> -
<%= 
+  
<%= 
   h(JSON.pretty_generate(query_attributes).strip)
   %>
+
+ +
<% end %>
From 5591dec3369fcba5d1d305e366357fe0ce4ec80e Mon Sep 17 00:00:00 2001 From: Brian Leonard Date: Mon, 22 Jul 2019 13:34:13 +0200 Subject: [PATCH 10/13] =?UTF-8?q?Don=E2=80=99t=20flush=20it=20all!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/spec_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6b6c896..54b2685 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -60,7 +60,7 @@ def test_list(*args) end config.after(:each) do begin - QueueBus.redis { |redis| redis.flushall } + QueueBus.redis { |redis| redis.flushdb } rescue end QueueBus.send(:reset) From 58d214e95c452a16e97ed10b8f264f14ab65bb47 Mon Sep 17 00:00:00 2001 From: Brian Leonard Date: Mon, 22 Jul 2019 13:34:21 +0200 Subject: [PATCH 11/13] adds some specs --- lib/resque_bus/server.rb | 1 + spec/server_helper_spec.rb | 114 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 spec/server_helper_spec.rb diff --git a/lib/resque_bus/server.rb b/lib/resque_bus/server.rb index a0d3921..4a75d7d 100644 --- a/lib/resque_bus/server.rb +++ b/lib/resque_bus/server.rb @@ -26,6 +26,7 @@ def self.included(base) class Helpers class << self def parse_query(query_string) + query_string = query_string.to_s.strip has_open_brace = query_string.include?("{") has_close_brace = query_string.include?("}") has_multiple_lines = query_string.include?("\n") diff --git a/spec/server_helper_spec.rb b/spec/server_helper_spec.rb new file mode 100644 index 0000000..0ee8d8e --- /dev/null +++ b/spec/server_helper_spec.rb @@ -0,0 +1,114 @@ +require 'spec_helper' +require_relative '../lib/resque_bus/server' + +describe "Web Server Helper" do + describe ".parse_query" do + it "should pass through valid json" do + input = %Q{ + { "name": "here", "number": 1, "bool": true } + } + output = {"name"=>"here", "number"=>1, "bool"=>true} + check = ::ResqueBus::Server::Helpers.parse_query(input) + check.should == output + end + + it "should handle multi-line json" do + input = %Q{ + { + "name": "here", + "number": 1, + "bool": true + } + } + output = {"name"=>"here", "number"=>1, "bool"=>true} + check = ::ResqueBus::Server::Helpers.parse_query(input) + check.should == output + end + + it "should interpret simple string as bus_event_type" do + input = %Q{ user_created } + output = {"bus_event_type" => "user_created", "more_here" => true} + check = ::ResqueBus::Server::Helpers.parse_query(input) + check.should == output + end + + it "should raise error on valid json that's not an Object" do + input = '[{ "name": "here" }]' + lambda { + ::ResqueBus::Server::Helpers.parse_query(input) + }.should raise_error("Not a JSON Object") + end + + it "should allow array from resque server panel with encoded json string arg array" do + input = '["{\"name\":\"here\"}"]' + output = {"name" => "here"} + check = ::ResqueBus::Server::Helpers.parse_query(input) + check.should == output + end + + it "should take in a arg list and make it json" do + input = %Q{ + bus_event_type: my_event + user_updated: true + } + output = {"bus_event_type" => "my_event", "user_updated" => "true"} + check = ::ResqueBus::Server::Helpers.parse_query(input) + check.should == output + end + + it "should take in an arg list with quoted json and commas" do + input = %Q{ + "bus_event_type": "my_event", + "user_updated": true + } + output = {"bus_event_type" => "my_event", "user_updated" => true} + check = ::ResqueBus::Server::Helpers.parse_query(input) + check.should == output + end + + it "should parse logged output from event.inspect" do + input = %Q{ + {"bus_published_at"=>1563793250, "bus_event_type"=>"user_created", :user_id=>42, :name=>"Brian" } + } + output = { + "bus_published_at" => 1563793250, + "bus_event_type" => "user_created", + "user_id" => 42, + "name" => "Brian" + } + check = ::ResqueBus::Server::Helpers.parse_query(input) + check.should == output + end + + it "should throw json parse error when it can't be handled" do + input = '{ "name": "here" q }' + lambda { + ::ResqueBus::Server::Helpers.parse_query(input) + }.should raise_error(/unexpected token/) + end + end + + describe ".sort_query" do + it "should alphabetize a query hash" do + input = {"cat" => true, "apple" => true, "dog" => true, "bear" => true} + output = {"apple" => true, "bear" => true, "cat" => true, "dog" => true } + check = ::ResqueBus::Server::Helpers.sort_query(input) + check.should == output + end + + it "should alphabetize a query sub-hashes but not arrays" do + input = {"cat" => true, "apple" => [ + "jackal", "kangaroo", "iguana" + ], "dog" => { + "frog" => 11, "elephant" => 12, "hare" => 16, "goat" => 14 + }, "bear" => true} + output = {"apple" => [ + "jackal", "kangaroo", "iguana" + ], "bear" => true, "cat" => true, "dog" => { + "elephant" => 12, "frog" => 11, "goat" => 14, "hare" => 16 + }} + check = ::ResqueBus::Server::Helpers.sort_query(input) + check.should == output + end + end +end From 9df1f8ec84bdadf820376c8c2cb85162c5246f4a Mon Sep 17 00:00:00 2001 From: Brian Leonard Date: Mon, 22 Jul 2019 13:35:08 +0200 Subject: [PATCH 12/13] rescue StandardError --- lib/resque_bus/server.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/resque_bus/server.rb b/lib/resque_bus/server.rb index 4a75d7d..b6f9c27 100644 --- a/lib/resque_bus/server.rb +++ b/lib/resque_bus/server.rb @@ -40,7 +40,7 @@ def parse_query(query_string) begin query_attributes = JSON.parse(query_string) raise "Not a JSON Object" unless query_attributes.is_a?(Hash) - rescue Exception => e + rescue StandardError => e exception = e end return query_attributes unless exception From 295634e17313888d4b87d08eede84c42cde57182 Mon Sep 17 00:00:00 2001 From: Brian Leonard Date: Mon, 22 Jul 2019 13:38:20 +0200 Subject: [PATCH 13/13] Clearer comment --- lib/resque_bus/server.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/resque_bus/server.rb b/lib/resque_bus/server.rb index b6f9c27..9d01bad 100644 --- a/lib/resque_bus/server.rb +++ b/lib/resque_bus/server.rb @@ -48,7 +48,8 @@ def parse_query(query_string) if query_attributes # it parsed but it's something else if query_attributes.is_a?(Array) && query_attributes.length == 1 - # maybe it's the thing from the queue + # maybe it's pasted from the inputs in the web UI like queues/bus_incoming + # this is an array (of job arguments) and the first one is a JSON string json_string = query_attributes.first fixed = JSON.parse(json_string) rescue nil return fixed if fixed
App KeySubscription Key Event Type Class Name Queue
Event Type App KeySubscription Key Class Name Queue Filters
Class Name App KeySubscription Key Event Type Queue Filters