Giter Club home page Giter Club logo

reliable's Introduction

Reliable is no longer maintained. The code is still public for reference.


Build Status

Reliable is.

Redis is a great storage service for building a reliable queue. That's what this is for.

Is this like Ost?

Ost was the inspiration for this project. We love ost, but it lacks a few of the nice things we want (retry, failures count, etc) and will will implement those extra features here. We also wanted parallelism baked in.

Configuring redis

One must set the REDIS_URL environment variable.

Enqueueing messages

The developer is responsible for enqueuing Strings or string-like objects.

Reliable[:messages].push(JSON.generate({
  id: 123,
  title: "Hello"
}))

In this example :messages is the queue name. The developer can make as many queues as they like.

Processing messages

Processing all messages as they arrive

Reliable[:messages].each do |message|
  hash = JSON.generate(message)
  DatabaseTable.find(hash["id"]).do_something_awesome(message)
end

or

Reliable[:emails].each do |message|
  hash = JSON.generate(message)
  Emailer.new(hash).deliver
end

Calling #each will block the main thread and sleep forever.

Processing messages in parallel

If the processing code is thread-safe, the developer can spawn any number of threads with #peach:

Reliable[:touches].peach(concurrency: 12) { |id| Model.find(id).touch }

In this example 12 threads will be created and all are joined with the main thread.

Processing some messages

It's also possible to process only some message by #take-ing as many as necessary:

Reliable[:urls].take(2) do |url|
  content = open(url)
  PersistentStore.store(content)
end

Or if you just want the urls themselves as an array:

urls = Reliable[:urls].take(2)

urls.each do |url|
  content = open(url)
  PersistentStore.store(content)
end

And if the developer wants, they can get an enumerator object and interact with it as necessary:

enumerator = Reliable[:ids].to_enum { |id| notify(id) }
assert_equal 0, notifications.length
4.times { enumerator.next }
assert_equal 4, notifications.length

Time

Make sure the distributed clock starts moving before you lock the main thread. The clock makes it possible to re-enqueue stale items.

Here is a full example:

Reliable[:emails].periodically_move_time_forward
Reliable[:emails].peach(concurrency: 6) do |message|
  hash = JSON.generate(message)
  Emailer.new(hash).deliver
end

Rails

Probably best to create an initializer:

# config/initializers/reliable.rb
Reliable[:emails].periodically_move_time_forward

Then in a controller one might:

# app/controllers/users_controller.rb
class UsersController < ApplicationController
  def create
    @user = User.create!(params.require(:email))
    Reliable[:emails].push JSON.generate({
      user_id: @user.id
    })
    redirect_to root_url
  end
end

Then, in a worker file:

# app/workers/emails_worker.rb
Reliable[:emails].peach(concurrency: 6) do |message|
  hash = JSON.parse(message)
  user = User.find(hash[:user_id])
  Emailer.welcome_email(user).deliver
end

And maybe one would have a Procfile like this:

web: bin/rails s -p$PORT
worker: bin/rails r app/workers/emails_worker.rb

reliable's People

Contributors

myobie avatar nathan-ms avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

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

reliable's Issues

Usage without Rails

@nathan-ms I'm trying to use the gem:

require 'reliable'
require 'awesome_print'

ENV['REDIS_URL'] = 'redis://localhost:6379/0'

Reliable[:messages].push(JSON.generate({
  id: 123,
  title: "Hello"
}))

# assuming reliable:queues:messages gets created ?

Reliable[:messages].each do |message|
  puts %|Got #{message}|
end

Unfortunately getting:

redis.rb:6:in `<main>': undefined method `[]' for Reliable:Module (NoMethodError)

Any idea ?

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.