Bläddra i källkod

Fixed async with result refactor

Andrew Kane 8 år sedan
förälder
incheckning
debadf6846

+ 15 - 10
app/controllers/blazer/queries_controller.rb

@@ -79,12 +79,15 @@ module Blazer
       if @run_id
         @timestamp = blazer_params[:timestamp].to_i
 
-        @columns, @rows, @error, @cached_at = @data_source.run_results(@run_id)
-        @success = !@rows.nil?
+        @result = @data_source.run_results(@run_id)
+        @success = !@result.nil?
 
         if @success
           @data_source.delete_results(@run_id)
-          @just_cached = !@error && @cached_at.present?
+          @columns = @result.columns
+          @rows = @result.rows
+          @error = @result.error
+          @just_cached = !@result.error && @result.cached_at.present?
           @cached_at = nil
           params[:data_source] = nil
           render_run
@@ -101,20 +104,28 @@ module Blazer
         @run_id = Blazer.async ? SecureRandom.uuid : nil
 
         options = {user: blazer_user, query: @query, refresh_cache: params[:check], run_id: @run_id}
-        result = []
         if Blazer.async && request.format.symbol != :csv
+          result = []
           Blazer::RunStatementJob.perform_async(result, @data_source, @statement, options)
           wait_start = Time.now
           loop do
             sleep(0.02)
             break if result.any? || Time.now - wait_start > 3
           end
+          @result = result.first
         else
           @result = @data_source.run_main_statement(@statement, options)
         end
 
         if @result
           @data_source.delete_results(@run_id) if @run_id
+
+          @columns = @result.columns
+          @rows = @result.rows
+          @error = @result.error
+          @cached_at = @result.cached_at
+          @just_cached = @result.just_cached
+
           render_run
         else
           @timestamp = Time.now.to_i
@@ -166,12 +177,6 @@ module Blazer
     end
 
     def render_run
-      @columns = @result.columns
-      @rows = @result.rows
-      @error = @result.error
-      @cached_at = @result.cached_at
-      @just_cached = @result.just_cached
-
       @checks = @query ? @query.checks : []
 
       @first_row = @rows.first || []

+ 1 - 1
lib/blazer/data_source.rb

@@ -136,7 +136,7 @@ module Blazer
     def read_cache(cache_key)
       value = Blazer.cache.read(cache_key)
       if value
-        Blazer::Result.new(*Marshal.load(value))
+        Blazer::Result.new(self, *Marshal.load(value), nil)
       end
     end
 

+ 1 - 1
lib/blazer/run_statement_job.rb

@@ -8,7 +8,7 @@ module Blazer
     def perform(result, data_source, statement, options)
       ActiveRecord::Base.connection_pool.with_connection do
         data_source.connection_model.connection_pool.with_connection do
-          result.concat(data_source.run_main_statement(statement, options))
+          result << data_source.run_main_statement(statement, options)
         end
       end
     end