• Stars
    star
    519
  • Rank 85,261 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 15 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

With newer Rails add this to your Gemfile:

# Gemfile
gem 'resque-status'

Then in an initializer:

# config/initializers/resque.rb
Resque.redis = "your/redis/socket" # default localhost:6379
Resque::Plugins::Status::Hash.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::Plugins::Status module. An example job would look something like:

class SleepJob
  include Resque::Plugins::Status

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

One major difference is that instead 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::Plugins::Status::Hash.get(job_id)

This returns a Resque::Plugins::Status::Hash 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::Plugins::Status::Hash.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::Plugins::Status::Hash.statuses #=> [#<Resque::Plugins::Status::Hash>, ...]

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:

set_status(filename: "myfilename")

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

completed('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::Plugins::Status::Hash.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.

Percent Complete and setting the message

Use at or tick to show progress in your job’s perform function (which is displayed on the resque-web status tab). This will also be where Killed is raised if the job is killed.

at(steps_completed, total_steps, "${steps_completed} of #{total_steps} steps completed!")

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 their related keys will expire in expire_in seconds from the last time theyre updated:

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

Testing

Recent versions of Resque introduced ‘Resque.inline` which changes the behavior to instead of enqueueing and performing jobs to just executing them inline. In Resque itself this removes the dependency on a Redis, however, `Resque::Status` uses Redis to store information about jobs, so though `inline` “works”, you will still need to use or mock a redis connection. You should be able to use a library like github.com/causes/mock_redis alongside `inline` if you really want to avoid Redis connections in your test.

resque-web

Though the main purpose of these trackable jobs is to allow you to surface the status of user created jobs through your 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.

More Repositories

1

sammy

Sammy is a tiny javascript framework built on top of jQuery, It's RESTful Evented Javascript.
JavaScript
2,990
star
2

magick

go (golang) wrapper around imagemagick
Go
209
star
3

vegas

Vegas aims to solve the simple problem of creating executable versions of Sinatra/Rack apps.
Ruby
184
star
4

swinger

Swinger is a couchapp for creating and showing presentations. Think Keynote, stored in CouchDB, run via Javascript and Sammy.js.
JavaScript
175
star
5

soca

Sammy On Couch App
JavaScript
156
star
6

sinatra-gen

Rubigen based generator for new sinatra projects
Ruby
146
star
7

jim

jim is your friendly javascript library manager
JavaScript
94
star
8

static_model

ActiveRecord like functionalities for reading from YAML with a simple class implementation
Ruby
61
star
9

resque-metrics

Metrics for your Resque jobs and workers
Ruby
55
star
10

the_json_store

Code for the Sammy Tutorial series
JavaScript
53
star
11

gembox

A sinatra based interface for browsing and admiring your gems
JavaScript
50
star
12

node-logger

A simple logging library that combines the simple APIs of Ruby's logger.rb and browser-js console.log()
JavaScript
47
star
13

stackprof-remote

A Middleware and CLI for fetching and interacting with [StackProf](https://github.com/tmm1/stackprof) dumps.
Ruby
41
star
14

redisk

An interface to Redis that mimic's Ruby's IO classes
Ruby
34
star
15

qadmin

A RESTful resource module and generator
Ruby
28
star
16

restful_query

ActiveRecord extension for querying data in a restful way
Ruby
25
star
17

action

simple soca (couchapp) for managing tasks
JavaScript
21
star
18

basecamphp

PHP Wrapper for the 37 Signals Basecamp API
PHP
21
star
19

sammyjs-org

Sammyjs.org
JavaScript
20
star
20

sinatra-sammy

A Sinatra extension that turns a Sinatra app into a one page app using the teamwork of Sammy.
Ruby
17
star
21

columnlog

a work in progress
Ruby
17
star
22

minitest-display

Patches MiniTest to allow for an easily configurable output. For Ruby 1.9 :D
Ruby
15
star
23

sammy-pushstate

Simple example soca/sammy/couchapp using the Sammy HTML5 History support
JavaScript
12
star
24

downlow

Downloading files on the DL
Ruby
11
star
25

qadmin-demo

A demo Rails app showcasing the Qadmin gem
Ruby
11
star
26

objspace_viewer

WIP Sinatra/AR app to view/analyze heapdumps from Ruby's ObjectSpace.dump_all
Ruby
11
star
27

quirkey.github.com

My github home
JavaScript
10
star
28

is_porterable

A rails plugin for easily adding import/exportability from/to CSV
Ruby
8
star
29

jqunit-spec

Its like BDD for your JS
JavaScript
7
star
30

portfolio

Sinatra app that powers my portfolio
JavaScript
7
star
31

remix-stash

A new way to deal with memcached
Ruby
6
star
32

imanip

Super-quick image resizing using the ImageMagick command line tools
Ruby
6
star
33

sequel-web

Browse (almost) any SQL database with a web browser using the power of Sequel + Sinatra
JavaScript
5
star
34

jekyll_layouts

Layout files for jekyll, specifically for code.quirkey.com
4
star
35

oneforty

An entirely client-side twitter using sammy.js
JavaScript
4
star
36

halpers

General helpers for different projects
Ruby
4
star
37

quirkey_com

Source for quirkey.com
JavaScript
3
star
38

format_benchmark

Some simple benchmarks for a talk about serialization formats in Ruby
Ruby
3
star
39

lighthouse_stats

Some scripts to generate some SCRUMish stats from Lighthouse
Ruby
3
star
40

rails-boot-reporting

A hack to output times of different tasks in the rails (2) boot process
Ruby
3
star
41

ey-cloud-recipes

A starter repo for custom chef recipes on EY's cloud platform
Ruby
2
star
42

rapture.js

Demo soca/sammy app using pushState and storage for jsbbq
JavaScript
2
star
43

qcontent

Mixins and Modules for extending ActiveRecord content models
Ruby
2
star
44

purse

Simple secure password/data storage via Ruby and Git (In development)
Ruby
2
star
45

flashdance

Get your Rails flash[] out of your controllers and into a YAML
Ruby
2
star
46

quirkey_theme

My wordpress theme
JavaScript
2
star
47

sammy-presentations

Presentations about Sammy
1
star
48

redis-reqrep

Ruby
1
star