Giter Club home page Giter Club logo

resque-status's Introduction

resque-status

resque-status is an extension to the resque queue system that provides simple trackable jobs.

About

resque-status provides a set of simple classes that extend resque’s default functionality (with 0% monkey patching) to give apps a way to track specific job instances and their status. It achieves this by giving job instances UUID’s and allowing the job instances to report their status from within their iterations.

Installation

resque-status *requires Redis >= 1.1* (though I recommend getting the latest stable version). You can download Redis here: code.google.com/p/redis/ or install it using homebrew (brew install redis).

Install the resque-status gem (which will pull in the dependencies).

gem install resque-status

To use with Rails, you can install as a plugin or add the gem to you’re config:

# environment.rb
config.gem 'resque-status', :lib => 'resque/status'

Then in an initializer:

# config/initializers/resque.rb
require 'resque/job_with_status'

Resque.redis = "your/redis/socket" # default localhost:6379
Resque::Status.expire_in = (24 * 60 * 60) # 24hrs in seconds

Usage

The most direct way to use resque-status is to create your jobs using the Resque::JobWithStatus class. An example job would look something like:

class SleepJob < Resque::JobWithStatus

  def perform
    total = options['length'].to_i || 1000
    num = 0
    while num < total
      at(num, total, "At #{num} of #{total}")
      sleep(1)
      num += 1
    end
    completed
  end

end

Instead of normal Resque job classes, we inherit from the JobWithStatus class. Another major difference is that intead of implementing perform as a class method, we do our job implementation within instances of the job class.

In order to queue a SleepJob up, we also won’t use Resque.enqueue, instead we’ll use the create class method which will wrap enqueue and creating a unique id (UUID) for us to track the job with.

job_id = SleepJob.create(:length => 100)

This will create a UUID enqueue the job and pass the :length option on the SleepJob instance as options (as you can see above).

Now that we have a UUID its really easy to get the status:

status = Resque::Status.get(job_id)

This returns a Resque::Status object, which is a Hash (with benefits).

status.pct_complete #=> 0
status.status #=> 'queued'
status.queued? #=> true
status.working? #=> false
status.time #=> Time object
status.message #=> "Created at ..."

Once the worker reserves the job, the instance of SleepJob updates the status at each iteration using at()

status = Resque::Status.get(job_id)
status.working? #=> true
status.num #=> 5
status.total => 100
status.pct_complete => 5

If an error occurs within the job instance, the status is set to ‘failed’ and then the error is re-raised so that Resque can capture it.

Its also possible to get a list of current/recent job statuses:

Resque::Status.statuses #=> [#<Resque::Status>, ...]

Passing back data from the job

You may want to save data from inside the job to access it from outside the job.

A common use-case is web-triggered jobs that create files, later available for download by the user.

A Status is actually just a hash, so inside a job you can do:

status['filename'] = '/myfilename'

Also, all the status setting methods take any number of hash arguments. So you could do:

complete('filename' => '/myfilename')

Kill! Kill! Kill!

Because we’re tracking UUIDs per instance, and we’re checking in/updating the status on each iteration (using at or tick) we can kill specific jobs by UUID.

Resque::Status.kill(job_id)

The next time the job at job_id calls at or tick, it will raise a Killed error and set the status to killed.

Expiration

Since Redis is RAM based, we probably don’t want to keep these statuses around forever (at least until @antirez releases the VM feature). By setting expire_in, all statuses and thier related keys will expire in expire_in seconds from the last time theyre updated:

Resque::Status.expire_in = (60 * 60) # 1 hour

resque-web

Though the main purpose of these trackable jobs is to allow you to surface the status of user created jobs through you’re apps’ own UI, I’ve added a simple example UI as a plugin to resque-web.

To use, you need to setup a resque-web config file:

# ~/resque_conf.rb
require 'resque/status_server'

Then start resque-web with your config:

resque-web ~/resque_conf.rb

This should launch resque-web in your browser and you should see a ‘Statuses’ tab.

More

Source: github.com/quirkey/resque-status API Docs: rdoc.info/projects/quirkey/resque-status Examples: github.com/quirkey/resque-status/tree/master/examples Resque: github.com/defunkt/resque

Thanks

Resque is awesome, @defunkt needs a shout-out.

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 Aaron Quint. See LICENSE for details.

resque-status's People

Contributors

dbalatero avatar drewski371-bot avatar karmi avatar quirkey avatar thbar avatar zapnap avatar

Stargazers

 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.