• Stars
    star
    606
  • Rank 73,958 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 13 years ago
  • Updated over 10 years ago

Reviews

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

Repository Details

Have a task you want to get done but don't want to do it yourself? Give it to girl_friday!

Please use Brandon Hilkert's sucker_punch instead of girl_friday now.

girl_friday is a Ruby library for performing asynchronous tasks. Often times you don't want to block a web response by performing some task, like sending an email, so you can just use this gem to perform it in the background. It works with any Ruby application, including Rails 3 applications.

Installation

We recommend using JRuby 1.6+ or Rubinius 2.0+ with girl_friday. Both are excellent options for executing Ruby these days.

gem install girl_friday

girl_friday does not support Ruby 1.8 (MRI) because of its lack of native threading. Ruby 1.9.3 will work reasonably well if you use gems that release the GIL for network I/O (mysql2 is a good example of this, do not use the original mysql gem).

Usage

Put girl_friday in your Gemfile:

gem 'girl_friday'

In your Rails app, create a config/initializers/girl_friday.rb which defines your queues:

EMAIL_QUEUE = GirlFriday::WorkQueue.new(:user_email, :size => 3) do |msg|
  UserMailer.registration_email(msg).deliver
end

IMAGE_QUEUE = GirlFriday::WorkQueue.new(:image_crawler, :size => 7) do |msg|
  ImageCrawler.process(msg)
end

:size is the number of workers to spin up and defaults to 5. Keep in mind, ActiveRecord defaults to a connection pool size of 5 so if your workers are accessing the database you'll want to ensure that the connection pool is large enough by modifying config/database.yml.

In order to use the Redis backend, you must use a connection pool to share a set of Redis connections with other threads and GirlFriday queues using the connection_pool gem:

require 'connection_pool'

redis_pool = ConnectionPool.new(:size => 5, :timeout => 5) { Redis.new }

CLEAN_FILTER_QUEUE = GirlFriday::WorkQueue.new(:clean_filter, :store => GirlFriday::Store::Redis, :store_config => { :pool => redis_pool }) do |msg|
  Filter.clean(msg)
end

In your controller action or model, you can call #push(msg)

EMAIL_QUEUE.push(:email => @user.email, :name => @user.name)

The msg parameter to push is just a Hash whose contents are completely up to you.

Your message processing block should not access any instance data or variables outside of the block. That's shared mutable state and dangerous to touch! I also strongly recommend your queue processor block be VERY short, ideally just a method call or two. You can unit test those methods easily but not the processor block itself.

You can call GirlFriday::WorkQueue.immediate! to process jobs immediately, which is helpful when testing. GirlFriday::WorkQueue.queue! will revert this & jobs will be processed by actors.

Queues are not garbage collected until they are shutdown, even if you have no reference to them. Make sure you call WorkQueue#shutdown if you are dynamically creating them so you don't leak memory. GirlFriday.shutdown! will shut down all running queues in the process.

More Detail

Please see the girl_friday wiki for more detail and advanced options and tuning. You'll find details on queue persistence with Redis, implementing clean shutdown, querying runtime metrics and SO MUCH MORE!

From wikipedia:

The term Man Friday has become an idiom, still in mainstream usage, to describe an especially faithful servant or one's best servant or right-hand man. The female equivalent is Girl Friday. The title of the movie His Girl Friday alludes to it and may have popularized it.

Thanks

Carbon Five, I wrote and maintained girl_friday on their clock.

This gem contains a copy of the Rubinius Actor API, modified to work on any Ruby VM. Thanks to Evan Phoenix, MenTaLguY and the Rubinius project for permission to use and distribute this code.

Author

Mike Perham, @mperham, mikeperham.com

More Repositories

1

sidekiq

Simple, efficient background processing for Ruby
Ruby
12,059
star
2

inspeqtor

Monitor your application infrastructure!
Go
1,657
star
3

connection_pool

Generic connection pooling for Ruby
Ruby
1,627
star
4

rack-fiber_pool

Rack middleware to execute each request in a Fiber
Ruby
244
star
5

bayes_motel

Multi-variate Bayesian classification engine
Ruby
190
star
6

evented

Your source for event-drivenness!
Ruby
172
star
7

phat

Asynchronous Rails!
Ruby
95
star
8

em_postgresql

ActiveRecord driver for Postgresql with EventMachine
Ruby
65
star
9

kuiq

Sidekiq desktop app
Ruby
55
star
10

inspeqtor-pro

Modern service monitoring, business edition.
Go
52
star
11

politics

Utilities and Algorithms for Distributed Processing.
Ruby
47
star
12

qanat

Fiber-based, highly concurrent MQ processor for Ruby
Ruby
39
star
13

chrono.js

Application metrics, webscale!
JavaScript
22
star
14

job_roundup

Benchmarking various background job engines
Ruby
17
star
15

bloaty_mcbloatface

"Oh my, you've put on some weight..."
Ruby
12
star
16

acting_lessons

Abusing Rubinius's Actors for fun and profit
Ruby
10
star
17

phony_baloney

Create fake servers for testing purposes
Ruby
10
star
18

gobox

How to use Golang's NaCl crypto API
Go
10
star
19

breakout

The classic Breakout game
Ruby
9
star
20

dotfiles

my dotfiles
Shell
7
star
21

blog

My blargh!
HTML
7
star
22

switch_redis

Ruby
7
star
23

discontent

The forum for terrible people
Ruby
7
star
24

sidekiq-websockets

Ruby
7
star
25

dotenv

Load contents of .env into child processes
Go
6
star
26

resque-client.go

Resque worker/client, written in Go
Go
6
star
27

contribsys.com

contribsys.com
CSS
5
star
28

edistuff

EDI parser and generation code
Java
5
star
29

queso

Query anything!
Ruby
5
star
30

gem_dir

Adds the 'gem dir' command to RubyGems to display the root directory of a given gem.
Ruby
4
star
31

tracknowledge

Race Tracks Galore!
Ruby
4
star
32

aoc2016

Advent of Code 2016
Ruby
4
star
33

right_aws

RightScale's right_aws 1.10.0 gem with fixes
Ruby
3
star
34

minesweeper

Minesweeper for MacRuby
3
star
35

docdb_shootout

Document-oriented database comparision
Ruby
3
star
36

news_flash

Show your users the latest news
Ruby
2
star
37

slice

My slice configuration
Ruby
1
star
38

perham.net

HTML
1
star