Skip to content

Instantly share code, notes, and snippets.

@bradleybuda
Last active December 13, 2015 22:49
Show Gist options
  • Select an option

  • Save bradleybuda/4987622 to your computer and use it in GitHub Desktop.

Select an option

Save bradleybuda/4987622 to your computer and use it in GitHub Desktop.
How not to use resque-status
class ExpensiveJob
include Resque::Plugins::Status
def perform
Service.find(options['id']).create_user
end
end
interval = 500
$('#create-user-form').on 'ajax:success', ->
url = jqXHR.getResponseHeader 'Location'
poll = ->
$.ajax
url: url
success: (data, textStatus, jqXHR) ->
if jqXHR.status == 202
setTimeout(poll, interval)
else
window.location = jqXHR.getResponseHeader 'Location'
setTimeout(poll, interval)
class ServicesController
def create_user
service = current_user.services.find(params[:id])
job_id = ExpensiveJob.create(service.id)
head :accepted, location: poll_services_path(job_id: job_id)
end
def poll
job_id = params[:job_id]
status = Resque::Plugins::Status::Hash.get(job_id).status
if %w(queued working).member? status
head :accepted, location: poll_services_path(job_id: job_id)
else
head :created, location: all_done_services_path(job_id: job_id)
end
end
def all_done
job_id = params[:job_id]
account_id = Resque::Plugins::Status::Hash.get(job_id).account_id
render json: {account_id: account_id}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment