Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Instead of blocking on the completion of HTTP requests using

JenxConnectionManager::value (which does a GCD group wait and thus
blocks the main thread and makes the UI unresponsive), allow GCD tasks
to run asynchronously and call the passed-in block on the main thread
via Dispatch::Queue.main.async (because UI stuff must be done on the
main thread to prevent crashes). This eliminates the spinning beach ball
when Jenx is doing HTTP requests to retrieve the status from Jenkins.

Fixes GH-37.
  • Loading branch information...
commit f6b8a1973eaa6c030c522e2e27f7e8268e99483b 1 parent 1ae2bdb
@msabramo authored
View
44 jenx/Jenx.rb
@@ -53,29 +53,35 @@ def ensure_connection(sender=nil)
if @refresh_timer.nil? || !@refresh_timer.isValid
create_timer
end
- JenxConnection.new(@prefs.build_server_url, @prefs.username, @prefs.password).is_connected? ? fetch_current_build_status : handle_broken_connection(ERROR_SERVER_CANNOT_BE_CONTACTED)
+ JenxConnection.new(@prefs.build_server_url, @prefs.username, @prefs.password).is_connected? do |is_connected|
+ is_connected ? fetch_current_build_status : handle_broken_connection(ERROR_SERVER_CANNOT_BE_CONTACTED)
+ end
end
def fetch_current_build_status
@old_default_build_status = @new_default_build_status
- @all_projects = JenxConnection.new(@prefs.build_server_url, @prefs.username, @prefs.password).all_projects
- NSLog("fetching current build status for #{@prefs.num_menu_projects} projects...")
-
- default_project_status_color = ''
- @all_projects['jobs'].find {|p| default_project_status_color = p['color'] if p['name'].downcase.eql?(@prefs.default_project.downcase)}
- @new_default_build_status = get_current_status_for(default_project_status_color)
-
- jenx_status_item = @jenx_item.menu.itemAtIndex(0)
- jenx_status_item.setTitle(CONNECTED)
-
- @menu_default_project.setTitle(localize_format("Project: %@", "#{@prefs.default_project}"))
- @menu_default_project_status.setTitle(localize_format("Status: %@", "#{@new_default_build_status}"))
- date = Time.now.strftime(localize("%I:%M:%S %p", "%I:%M:%S %p"))
- @menu_default_project_update_time.setTitle(localize_format("Last Update: %@", date))
-
- @jenx_item.setImage(get_current_status_icon_for(default_project_status_color, nil))
-
- load_projects
+ JenxConnection.new(@prefs.build_server_url, @prefs.username, @prefs.password).all_projects do |all_projects|
+ @all_projects = all_projects
+ if @all_projects then
+ NSLog("fetching current build status for #{@prefs.num_menu_projects} projects...")
+
+ default_project_status_color = ''
+ @all_projects['jobs'].find {|p| default_project_status_color = p['color'] if p['name'].downcase.eql?(@prefs.default_project.downcase)}
+ @new_default_build_status = get_current_status_for(default_project_status_color)
+
+ jenx_status_item = @jenx_item.menu.itemAtIndex(0)
+ jenx_status_item.setTitle(CONNECTED)
+
+ @menu_default_project.setTitle(localize_format("Project: %@", "#{@prefs.default_project}"))
+ @menu_default_project_status.setTitle(localize_format("Status: %@", "#{@new_default_build_status}"))
+ date = Time.now.strftime(localize("%I:%M:%S %p", "%I:%M:%S %p"))
+ @menu_default_project_update_time.setTitle(localize_format("Last Update: %@", date))
+
+ @jenx_item.setImage(get_current_status_icon_for(default_project_status_color, nil))
+
+ load_projects
+ end
+ end
rescue Exception => e
NSLog("error fetching build status: #{e.message}...")
end
View
18 jenx/JenxConnection.rb
@@ -25,7 +25,7 @@ def initSSL(http, scheme)
end
end
- def all_projects
+ def all_projects(&block)
uri = URI.parse(@url)
if !uri.respond_to?(:request_uri) then
@@ -34,6 +34,7 @@ def all_projects
end
connection_result = JenxConnectionManager.new do
+ all_projects = nil
http = Net::HTTP.new(uri.host, uri.port)
initSSL(http, uri.scheme)
req = Net::HTTP::Get.new(uri.request_uri + JENX_API_URI)
@@ -41,13 +42,16 @@ def all_projects
response = http.request(req)
if response.code_type == Net::HTTPOK then
result = response.body
- JSON.parse(result)
+ all_projects = JSON.parse(result)
+ end
+ Dispatch::Queue.main.async do
+ block.call(all_projects)
end
end
- connection_result.value
+ ## This blocks: connection_result.value
end
- def is_connected?
+ def is_connected?(&block)
connection_result = JenxConnectionManager.new do
uri = URI.parse(@url)
http = Net::HTTP.new(uri.host, uri.port)
@@ -55,8 +59,10 @@ def is_connected?
req = Net::HTTP::Head.new(uri.request_uri + JENX_API_URI)
auth(req)
response = http.request(req)
- response.code_type == Net::HTTPOK
+ Dispatch::Queue.main.async do
+ block.call(response.code_type == Net::HTTPOK)
+ end
end
- connection_result.value
+ ## This blocks: connection_result.value
end
end
View
20 jenx/PreferencesGeneralViewController.rb
@@ -53,15 +53,17 @@ def load_projects(sender=nil)
@project_list.removeAllItems
begin
url = @server_url.stringValue[-1,1].eql?('/') ? @server_url.stringValue : @server_url.stringValue + '/'
- @all_projects = JenxConnection.new(url, @username.stringValue, @password.stringValue).all_projects
- @all_projects['jobs'].each do |project|
- @project_list.addItemWithObjectValue(project['name'])
- end
-
- if !@prefs.default_project
- @project_list.selectItemWithObjectValue(0)
- else
- @project_list.selectItemWithObjectValue(@prefs.default_project)
+ JenxConnection.new(url, @username.stringValue, @password.stringValue).all_projects do |all_projects|
+ @all_projects = all_projects
+ @all_projects['jobs'].each do |project|
+ @project_list.addItemWithObjectValue(project['name'])
+ end
+
+ if !@prefs.default_project
+ @project_list.selectItemWithObjectValue(0)
+ else
+ @project_list.selectItemWithObjectValue(@prefs.default_project)
+ end
end
rescue URI::InvalidURIError => uri_error
NSLog(uri_error.inspect)
Please sign in to comment.
Something went wrong with that request. Please try again.