Last active
December 13, 2015 22:49
-
-
Save bradleybuda/4987622 to your computer and use it in GitHub Desktop.
How not to use resque-status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ExpensiveJob | |
| include Resque::Plugins::Status | |
| def perform | |
| Service.find(options['id']).create_user | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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