Giter VIP home page Giter VIP logo

stalker's Introduction

This repo is unmaintained

Please go to the new official fork

Stalker - a job queueing DSL for Beanstalk

Beanstalkd is a fast, lightweight queueing backend inspired by mmemcached. The Ruby Beanstalk client is a bit raw, however, so Stalker provides a thin wrapper to make job queueing from your Ruby app easy and fun.

Queueing jobs

From anywhere in your app:

require 'stalker'

Stalker.enqueue('email.send', :to => '[email protected]')
Stalker.enqueue('post.cleanup.all')
Stalker.enqueue('post.cleanup', :id => post.id)

Working jobs

In a standalone file, typically jobs.rb or worker.rb:

require 'stalker'
include Stalker

job 'email.send' do |args|
  Pony.send(:to => args['to'], :subject => "Hello there")
end

job 'post.cleanup.all' do |args|
  Post.all.each do |post|
    enqueue('post.cleanup', :id => post.all)
  end
end

job 'post.cleanup' do |args|
  Post.find(args['id']).cleanup
end

Running

First, make sure you have Beanstalkd installed and running:

$ sudo port install beanstalkd
$ beanstalkd

Stalker:

$ sudo gem install stalker

Now run a worker using the stalk binary:

$ stalk jobs.rb
Working 3 jobs: [ email.send post.cleanup.all post.cleanup ]
Working send.email ([email protected])
Finished send.email in 31ms

Stalker will log to stdout as it starts working each job, and then again when the job finishes including the ellapsed time in milliseconds.

Filter to a list of jobs you wish to run with an argument:

$ stalk jobs.rb post.cleanup.all,post.cleanup
Working 2 jobs: [ post.cleanup.all post.cleanup ]

In a production environment you may run one or more high-priority workers (limited to short/urgent jobs) and any number of regular workers (working all jobs). For example, two workers working just the email.send job, and four running all jobs:

$ for i in 1 2; do stalk jobs.rb email.send > log/urgent-worker.log 2>&1; end
$ for i in 1 2 3 4; do stalk jobs.rb > log/worker.log 2>&1; end

Error Handling

If you include an error block in your jobs definition, that block will be invoked when a worker encounters an error. You might use this to report errors to an external monitoring service:

error do |e, job, args|
  Exceptional.handle(e)
end

Before filter

If you wish to run a block of code prior to any job:

before do |job|
  puts "About to work #{job}"
end

Tidbits

  • Jobs are serialized as JSON, so you should stick to strings, integers, arrays, and hashes as arguments to jobs. e.g. don't pass full Ruby objects - use something like an ActiveRecord/MongoMapper/CouchRest id instead.
  • Because there are no class definitions associated with jobs, you can queue jobs from anywhere without needing to include your full app's environment.
  • If you need to change the location of your Beanstalk from the default (localhost:11300), set BEANSTALK_URL in your environment, e.g. export BEANSTALK_URL=beanstalk://example.com:11300/
  • The stalk binary is just for convenience, you can also run a worker with a straight Ruby command: $ ruby -r jobs -e Stalker.work

Running the tests

If you wish to hack on Stalker, install these extra gems:

$ gem install contest mocha

Make sure you have a beanstalkd running, then run the tests:

$ ruby test/stalker_test.rb

Meta

Created by Adam Wiggins

Patches from Jamie Cobbett, Scott Water, Keith Rarick, Mark McGranaghan, Sean Walberg, Adam Pohorecki

Heavily inspired by Minion by Orion Henry

Released under the MIT License: http://www.opensource.org/licenses/mit-license.php

http://github.com/adamwiggins/stalker

stalker's People

Contributors

adamwiggins avatar kr avatar mmcgrana avatar scottwater avatar swalberg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

stalker's Issues

Mysql server has gone away

HI
I'm using stalker on engineyard hosting for sending emails in a background. So generally it works fine but after some time it stops to send emails. I check logs and see errors
"Mysql server has gone away"
What I'm doing wrong?

thanks in advance

Stalker gem needs to specify json as a dependency

Noticed it when working with bundler:

1.8.7 stealth:vc_whitebox jcobbett$ bundle exec stalk app/workers/card_data.rb
/Users/cobbo/projects/vc_whitebox/lock/gems/stalker-0.2.4/bin/../lib/stalker.rb:2:in `require': no such file to load -- json (LoadError)

Blocking job queuing with response

I'd like to see something like :

response = Stalker.enqueue_with_response("sum", :a => 1, :b => 2)
or 
Stalker.enqueue_with_response("sum", :a => 1, :b => 2) do |response|
  puts "1 +2 = #{response['sum']}"
end

and the worker:

job_with_response "sum" do |args| 
  return :sum => args['a'] + args['b']
end

This would work by adding something like :respond_to => "stalker-response-#{$$}-#{SecureRandom.uuid}" to the job params, then subscribing to that queue (perhaps with a configurable timeout) where the worker will then enqueue it's return value once finished.

There could also be partial responses so one could do something like:

job_with_response "sum" do |args, response|
  response['status'] = 'going to calculate!'
  response['status_pct'] = 0
  response['sum'] = args['a'] + args['b']
  response['status'] = 'done calculating!'
  response['status_pct'] = 100
end

Getting a little carried away here.

Beanstalk InvalidTubeName

I was trying stalker and when I try to start a worker I get Beanstalk::InvalidTubeName . I am using ruby 1.8.6 rails 2.3.3 and beanstalk-client 1.1.0 and stalker 0.9.0

Here is the full trace

/home/jetset/.rvm/gems/ruby-1.8.6-p420/gems/beanstalk-client-1.1.0/lib/beanstalk-client/connection.rb:146:in watch': fetch_trends (Beanstalk::InvalidTubeName) from /home/jetset/.rvm/gems/ruby-1.8.6-p420/gems/beanstalk-client-1.1.0/lib/beanstalk-client/connection.rb:382:insend'
from /home/jetset/.rvm/gems/ruby-1.8.6-p420/gems/beanstalk-client-1.1.0/lib/beanstalk-client/connection.rb:382:in call_wrap' from /home/jetset/.rvm/gems/ruby-1.8.6-p420/gems/beanstalk-client-1.1.0/lib/beanstalk-client/connection.rb:412:insend_to_all_conns'
from /home/jetset/.rvm/gems/ruby-1.8.6-p420/gems/beanstalk-client-1.1.0/lib/beanstalk-client/connection.rb:424:in map_hash' from /home/jetset/.rvm/rubies/ruby-1.8.6-p420/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:inmap'
from /home/jetset/.rvm/gems/ruby-1.8.6-p420/gems/beanstalk-client-1.1.0/lib/beanstalk-client/connection.rb:424:in each' from /home/jetset/.rvm/gems/ruby-1.8.6-p420/gems/beanstalk-client-1.1.0/lib/beanstalk-client/connection.rb:424:inmap'
from /home/jetset/.rvm/gems/ruby-1.8.6-p420/gems/beanstalk-client-1.1.0/lib/beanstalk-client/connection.rb:424:in map_hash' ... 7 levels... from /home/jetset/.rvm/gems/ruby-1.8.6-p420/gems/stalker-0.9.0/lib/stalker.rb:63:inwork'
from /home/jetset/.rvm/gems/ruby-1.8.6-p420/gems/stalker-0.9.0/bin/stalk:21
from /home/jetset/.rvm/gems/ruby-1.8.6-p420/bin/stalk:19:in `load'
from /home/jetset/.rvm/gems/ruby-1.8.6-p420/bin/stalk:19

Hangs on the same job when saving without validation

job "address.geocode" do |args|
    address = Address.find(args["id"])
    address.geocode
    address.save
end

does work. But when using address.save :validate => false the same job gets processed over and over again until it fails.

Working address.geocode (id=4)
Finished address.geocode in 233ms 
Working address.geocode (id=4)
Finished address.geocode in 321ms 
Working address.geocode (id=4)
Finished address.geocode in 221ms 
Working address.geocode (id=4)
Finished address.geocode in 188ms 
Working address.geocode (id=4)
...


Idk if it's just me or if this is a bug. However I would mention this in the wiki.

Exception Stalker::JobTimeout

Hi,

This isn't exactly an issue but i am wondering if there is a way to add a job to the stack but set the timeout manually. I have everything configured to process jobs for video transcoding however these jobs take a long time so I need to set the Timeout to a few hours or so.

Thank you advance.

Crash not caught

The worker exited after this exception, why didn't the error handler catch it?

[Thu Sep 09 19:04:32 +0000 2010] -> instance.launch app/models/instance.rb:70:in `launch': Instance::RunawayLaunches (Instance::RunawayLaunches)
from ./jobs.rb:24
from .bundle/gems/stalker-0.4.2/lib/stalker.rb:62:in `call'
from .bundle/gems/stalker-0.4.2/lib/stalker.rb:62:in `work_one_job'
from .bundle/gems/stalker-0.4.2/lib/stalker.rb:53:in `work'
from .bundle/gems/stalker-0.4.2/lib/stalker.rb:53:in `loop'
from .bundle/gems/stalker-0.4.2/lib/stalker.rb:53:in `work'
from .bundle/gems/stalker-0.4.2/bin/stalk:19
from .bundle/bin/stalk:19:in `load'
from .bundle/bin/stalk:19

Add the possibility to use multiple beanstalkd servers

Currently, stalker manage only 1 beanstalkd server. It is hard coded to initialize a pool of 1 server. (Beanstalk::Pool.new([ beanstalk_host_and_port ])).

Could you add the possibility to set a collection of server based on a configuration?

Thanks,

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.