Giter Club home page Giter Club logo

resque-poll's Introduction

Build Status

resque-poll

resque-poll is a Rails engine that allows you to easily do long polling for Resque jobs. resque-poll depends on resque-status.

Installation

Include resque-poll in your Gemfile:

gem 'resque-poll'

Mount the engine in your routes.rb:

mount ResquePoll::Engine => '/resque_poll'

Require the javascript files & the dependencies inyour application.js javascript file (requires Asset Pipeline):

//= require jquery
//= require resque_poll

Usage

In a form, the data-resque-poll attribute to your form tag:

<%= form_for @movie, html: { data: { 'resque-poll' => true } } do |f| %>
  <%= f.input :title %>
  <%= f.submit %>

Your controller can then use ResquePoll to return the appropriate response to begin polling:

class MoviesController < ApplicationController
  def create
    poll = ResquePoll::Job.create(LongRunningMoveJob, {title: params[:title]})
    render json: poll.to_json
  end
end

resque-poll will fire events when the polling has completed (successfully or unsuccessfully). To make use of this, you can hook into the triggered events and take action on them. Any data passed back from the resque job will also be available to you in the response.

The following events are fired:

  • resque:poll:stopped - fired when processing has finished successfully or unsuccessfully
  • resque:poll:success - fired when processing has finished successfully
  • resque:poll:stopped - fired when processing has finished unsuccessfully

Continuing with our form from above:

$('form')
  .bind('resque:poll:stopped', function(event, response) {
    console.log('Processing finished!');
  }).bind('resque:poll:success', function(event, response) {
    console.log('Processing successful!');
  }).bind('resque:poll:error', function(event, response) {
    console.log('Processing failed!');
  });

Since a common use case would be to enable/disable the submit button during the course of processing the background job, the resque-poll-disable-with attribute can be used to automatically disable/enable a button with a given value when processing has begun or completed.

<%= f.submit :'data-resque-poll-disable-with' => 'Creating...' %>

Dependencies

resque-poll's latest version, 2.0.0, is dependent on Rails 4.0.0. It is not backwards-compatible with Rails 3. For the Rails 3 compatible version of resque-poll, please downgrade to 1.0.1.

resque-poll's People

Contributors

agau4779 avatar alan-andrade avatar anthonylumos avatar cfurrow avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.